|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require 'temporalio/client' |
| 4 | +require 'temporalio/api/workflowservice/v1/request_response' |
| 5 | +require 'temporalio/worker_deployment_version' |
| 6 | +require 'logger' |
| 7 | +require 'securerandom' |
| 8 | +require_relative 'constants' |
| 9 | + |
| 10 | +def main(client = nil) |
| 11 | + logger = Logger.new($stdout, level: Logger::INFO) |
| 12 | + |
| 13 | + client ||= Temporalio::Client.connect( |
| 14 | + 'localhost:7233', |
| 15 | + 'default', |
| 16 | + logger: |
| 17 | + ) |
| 18 | + |
| 19 | + # Wait for v1 worker and set as current version |
| 20 | + logger.info( |
| 21 | + 'Waiting for v1 worker to appear. Run `ruby worker_versioning/workerv1.rb` in another terminal' |
| 22 | + ) |
| 23 | + wait_for_worker_and_make_current(client, '1.0') |
| 24 | + |
| 25 | + # Start auto-upgrading and pinned workflows. Importantly, note that when we start the workflows, |
| 26 | + # we are using a workflow type name which does *not* include the version number. We defined them |
| 27 | + # with versioned names so we could show changes to the code, but here when the client invokes |
| 28 | + # them, we're demonstrating that the client remains version-agnostic. |
| 29 | + auto_upgrade_workflow_id = "worker-versioning-versioning-autoupgrade_#{SecureRandom.uuid}" |
| 30 | + auto_upgrade_execution = client.start_workflow( |
| 31 | + :AutoUpgrading, |
| 32 | + id: auto_upgrade_workflow_id, |
| 33 | + task_queue: WorkerVersioning::Constants::TASK_QUEUE |
| 34 | + ) |
| 35 | + |
| 36 | + pinned_workflow_id = "worker-versioning-versioning-pinned_#{SecureRandom.uuid}" |
| 37 | + pinned_execution = client.start_workflow( |
| 38 | + :Pinned, |
| 39 | + id: pinned_workflow_id, |
| 40 | + task_queue: WorkerVersioning::Constants::TASK_QUEUE |
| 41 | + ) |
| 42 | + |
| 43 | + logger.info("Started auto-upgrading workflow: #{auto_upgrade_execution.id}") |
| 44 | + logger.info("Started pinned workflow: #{pinned_execution.id}") |
| 45 | + |
| 46 | + # Signal both workflows a few times to drive them |
| 47 | + advance_workflows(auto_upgrade_execution, pinned_execution) |
| 48 | + |
| 49 | + # Now wait for the v1.1 worker to appear and become current |
| 50 | + logger.info( |
| 51 | + 'Waiting for v1.1 worker to appear. Run `ruby worker_versioning/workerv1_1.rb` in another terminal' |
| 52 | + ) |
| 53 | + wait_for_worker_and_make_current(client, '1.1') |
| 54 | + |
| 55 | + # Once it has, we will continue to advance the workflows. |
| 56 | + # The auto-upgrade workflow will now make progress on the new worker, while the pinned one will |
| 57 | + # keep progressing on the old worker. |
| 58 | + advance_workflows(auto_upgrade_execution, pinned_execution) |
| 59 | + |
| 60 | + # Finally we'll start the v2 worker, and again it'll become the new current version |
| 61 | + logger.info( |
| 62 | + 'Waiting for v2 worker to appear. Run `ruby worker_versioning/workerv2.rb` in another terminal' |
| 63 | + ) |
| 64 | + wait_for_worker_and_make_current(client, '2.0') |
| 65 | + |
| 66 | + # Once it has we'll start one more new workflow, another pinned one, to demonstrate that new |
| 67 | + # pinned workflows start on the current version. |
| 68 | + pinned_workflow_2_id = "worker-versioning-versioning-pinned-2_#{SecureRandom.uuid}" |
| 69 | + pinned_execution_v2 = client.start_workflow( |
| 70 | + :Pinned, |
| 71 | + id: pinned_workflow_2_id, |
| 72 | + task_queue: WorkerVersioning::Constants::TASK_QUEUE |
| 73 | + ) |
| 74 | + logger.info("Started pinned workflow v2: #{pinned_execution_v2.id}") |
| 75 | + |
| 76 | + # Now we'll conclude all workflows. You should be able to see in your server UI that the pinned |
| 77 | + # workflow always stayed on 1.0, while the auto-upgrading workflow migrated. |
| 78 | + [auto_upgrade_execution, pinned_execution, pinned_execution_v2].each do |handle| |
| 79 | + handle.signal(:do_next_signal, 'conclude') |
| 80 | + handle.result |
| 81 | + end |
| 82 | + |
| 83 | + logger.info('All workflows completed') |
| 84 | +end |
| 85 | + |
| 86 | +def advance_workflows(auto_upgrade_execution, pinned_execution) |
| 87 | + # Signal both workflows a few times to drive them. |
| 88 | + 3.times do |
| 89 | + auto_upgrade_execution.signal(:do_next_signal, 'do-activity') |
| 90 | + pinned_execution.signal(:do_next_signal, 'some-signal') |
| 91 | + end |
| 92 | +end |
| 93 | + |
| 94 | +def wait_for_worker_and_make_current(client, build_id) |
| 95 | + target_version = Temporalio::WorkerDeploymentVersion.new( |
| 96 | + deployment_name: WorkerVersioning::Constants::DEPLOYMENT_NAME, |
| 97 | + build_id: build_id |
| 98 | + ) |
| 99 | + |
| 100 | + loop do |
| 101 | + describe_request = Temporalio::Api::WorkflowService::V1::DescribeWorkerDeploymentRequest.new( |
| 102 | + namespace: client.namespace, |
| 103 | + deployment_name: WorkerVersioning::Constants::DEPLOYMENT_NAME |
| 104 | + ) |
| 105 | + response = client.workflow_service.describe_worker_deployment(describe_request) |
| 106 | + |
| 107 | + found = response.worker_deployment_info.version_summaries.any? do |version_summary| |
| 108 | + version_summary.deployment_version.deployment_name == target_version.deployment_name && |
| 109 | + version_summary.deployment_version.build_id == target_version.build_id |
| 110 | + end |
| 111 | + |
| 112 | + break if found |
| 113 | + |
| 114 | + sleep(1) |
| 115 | + rescue Temporalio::Error::RPCError => e |
| 116 | + # If not-found, wait a second and try again |
| 117 | + raise unless e.code == Temporalio::Error::RPCError::Code::NOT_FOUND |
| 118 | + |
| 119 | + sleep(1) |
| 120 | + next |
| 121 | + end |
| 122 | + |
| 123 | + # Once the version is available, set it as current |
| 124 | + set_request = Temporalio::Api::WorkflowService::V1::SetWorkerDeploymentCurrentVersionRequest.new( |
| 125 | + namespace: client.namespace, |
| 126 | + deployment_name: WorkerVersioning::Constants::DEPLOYMENT_NAME, |
| 127 | + version: target_version.to_canonical_string |
| 128 | + ) |
| 129 | + client.workflow_service.set_worker_deployment_current_version(set_request) |
| 130 | +end |
| 131 | + |
| 132 | +main if __FILE__ == $PROGRAM_NAME |
0 commit comments