Module: Temporal

Defined in:
lib/temporal/thread_pool.rb,
lib/temporal.rb,
lib/temporal/json.rb,
lib/temporal/uuid.rb,
lib/temporal/types.rb,
lib/temporal/client.rb,
lib/temporal/errors.rb,
lib/temporal/logger.rb,
lib/temporal/worker.rb,
lib/temporal/metrics.rb,
lib/temporal/testing.rb,
lib/temporal/version.rb,
lib/temporal/activity.rb,
lib/temporal/metadata.rb,
lib/temporal/workflow.rb,
lib/temporal/saga/saga.rb,
lib/temporal/saga/result.rb,
lib/temporal/retry_policy.rb,
lib/temporal/saga/concern.rb,
lib/temporal/client/errors.rb,
lib/temporal/configuration.rb,
lib/temporal/error_handler.rb,
lib/temporal/metadata/base.rb,
lib/temporal/client/retryer.rb,
lib/temporal/concerns/typed.rb,
lib/temporal/activity/poller.rb,
lib/temporal/workflow/errors.rb,
lib/temporal/workflow/future.rb,
lib/temporal/workflow/poller.rb,
lib/temporal/activity/context.rb,
lib/temporal/middleware/chain.rb,
lib/temporal/middleware/entry.rb,
lib/temporal/workflow/command.rb,
lib/temporal/workflow/context.rb,
lib/temporal/workflow/history.rb,
lib/temporal/client/serializer.rb,
lib/temporal/concerns/payloads.rb,
lib/temporal/executable_lookup.rb,
lib/temporal/execution_options.rb,
lib/temporal/metadata/activity.rb,
lib/temporal/metadata/workflow.rb,
lib/temporal/workflow/executor.rb,
lib/temporal/client/grpc_client.rb,
lib/temporal/concerns/executable.rb,
lib/temporal/workflow/dispatcher.rb,
lib/temporal/activity/async_token.rb,
lib/temporal/metrics_adapters/log.rb,
lib/temporal/thread_local_context.rb,
lib/temporal/client/converter/base.rb,
lib/temporal/metrics_adapters/null.rb,
lib/temporal/client/serializer/base.rb,
lib/temporal/metadata/workflow_task.rb,
lib/temporal/workflow/history/event.rb,
lib/temporal/workflow/state_manager.rb,
lib/temporal/activity/task_processor.rb,
lib/temporal/testing/future_registry.rb,
lib/temporal/workflow/execution_info.rb,
lib/temporal/workflow/history/window.rb,
lib/temporal/workflow/task_processor.rb,
lib/gen/temporal/api/enums/v1/query_pb.rb,
lib/temporal/client/serializer/failure.rb,
lib/temporal/testing/temporal_override.rb,
lib/temporal/testing/workflow_override.rb,
lib/gen/temporal/api/enums/v1/common_pb.rb,
lib/temporal/client/converter/composite.rb,
lib/temporal/testing/workflow_execution.rb,
lib/gen/temporal/api/query/v1/message_pb.rb,
lib/temporal/testing/scheduled_workflows.rb,
lib/gen/temporal/api/common/v1/message_pb.rb,
lib/gen/temporal/api/enums/v1/workflow_pb.rb,
lib/gen/temporal/api/filter/v1/message_pb.rb,
lib/temporal/client/converter/payload/nil.rb,
lib/temporal/workflow/convenience_methods.rb,
lib/temporal/workflow/replay_aware_logger.rb,
lib/gen/temporal/api/command/v1/message_pb.rb,
lib/gen/temporal/api/enums/v1/namespace_pb.rb,
lib/gen/temporal/api/failure/v1/message_pb.rb,
lib/gen/temporal/api/history/v1/message_pb.rb,
lib/gen/temporal/api/version/v1/message_pb.rb,
lib/temporal/client/converter/payload/json.rb,
lib/temporal/client/serializer/start_timer.rb,
lib/temporal/workflow/history/event_target.rb,
lib/gen/temporal/api/enums/v1/event_type_pb.rb,
lib/gen/temporal/api/enums/v1/task_queue_pb.rb,
lib/gen/temporal/api/workflow/v1/message_pb.rb,
lib/temporal/client/converter/payload/bytes.rb,
lib/temporal/client/serializer/cancel_timer.rb,
lib/temporal/client/serializer/retry_policy.rb,
lib/temporal/testing/local_activity_context.rb,
lib/temporal/testing/local_workflow_context.rb,
lib/temporal/workflow/command_state_machine.rb,
lib/gen/temporal/api/namespace/v1/message_pb.rb,
lib/gen/temporal/api/taskqueue/v1/message_pb.rb,
lib/temporal/client/serializer/fail_workflow.rb,
lib/temporal/client/serializer/record_marker.rb,
lib/gen/temporal/api/enums/v1/command_type_pb.rb,
lib/gen/temporal/api/enums/v1/failed_cause_pb.rb,
lib/gen/temporal/api/replication/v1/message_pb.rb,
lib/temporal/client/serializer/continue_as_new.rb,
lib/gen/temporal/api/errordetails/v1/message_pb.rb,
lib/temporal/client/serializer/complete_workflow.rb,
lib/temporal/client/serializer/schedule_activity.rb,
lib/gen/temporal/api/workflowservice/v1/service_pb.rb,
lib/temporal/activity/workflow_convenience_methods.rb,
lib/temporal/client/serializer/start_child_workflow.rb,
lib/gen/temporal/api/workflowservice/v1/request_response_pb.rb,
lib/gen/temporal/api/workflowservice/v1/service_services_pb.rb,
lib/temporal/client/serializer/request_activity_cancellation.rb

