|
| 1 | +# Patching Sample |
| 2 | + |
| 3 | +This sample shows how to safely alter a workflow using `patched` and `deprecate_patch` in stages. |
| 4 | + |
| 5 | +To run, first see [README.md](../README.md) for prerequisites. Then follow the patching stages below. |
| 6 | + |
| 7 | +### Stage 1 - Initial code |
| 8 | + |
| 9 | +This stage is for existing running workflows. To simulate our initial workflow, run the worker in a separate terminal: |
| 10 | + |
| 11 | + poetry run python worker.py --workflow initial |
| 12 | + |
| 13 | +Now we can start this workflow: |
| 14 | + |
| 15 | + poetry run python starter.py --start-workflow initial-workflow-id |
| 16 | + |
| 17 | +This will output "Started workflow with ID initial-workflow-id and ...". Now query this workflow: |
| 18 | + |
| 19 | + poetry run python starter.py --query-workflow initial-workflow-id |
| 20 | + |
| 21 | +This will output "Query result for ID initial-workflow-id: pre-patch". |
| 22 | + |
| 23 | +### Stage 2 - Patch the workflow |
| 24 | + |
| 25 | +This stage is for needing to run old and new workflows at the same time. To simulate our patched workflow, stop the |
| 26 | +worker from before and start it again with the patched workflow: |
| 27 | + |
| 28 | + poetry run python worker.py --workflow patched |
| 29 | + |
| 30 | +Now let's start another workflow with this patched code: |
| 31 | + |
| 32 | + poetry run python starter.py --start-workflow patched-workflow-id |
| 33 | + |
| 34 | +This will output "Started workflow with ID patched-workflow-id and ...". Now query the old workflow that's still |
| 35 | +running: |
| 36 | + |
| 37 | + poetry run python starter.py --query-workflow initial-workflow-id |
| 38 | + |
| 39 | +This will output "Query result for ID initial-workflow-id: pre-patch" since it is pre-patch. But if we execute a query |
| 40 | +against the new code: |
| 41 | + |
| 42 | + poetry run python starter.py --query-workflow patched-workflow-id |
| 43 | + |
| 44 | +We get "Query result for ID patched-workflow-id: post-patch". This is how old workflow code can take old paths and new |
| 45 | +workflow code can take new paths. |
| 46 | + |
| 47 | +### Stage 3 - Deprecation |
| 48 | + |
| 49 | +Once we know that all workflows that started with the initial code from "Stage 1" are no longer running, we don't need |
| 50 | +the patch so we can deprecate it. To use the patch deprecated workflow, stop the workflow from before and start it again |
| 51 | +with: |
| 52 | + |
| 53 | + poetry run python worker.py --workflow patch-deprecated |
| 54 | + |
| 55 | +All workflows in "Stage 2" and any new workflows will work. Now let's start another workflow with this patch deprecated |
| 56 | +code: |
| 57 | + |
| 58 | + poetry run python starter.py --start-workflow patch-deprecated-workflow-id |
| 59 | + |
| 60 | +This will output "Started workflow with ID patch-deprecated-workflow-id and ...". Now query the patched workflow that's |
| 61 | +still running: |
| 62 | + |
| 63 | + poetry run python starter.py --query-workflow patched-workflow-id |
| 64 | + |
| 65 | +This will output "Query result for ID patched-workflow-id: post-patch". And if we execute a query against the latest |
| 66 | +workflow: |
| 67 | + |
| 68 | + poetry run python starter.py --query-workflow patch-deprecated-workflow-id |
| 69 | + |
| 70 | +As expected, this will output "Query result for ID patch-deprecated-workflow-id: post-patch". |
| 71 | + |
| 72 | +### Stage 4 - Patch complete |
| 73 | + |
| 74 | +Once we know we don't even have any workflows running on "Stage 2" or before (i.e. the workflow with the patch with |
| 75 | +both code paths), we can just remove the patch deprecation altogether. To use the patch complete workflow, stop the |
| 76 | +workflow from before and start it again with: |
| 77 | + |
| 78 | + poetry run python worker.py --workflow patch-complete |
| 79 | + |
| 80 | +All workflows in "Stage 3" and any new workflows will work. Now let's start another workflow with this patch complete |
| 81 | +code: |
| 82 | + |
| 83 | + poetry run python starter.py --start-workflow patch-complete-workflow-id |
| 84 | + |
| 85 | +This will output "Started workflow with ID patch-complete-workflow-id and ...". Now query the patch deprecated workflow |
| 86 | +that's still running: |
| 87 | + |
| 88 | + poetry run python starter.py --query-workflow patch-deprecated-workflow-id |
| 89 | + |
| 90 | +This will output "Query result for ID patch-deprecated-workflow-id: post-patch". And if we execute a query against the |
| 91 | +latest workflow: |
| 92 | + |
| 93 | + poetry run python starter.py --query-workflow patch-complete-workflow-id |
| 94 | + |
| 95 | +As expected, this will output "Query result for ID patch-complete-workflow-id: post-patch". |
| 96 | + |
| 97 | +Following these stages, we have successfully altered our workflow code. |
0 commit comments