|
| 1 | +# frozen_string_literal: true |
| 2 | + |
| 3 | +require_relative 'normal_activities' |
| 4 | +require_relative 'worker_specific_activities' |
| 5 | +require 'temporalio/workflow' |
| 6 | + |
| 7 | +module WorkerSpecificTaskQueues |
| 8 | + class FileProcessingWorkflow < Temporalio::Workflow::Definition |
| 9 | + def execute(max_attempts) |
| 10 | + attempt = 0 |
| 11 | + |
| 12 | + loop do |
| 13 | + attempt += 1 |
| 14 | + begin |
| 15 | + process_file |
| 16 | + return |
| 17 | + rescue StandardError => e |
| 18 | + # If it's at max attempts, re-raise to fail the workflow |
| 19 | + if attempt >= max_attempts |
| 20 | + Temporalio::Workflow.logger.error( |
| 21 | + "File processing failed and reached #{attempt} attempts, failing workflow: #{e.message}" |
| 22 | + ) |
| 23 | + raise |
| 24 | + end |
| 25 | + # Otherwise, just warn and continue |
| 26 | + Temporalio::Workflow.logger.warn( |
| 27 | + "File processing failed on attempt #{attempt}, trying again: #{e.message}" |
| 28 | + ) |
| 29 | + end |
| 30 | + end |
| 31 | + end |
| 32 | + |
| 33 | + private |
| 34 | + |
| 35 | + def process_file |
| 36 | + # Get a unique task queue from any worker |
| 37 | + unique_worker_task_queue = Temporalio::Workflow.execute_activity( |
| 38 | + NormalActivities::GetUniqueTaskQueueActivity, |
| 39 | + start_to_close_timeout: 60 |
| 40 | + ) |
| 41 | + |
| 42 | + # Download the file on the specific worker |
| 43 | + download_path = Temporalio::Workflow.execute_activity( |
| 44 | + WorkerSpecificActivities::DownloadFileActivity, |
| 45 | + 'https://temporal.io', |
| 46 | + task_queue: unique_worker_task_queue, |
| 47 | + schedule_to_close_timeout: 300, |
| 48 | + heartbeat_timeout: 60 |
| 49 | + ) |
| 50 | + |
| 51 | + # Process the file on the same worker |
| 52 | + Temporalio::Workflow.execute_activity( |
| 53 | + WorkerSpecificActivities::WorkOnFileActivity, |
| 54 | + download_path, |
| 55 | + task_queue: unique_worker_task_queue, |
| 56 | + schedule_to_close_timeout: 300, |
| 57 | + heartbeat_timeout: 60 |
| 58 | + ) |
| 59 | + |
| 60 | + # Clean up the file on the same worker |
| 61 | + Temporalio::Workflow.execute_activity( |
| 62 | + WorkerSpecificActivities::CleanupFileActivity, |
| 63 | + download_path, |
| 64 | + task_queue: unique_worker_task_queue, |
| 65 | + schedule_to_close_timeout: 300, |
| 66 | + heartbeat_timeout: 60 |
| 67 | + ) |
| 68 | + end |
| 69 | + end |
| 70 | +end |
0 commit comments