Overview

Provides context for Temporal::Activity::WorkflowConvenienceMethods

Defined Under Namespace

Modules: Api, Client, Concerns, ErrorHandler, JSON, Metadata, MetricsAdapters, Middleware, Saga, Testing, ThreadLocalContext, Types, UUID Classes: Activity, ActivityException, ActivityNotRegistered, ApiError, CancellationAlreadyRequestedFailure, ClientError, ClientVersionNotSupportedFailure, Configuration, Error, ExecutableLookup, ExecutionOptions, FeatureVersionNotSupportedFailure, InternalError, Logger, Metrics, NamespaceAlreadyExistsFailure, NamespaceNotActiveFailure, NonDeterministicWorkflowError, NotFoundFailure, QueryFailedFailure, RetryPolicy, ThreadPool, TimeoutError, Worker, Workflow, WorkflowCanceled, WorkflowError, WorkflowExecutionAlreadyStartedFailure, WorkflowNotRegistered, WorkflowRunContinuedAsNew, WorkflowRunError, WorkflowTerminated, WorkflowTimedOut

Constant Summary collapse

VERSION =
'0.0.1'.freeze

Class Method Summary collapse

Class Method Details

.await_workflow_result(workflow, workflow_id:, run_id: nil, timeout: nil, namespace: nil) ⇒ Object

Long polls for a workflow to be completed and returns whatever the execute function returned. This function times out after 30 seconds and throws Temporal::TimeoutError, not to be confused with Temporal::WorkflowTimedOut which reports that the workflow itself timed out. run_id of nil: await the entire workflow completion. This can span multiple runs in the case where the workflow uses continue-as-new. timeout: seconds to wait for the result. This cannot be longer than 30 seconds because that is the maximum the server supports. namespace: if nil, choose the one declared on the Workflow, or the global default



96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/temporal.rb', line 96

def await_workflow_result(workflow, workflow_id:, run_id: nil, timeout: nil, namespace: nil)
  options = namespace ? {namespace: namespace} : {}
  execution_options = ExecutionOptions.new(workflow, options)
  max_timeout = Temporal::Client::GRPCClient::SERVER_MAX_GET_WORKFLOW_EXECUTION_HISTORY_POLL
  history_response = nil
  begin
    history_response = client.get_workflow_execution_history(
      namespace: execution_options.namespace,
      workflow_id: workflow_id,
      run_id: run_id,
      wait_for_new_event: true,
      event_type: :close,
      timeout: timeout || max_timeout,
    )
  rescue GRPC::DeadlineExceeded => e
    message = if timeout 
      "Timed out after your specified limit of timeout: #{timeout} seconds"
    else
      "Timed out after #{max_timeout} seconds, which is the maximum supported amount."
    end
    raise TimeoutError.new(message)
  end
  history = Workflow::History.new(history_response.history.events)
  closed_event = history.events.first
  case closed_event.type
  when 'WORKFLOW_EXECUTION_COMPLETED'
    payloads = closed_event.attributes.result
    return ResultConverter.from_result_payloads(payloads)
  when 'WORKFLOW_EXECUTION_TIMED_OUT'
    raise Temporal::WorkflowTimedOut
  when 'WORKFLOW_EXECUTION_TERMINATED'
    raise Temporal::WorkflowTerminated
  when 'WORKFLOW_EXECUTION_CANCELED'
    raise Temporal::WorkflowCanceled
  when 'WORKFLOW_EXECUTION_FAILED'
    raise Temporal::Workflow::Errors.generate_error(closed_event.attributes.failure)
  when 'WORKFLOW_EXECUTION_CONTINUED_AS_NEW'
    new_run_id = closed_event.attributes.new_execution_run_id
    # Throw to let the caller know they're not getting the result
    # they wanted.  They can re-call with the new run_id to poll.
    raise Temporal::WorkflowRunContinuedAsNew.new(new_run_id: new_run_id)
  else
    raise NotImplementedError, "Unexpected event type #{closed_event.type}."
  end
end

.complete_activity(async_token, result = nil) ⇒ Object



179
180
181
182
183
184
185
186
187
188
189
# File 'lib/temporal.rb', line 179

def complete_activity(async_token, result = nil)
  details = Activity::AsyncToken.decode(async_token)

  client.respond_activity_task_completed_by_id(
    namespace: details.namespace,
    activity_id: details.activity_id,
    workflow_id: details.workflow_id,
    run_id: details.run_id,
    result: result
  )
end

.configurationObject



207
208
209
# File 'lib/temporal.rb', line 207

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



203
204
205
# File 'lib/temporal.rb', line 203

def configure(&block)
  yield configuration
end

.fail_activity(async_token, exception) ⇒ Object



191
192
193
194
195
196
197
198
199
200
201
# File 'lib/temporal.rb', line 191

def fail_activity(async_token, exception)
  details = Activity::AsyncToken.decode(async_token)

  client.respond_activity_task_failed_by_id(
    namespace: details.namespace,
    activity_id: details.activity_id,
    workflow_id: details.workflow_id,
    run_id: details.run_id,
    exception: exception
  )
end

.fetch_workflow_execution_info(namespace, workflow_id, run_id) ⇒ Object



169
170
171
172
173
174
175
176
177
# File 'lib/temporal.rb', line 169

def fetch_workflow_execution_info(namespace, workflow_id, run_id)
  response = client.describe_workflow_execution(
    namespace: namespace,
    workflow_id: workflow_id,
    run_id: run_id
  )

  Workflow::ExecutionInfo.generate_from(response.workflow_execution_info)
end

.loggerObject



211
212
213
# File 'lib/temporal.rb', line 211

def logger
  configuration.logger
end

.metricsObject



215
216
217
# File 'lib/temporal.rb', line 215

def metrics
  @metrics ||= Metrics.new(configuration.metrics_adapter)
end

.register_namespace(name, description = nil) ⇒ Object



71
72
73
# File 'lib/temporal.rb', line 71

def register_namespace(name, description = nil)
  client.register_namespace(name: name, description: description)
end

.reset_workflow(namespace, workflow_id, run_id, workflow_task_id: nil, reason: 'manual reset') ⇒ Object

Raises:



142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/temporal.rb', line 142

def reset_workflow(namespace, workflow_id, run_id, workflow_task_id: nil, reason: 'manual reset')
  workflow_task_id ||= get_last_completed_workflow_task_id(namespace, workflow_id, run_id)
  raise Error, 'Could not find a completed workflow task event' unless workflow_task_id

  response = client.reset_workflow_execution(
    namespace: namespace,
    workflow_id: workflow_id,
    run_id: run_id,
    reason: reason,
    workflow_task_event_id: workflow_task_id
  )

  response.run_id
end

.schedule_workflow(workflow, cron_schedule, *input, **args) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/temporal.rb', line 44

def schedule_workflow(workflow, cron_schedule, *input, **args)
  options = args.delete(:options) || {}
  input << args unless args.empty?

  execution_options = ExecutionOptions.new(workflow, options)
  workflow_id = options[:workflow_id] || SecureRandom.uuid

  response = client.start_workflow_execution(
    namespace: execution_options.namespace,
    workflow_id: workflow_id,
    workflow_name: execution_options.name,
    task_queue: execution_options.task_queue,
    input: input,
    execution_timeout: execution_options.timeouts[:execution],
    # Execution timeout is across all scheduled jobs, whereas run is for an individual run.
    # This default is here for backward compatibility.  Certainly, the run timeout shouldn't be higher
    # than the execution timeout.
    run_timeout: execution_options.timeouts[:run] || execution_options.timeouts[:execution],
    task_timeout: execution_options.timeouts[:task],
    workflow_id_reuse_policy: options[:workflow_id_reuse_policy],
    headers: execution_options.headers,
    cron_schedule: cron_schedule
  )

  response.run_id
end

.signal_workflow(workflow, signal, workflow_id, run_id, input = nil) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
# File 'lib/temporal.rb', line 75

def signal_workflow(workflow, signal, workflow_id, run_id, input = nil)
  execution_options = ExecutionOptions.new(workflow)

  client.signal_workflow_execution(
    namespace: execution_options.namespace, # TODO: allow passing namespace instead
    workflow_id: workflow_id,
    run_id: run_id,
    signal: signal,
    input: input
  )
end

.start_workflow(workflow, *input, **args) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/temporal.rb', line 20

def start_workflow(workflow, *input, **args)
  options = args.delete(:options) || {}
  input << args unless args.empty?

  execution_options = ExecutionOptions.new(workflow, options)
  workflow_id = options[:workflow_id] || SecureRandom.uuid

  response = client.start_workflow_execution(
    namespace: execution_options.namespace,
    workflow_id: workflow_id,
    workflow_name: execution_options.name,
    task_queue: execution_options.task_queue,
    input: input,
    execution_timeout: execution_options.timeouts[:execution],
    # If unspecified, individual runs should have the full time for the execution (which includes retries).
    run_timeout: execution_options.timeouts[:run] || execution_options.timeouts[:execution],
    task_timeout: execution_options.timeouts[:task],
    workflow_id_reuse_policy: options[:workflow_id_reuse_policy],
    headers: execution_options.headers
  )

  response.run_id
end

.terminate_workflow(workflow_id, namespace: nil, run_id: nil, reason: nil, details: nil) ⇒ Object



157
158
159
160
161
162
163
164
165
166
167
# File 'lib/temporal.rb', line 157

def terminate_workflow(workflow_id, namespace: nil, run_id: nil, reason: nil, details: nil)
  namespace ||= Temporal.configuration.namespace

  client.terminate_workflow_execution(
    namespace: namespace,
    workflow_id: workflow_id,
    run_id: run_id,
    reason: reason,
    details: details
  )
end