temporalio 0.4.0-aarch64-linux-musl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (183) hide show
  1. checksums.yaml +7 -0
  2. data/.yardopts +2 -0
  3. data/Gemfile +27 -0
  4. data/Rakefile +101 -0
  5. data/lib/temporalio/activity/complete_async_error.rb +11 -0
  6. data/lib/temporalio/activity/context.rb +123 -0
  7. data/lib/temporalio/activity/definition.rb +192 -0
  8. data/lib/temporalio/activity/info.rb +67 -0
  9. data/lib/temporalio/activity.rb +12 -0
  10. data/lib/temporalio/api/activity/v1/message.rb +25 -0
  11. data/lib/temporalio/api/batch/v1/message.rb +36 -0
  12. data/lib/temporalio/api/cloud/account/v1/message.rb +28 -0
  13. data/lib/temporalio/api/cloud/cloudservice/v1/request_response.rb +126 -0
  14. data/lib/temporalio/api/cloud/cloudservice/v1/service.rb +25 -0
  15. data/lib/temporalio/api/cloud/cloudservice.rb +3 -0
  16. data/lib/temporalio/api/cloud/identity/v1/message.rb +41 -0
  17. data/lib/temporalio/api/cloud/namespace/v1/message.rb +42 -0
  18. data/lib/temporalio/api/cloud/nexus/v1/message.rb +31 -0
  19. data/lib/temporalio/api/cloud/operation/v1/message.rb +28 -0
  20. data/lib/temporalio/api/cloud/region/v1/message.rb +24 -0
  21. data/lib/temporalio/api/cloud/resource/v1/message.rb +23 -0
  22. data/lib/temporalio/api/cloud/sink/v1/message.rb +24 -0
  23. data/lib/temporalio/api/cloud/usage/v1/message.rb +31 -0
  24. data/lib/temporalio/api/command/v1/message.rb +46 -0
  25. data/lib/temporalio/api/common/v1/grpc_status.rb +23 -0
  26. data/lib/temporalio/api/common/v1/message.rb +48 -0
  27. data/lib/temporalio/api/deployment/v1/message.rb +38 -0
  28. data/lib/temporalio/api/enums/v1/batch_operation.rb +22 -0
  29. data/lib/temporalio/api/enums/v1/command_type.rb +21 -0
  30. data/lib/temporalio/api/enums/v1/common.rb +26 -0
  31. data/lib/temporalio/api/enums/v1/deployment.rb +23 -0
  32. data/lib/temporalio/api/enums/v1/event_type.rb +21 -0
  33. data/lib/temporalio/api/enums/v1/failed_cause.rb +26 -0
  34. data/lib/temporalio/api/enums/v1/namespace.rb +23 -0
  35. data/lib/temporalio/api/enums/v1/nexus.rb +21 -0
  36. data/lib/temporalio/api/enums/v1/query.rb +22 -0
  37. data/lib/temporalio/api/enums/v1/reset.rb +23 -0
  38. data/lib/temporalio/api/enums/v1/schedule.rb +21 -0
  39. data/lib/temporalio/api/enums/v1/task_queue.rb +25 -0
  40. data/lib/temporalio/api/enums/v1/update.rb +22 -0
  41. data/lib/temporalio/api/enums/v1/workflow.rb +31 -0
  42. data/lib/temporalio/api/errordetails/v1/message.rb +44 -0
  43. data/lib/temporalio/api/export/v1/message.rb +24 -0
  44. data/lib/temporalio/api/failure/v1/message.rb +37 -0
  45. data/lib/temporalio/api/filter/v1/message.rb +27 -0
  46. data/lib/temporalio/api/history/v1/message.rb +92 -0
  47. data/lib/temporalio/api/namespace/v1/message.rb +31 -0
  48. data/lib/temporalio/api/nexus/v1/message.rb +41 -0
  49. data/lib/temporalio/api/operatorservice/v1/request_response.rb +49 -0
  50. data/lib/temporalio/api/operatorservice/v1/service.rb +23 -0
  51. data/lib/temporalio/api/operatorservice.rb +3 -0
  52. data/lib/temporalio/api/payload_visitor.rb +1581 -0
  53. data/lib/temporalio/api/protocol/v1/message.rb +23 -0
  54. data/lib/temporalio/api/query/v1/message.rb +28 -0
  55. data/lib/temporalio/api/replication/v1/message.rb +26 -0
  56. data/lib/temporalio/api/schedule/v1/message.rb +43 -0
  57. data/lib/temporalio/api/sdk/v1/enhanced_stack_trace.rb +25 -0
  58. data/lib/temporalio/api/sdk/v1/task_complete_metadata.rb +21 -0
  59. data/lib/temporalio/api/sdk/v1/user_metadata.rb +23 -0
  60. data/lib/temporalio/api/sdk/v1/workflow_metadata.rb +23 -0
  61. data/lib/temporalio/api/taskqueue/v1/message.rb +48 -0
  62. data/lib/temporalio/api/testservice/v1/request_response.rb +31 -0
  63. data/lib/temporalio/api/testservice/v1/service.rb +23 -0
  64. data/lib/temporalio/api/update/v1/message.rb +33 -0
  65. data/lib/temporalio/api/version/v1/message.rb +26 -0
  66. data/lib/temporalio/api/workflow/v1/message.rb +51 -0
  67. data/lib/temporalio/api/workflowservice/v1/request_response.rb +233 -0
  68. data/lib/temporalio/api/workflowservice/v1/service.rb +23 -0
  69. data/lib/temporalio/api/workflowservice.rb +3 -0
  70. data/lib/temporalio/api.rb +15 -0
  71. data/lib/temporalio/cancellation.rb +170 -0
  72. data/lib/temporalio/client/activity_id_reference.rb +32 -0
  73. data/lib/temporalio/client/async_activity_handle.rb +85 -0
  74. data/lib/temporalio/client/connection/cloud_service.rb +726 -0
  75. data/lib/temporalio/client/connection/operator_service.rb +201 -0
  76. data/lib/temporalio/client/connection/service.rb +42 -0
  77. data/lib/temporalio/client/connection/test_service.rb +111 -0
  78. data/lib/temporalio/client/connection/workflow_service.rb +1251 -0
  79. data/lib/temporalio/client/connection.rb +316 -0
  80. data/lib/temporalio/client/interceptor.rb +455 -0
  81. data/lib/temporalio/client/schedule.rb +991 -0
  82. data/lib/temporalio/client/schedule_handle.rb +126 -0
  83. data/lib/temporalio/client/with_start_workflow_operation.rb +115 -0
  84. data/lib/temporalio/client/workflow_execution.rb +119 -0
  85. data/lib/temporalio/client/workflow_execution_count.rb +36 -0
  86. data/lib/temporalio/client/workflow_execution_status.rb +18 -0
  87. data/lib/temporalio/client/workflow_handle.rb +389 -0
  88. data/lib/temporalio/client/workflow_query_reject_condition.rb +14 -0
  89. data/lib/temporalio/client/workflow_update_handle.rb +65 -0
  90. data/lib/temporalio/client/workflow_update_wait_stage.rb +17 -0
  91. data/lib/temporalio/client.rb +607 -0
  92. data/lib/temporalio/common_enums.rb +41 -0
  93. data/lib/temporalio/contrib/open_telemetry.rb +470 -0
  94. data/lib/temporalio/converters/data_converter.rb +99 -0
  95. data/lib/temporalio/converters/failure_converter.rb +202 -0
  96. data/lib/temporalio/converters/payload_codec.rb +26 -0
  97. data/lib/temporalio/converters/payload_converter/binary_null.rb +34 -0
  98. data/lib/temporalio/converters/payload_converter/binary_plain.rb +35 -0
  99. data/lib/temporalio/converters/payload_converter/binary_protobuf.rb +42 -0
  100. data/lib/temporalio/converters/payload_converter/composite.rb +66 -0
  101. data/lib/temporalio/converters/payload_converter/encoding.rb +35 -0
  102. data/lib/temporalio/converters/payload_converter/json_plain.rb +44 -0
  103. data/lib/temporalio/converters/payload_converter/json_protobuf.rb +41 -0
  104. data/lib/temporalio/converters/payload_converter.rb +71 -0
  105. data/lib/temporalio/converters/raw_value.rb +20 -0
  106. data/lib/temporalio/converters.rb +9 -0
  107. data/lib/temporalio/error/failure.rb +219 -0
  108. data/lib/temporalio/error.rb +156 -0
  109. data/lib/temporalio/internal/bridge/3.2/temporalio_bridge.so +0 -0
  110. data/lib/temporalio/internal/bridge/3.3/temporalio_bridge.so +0 -0
  111. data/lib/temporalio/internal/bridge/3.4/temporalio_bridge.so +0 -0
  112. data/lib/temporalio/internal/bridge/api/activity_result/activity_result.rb +34 -0
  113. data/lib/temporalio/internal/bridge/api/activity_task/activity_task.rb +31 -0
  114. data/lib/temporalio/internal/bridge/api/child_workflow/child_workflow.rb +33 -0
  115. data/lib/temporalio/internal/bridge/api/common/common.rb +27 -0
  116. data/lib/temporalio/internal/bridge/api/core_interface.rb +40 -0
  117. data/lib/temporalio/internal/bridge/api/external_data/external_data.rb +27 -0
  118. data/lib/temporalio/internal/bridge/api/nexus/nexus.rb +33 -0
  119. data/lib/temporalio/internal/bridge/api/workflow_activation/workflow_activation.rb +56 -0
  120. data/lib/temporalio/internal/bridge/api/workflow_commands/workflow_commands.rb +57 -0
  121. data/lib/temporalio/internal/bridge/api/workflow_completion/workflow_completion.rb +31 -0
  122. data/lib/temporalio/internal/bridge/api.rb +3 -0
  123. data/lib/temporalio/internal/bridge/client.rb +95 -0
  124. data/lib/temporalio/internal/bridge/runtime.rb +56 -0
  125. data/lib/temporalio/internal/bridge/testing.rb +69 -0
  126. data/lib/temporalio/internal/bridge/worker.rb +85 -0
  127. data/lib/temporalio/internal/bridge.rb +36 -0
  128. data/lib/temporalio/internal/client/implementation.rb +922 -0
  129. data/lib/temporalio/internal/metric.rb +122 -0
  130. data/lib/temporalio/internal/proto_utils.rb +165 -0
  131. data/lib/temporalio/internal/worker/activity_worker.rb +385 -0
  132. data/lib/temporalio/internal/worker/multi_runner.rb +213 -0
  133. data/lib/temporalio/internal/worker/workflow_instance/child_workflow_handle.rb +54 -0
  134. data/lib/temporalio/internal/worker/workflow_instance/context.rb +383 -0
  135. data/lib/temporalio/internal/worker/workflow_instance/details.rb +46 -0
  136. data/lib/temporalio/internal/worker/workflow_instance/external_workflow_handle.rb +32 -0
  137. data/lib/temporalio/internal/worker/workflow_instance/externally_immutable_hash.rb +22 -0
  138. data/lib/temporalio/internal/worker/workflow_instance/handler_execution.rb +25 -0
  139. data/lib/temporalio/internal/worker/workflow_instance/handler_hash.rb +41 -0
  140. data/lib/temporalio/internal/worker/workflow_instance/illegal_call_tracer.rb +97 -0
  141. data/lib/temporalio/internal/worker/workflow_instance/inbound_implementation.rb +62 -0
  142. data/lib/temporalio/internal/worker/workflow_instance/outbound_implementation.rb +400 -0
  143. data/lib/temporalio/internal/worker/workflow_instance/replay_safe_logger.rb +37 -0
  144. data/lib/temporalio/internal/worker/workflow_instance/replay_safe_metric.rb +40 -0
  145. data/lib/temporalio/internal/worker/workflow_instance/scheduler.rb +183 -0
  146. data/lib/temporalio/internal/worker/workflow_instance.rb +774 -0
  147. data/lib/temporalio/internal/worker/workflow_worker.rb +239 -0
  148. data/lib/temporalio/internal.rb +7 -0
  149. data/lib/temporalio/metric.rb +109 -0
  150. data/lib/temporalio/retry_policy.rb +74 -0
  151. data/lib/temporalio/runtime/metric_buffer.rb +94 -0
  152. data/lib/temporalio/runtime.rb +352 -0
  153. data/lib/temporalio/scoped_logger.rb +96 -0
  154. data/lib/temporalio/search_attributes.rb +356 -0
  155. data/lib/temporalio/testing/activity_environment.rb +160 -0
  156. data/lib/temporalio/testing/workflow_environment.rb +406 -0
  157. data/lib/temporalio/testing.rb +10 -0
  158. data/lib/temporalio/version.rb +5 -0
  159. data/lib/temporalio/worker/activity_executor/fiber.rb +49 -0
  160. data/lib/temporalio/worker/activity_executor/thread_pool.rb +46 -0
  161. data/lib/temporalio/worker/activity_executor.rb +55 -0
  162. data/lib/temporalio/worker/interceptor.rb +365 -0
  163. data/lib/temporalio/worker/thread_pool.rb +237 -0
  164. data/lib/temporalio/worker/tuner.rb +189 -0
  165. data/lib/temporalio/worker/workflow_executor/thread_pool.rb +235 -0
  166. data/lib/temporalio/worker/workflow_executor.rb +26 -0
  167. data/lib/temporalio/worker/workflow_replayer.rb +350 -0
  168. data/lib/temporalio/worker.rb +603 -0
  169. data/lib/temporalio/workflow/activity_cancellation_type.rb +20 -0
  170. data/lib/temporalio/workflow/child_workflow_cancellation_type.rb +21 -0
  171. data/lib/temporalio/workflow/child_workflow_handle.rb +43 -0
  172. data/lib/temporalio/workflow/definition.rb +598 -0
  173. data/lib/temporalio/workflow/external_workflow_handle.rb +41 -0
  174. data/lib/temporalio/workflow/future.rb +151 -0
  175. data/lib/temporalio/workflow/handler_unfinished_policy.rb +13 -0
  176. data/lib/temporalio/workflow/info.rb +104 -0
  177. data/lib/temporalio/workflow/parent_close_policy.rb +19 -0
  178. data/lib/temporalio/workflow/update_info.rb +20 -0
  179. data/lib/temporalio/workflow.rb +575 -0
  180. data/lib/temporalio/workflow_history.rb +47 -0
  181. data/lib/temporalio.rb +11 -0
  182. data/temporalio.gemspec +29 -0
  183. metadata +258 -0
@@ -0,0 +1,603 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'temporalio/activity'
4
+ require 'temporalio/cancellation'
5
+ require 'temporalio/client'
6
+ require 'temporalio/error'
7
+ require 'temporalio/internal/bridge'
8
+ require 'temporalio/internal/bridge/worker'
9
+ require 'temporalio/internal/proto_utils'
10
+ require 'temporalio/internal/worker/activity_worker'
11
+ require 'temporalio/internal/worker/multi_runner'
12
+ require 'temporalio/internal/worker/workflow_instance'
13
+ require 'temporalio/internal/worker/workflow_worker'
14
+ require 'temporalio/worker/activity_executor'
15
+ require 'temporalio/worker/interceptor'
16
+ require 'temporalio/worker/thread_pool'
17
+ require 'temporalio/worker/tuner'
18
+ require 'temporalio/worker/workflow_executor'
19
+
20
+ module Temporalio
21
+ # Worker for processing activities and workflows on a task queue.
22
+ #
23
+ # Workers are created for a task queue and the items they can run. Then {run} is used for running a single worker, or
24
+ # {run_all} is used for a collection of workers. These can wait until a block is complete or a {Cancellation} is
25
+ # canceled.
26
+ class Worker
27
+ Options = Data.define(
28
+ :client,
29
+ :task_queue,
30
+ :activities,
31
+ :workflows,
32
+ :tuner,
33
+ :activity_executors,
34
+ :workflow_executor,
35
+ :interceptors,
36
+ :build_id,
37
+ :identity,
38
+ :logger,
39
+ :max_cached_workflows,
40
+ :max_concurrent_workflow_task_polls,
41
+ :nonsticky_to_sticky_poll_ratio,
42
+ :max_concurrent_activity_task_polls,
43
+ :no_remote_activities,
44
+ :sticky_queue_schedule_to_start_timeout,
45
+ :max_heartbeat_throttle_interval,
46
+ :default_heartbeat_throttle_interval,
47
+ :max_activities_per_second,
48
+ :max_task_queue_activities_per_second,
49
+ :graceful_shutdown_period,
50
+ :use_worker_versioning,
51
+ :disable_eager_activity_execution,
52
+ :illegal_workflow_calls,
53
+ :workflow_failure_exception_types,
54
+ :workflow_payload_codec_thread_pool,
55
+ :unsafe_workflow_io_enabled,
56
+ :debug_mode
57
+ )
58
+
59
+ # Options as returned from {options} for `**to_h` splat use in {initialize}. See {initialize} for details.
60
+ #
61
+ # Note, the `client` within can be replaced via client setter.
62
+ class Options; end # rubocop:disable Lint/EmptyClass
63
+
64
+ # @return [String] Memoized default build ID. This default value is built as a checksum of all of the loaded Ruby
65
+ # source files in `$LOADED_FEATURES`. Users may prefer to set the build ID to a better representation of the
66
+ # source.
67
+ def self.default_build_id
68
+ @default_build_id ||= _load_default_build_id
69
+ end
70
+
71
+ # @!visibility private
72
+ def self._load_default_build_id
73
+ # The goal is to get a hash of runtime code, both Temporal's and the
74
+ # user's. After all options were explored, we have decided to default to
75
+ # hashing all bytecode of required files. This means later/dynamic require
76
+ # won't be accounted for because this is memoized. It also means the
77
+ # tiniest code change will affect this, which is what we want since this
78
+ # is meant to be a "binary checksum". We have chosen to use MD5 for speed,
79
+ # similarity with other SDKs, and because security is not a factor.
80
+ require 'digest'
81
+
82
+ saw_bridge = false
83
+ build_id = $LOADED_FEATURES.each_with_object(Digest::MD5.new) do |file, digest|
84
+ saw_bridge = true if file.include?('temporalio_bridge.')
85
+ digest.update(File.read(file)) if File.file?(file)
86
+ end.hexdigest
87
+ raise 'Temporal bridge library not in $LOADED_FEATURES, unable to calculate default build ID' unless saw_bridge
88
+
89
+ build_id
90
+ end
91
+
92
+ # Run all workers until cancellation or optional block completes. When the cancellation or block is complete, the
93
+ # workers are shut down. This will return the block result if everything successful or raise an error if not. See
94
+ # {run} for details on how worker shutdown works.
95
+ #
96
+ # @param workers [Array<Worker>] Workers to run.
97
+ # @param cancellation [Cancellation] Cancellation that can be canceled to shut down all workers.
98
+ # @param shutdown_signals [Array] Signals to trap and cause worker shutdown.
99
+ # @param raise_in_block_on_shutdown [Exception, nil] Exception to {::Thread.raise} or {::Fiber.raise} if a block is
100
+ # present and still running on shutdown. If nil, `raise` is not used.
101
+ # @param wait_block_complete [Boolean] If block given and shutdown caused by something else (e.g. cancellation
102
+ # canceled), whether to wait on the block to complete before returning.
103
+ # @yield Optional block. This will be run in a new background thread or fiber. Workers will shut down upon
104
+ # completion of this and, assuming no other failures, return/bubble success/exception of the block.
105
+ # @return [Object] Return value of the block or nil of no block given.
106
+ def self.run_all(
107
+ *workers,
108
+ cancellation: Cancellation.new,
109
+ shutdown_signals: [],
110
+ raise_in_block_on_shutdown: Error::CanceledError.new('Workers finished'),
111
+ wait_block_complete: true,
112
+ &block
113
+ )
114
+ # Confirm there is at least one and they are all workers
115
+ raise ArgumentError, 'At least one worker required' if workers.empty?
116
+ raise ArgumentError, 'Not all parameters are workers' unless workers.all? { |w| w.is_a?(Worker) }
117
+
118
+ Internal::Bridge.assert_fiber_compatibility!
119
+
120
+ # Start the multi runner
121
+ runner = Internal::Worker::MultiRunner.new(workers:, shutdown_signals:)
122
+
123
+ # Apply block
124
+ runner.apply_thread_or_fiber_block(&block)
125
+
126
+ # Reuse first worker logger
127
+ logger = workers.first&.options&.logger or raise # Never nil
128
+
129
+ # On cancel, initiate shutdown
130
+ cancellation.add_cancel_callback do
131
+ logger.info('Cancel invoked, beginning worker shutdown')
132
+ runner.initiate_shutdown
133
+ end
134
+
135
+ # Poller loop, run until all pollers shut down
136
+ first_error = nil
137
+ block_result = nil
138
+ loop do
139
+ event = runner.next_event
140
+ # TODO(cretz): Consider improving performance instead of this case statement
141
+ case event
142
+ when Internal::Worker::MultiRunner::Event::PollSuccess
143
+ # Successful poll
144
+ event.worker #: Worker
145
+ ._on_poll_bytes(runner, event.worker_type, event.bytes)
146
+ when Internal::Worker::MultiRunner::Event::PollFailure
147
+ # Poll failure, this causes shutdown of all workers
148
+ logger.error('Poll failure (beginning worker shutdown if not already occurring)')
149
+ logger.error(event.error)
150
+ first_error ||= event.error
151
+ runner.initiate_shutdown
152
+ when Internal::Worker::MultiRunner::Event::WorkflowActivationDecoded
153
+ # Came back from a codec as decoded
154
+ event.workflow_worker.handle_activation(runner:, activation: event.activation, decoded: true)
155
+ when Internal::Worker::MultiRunner::Event::WorkflowActivationComplete
156
+ # An activation is complete
157
+ event.workflow_worker.handle_activation_complete(
158
+ runner:,
159
+ activation_completion: event.activation_completion,
160
+ encoded: event.encoded,
161
+ completion_complete_queue: event.completion_complete_queue
162
+ )
163
+ when Internal::Worker::MultiRunner::Event::WorkflowActivationCompletionComplete
164
+ # Completion complete, only need to log error if it occurs here
165
+ if event.error
166
+ logger.error("Activation completion failed to record on run ID #{event.run_id}")
167
+ logger.error(event.error)
168
+ end
169
+ when Internal::Worker::MultiRunner::Event::PollerShutDown
170
+ # Individual poller shut down. Nothing to do here until we support
171
+ # worker status or something.
172
+ when Internal::Worker::MultiRunner::Event::AllPollersShutDown
173
+ # This is where we break the loop, no more polling can happen
174
+ break
175
+ when Internal::Worker::MultiRunner::Event::BlockSuccess
176
+ logger.info('Block completed, beginning worker shutdown')
177
+ block_result = event
178
+ runner.initiate_shutdown
179
+ when Internal::Worker::MultiRunner::Event::BlockFailure
180
+ logger.error('Block failure (beginning worker shutdown)')
181
+ logger.error(event.error)
182
+ block_result = event
183
+ first_error ||= event.error
184
+ runner.initiate_shutdown
185
+ when Internal::Worker::MultiRunner::Event::ShutdownSignalReceived
186
+ logger.info('Signal received, beginning worker shutdown')
187
+ runner.initiate_shutdown
188
+ else
189
+ raise "Unexpected event: #{event}"
190
+ end
191
+ end
192
+
193
+ # Now that all pollers have stopped, let's wait for all to complete
194
+ begin
195
+ runner.wait_complete_and_finalize_shutdown
196
+ rescue StandardError => e
197
+ logger.warn('Failed waiting and finalizing')
198
+ logger.warn(e)
199
+ end
200
+
201
+ # If there was a block but not a result yet, we want to raise if that is
202
+ # wanted, and wait if that is wanted
203
+ if block_given? && block_result.nil?
204
+ runner.raise_in_thread_or_fiber_block(raise_in_block_on_shutdown) unless raise_in_block_on_shutdown.nil?
205
+ if wait_block_complete
206
+ event = runner.next_event
207
+ case event
208
+ when Internal::Worker::MultiRunner::Event::BlockSuccess
209
+ logger.info('Block completed (after worker shutdown)')
210
+ block_result = event
211
+ when Internal::Worker::MultiRunner::Event::BlockFailure
212
+ logger.error('Block failure (after worker shutdown)')
213
+ logger.error(event.error)
214
+ block_result = event
215
+ first_error ||= event.error
216
+ when Internal::Worker::MultiRunner::Event::ShutdownSignalReceived
217
+ # Do nothing, waiting for block
218
+ else
219
+ raise "Unexpected event: #{event}"
220
+ end
221
+ end
222
+ end
223
+
224
+ # Notify each worker we're done with it
225
+ workers.each(&:_on_shutdown_complete)
226
+
227
+ # If there was an shutdown-causing error, we raise that
228
+ if !first_error.nil?
229
+ raise first_error
230
+ elsif block_result.is_a?(Internal::Worker::MultiRunner::Event::BlockSuccess)
231
+ block_result.result
232
+ end
233
+ end
234
+
235
+ # @return [Hash<String, [:all, Array<Symbol>]>] Default, immutable set illegal calls used for the
236
+ # `illegal_workflow_calls` worker option. See the documentation of that option for more details.
237
+ def self.default_illegal_workflow_calls
238
+ @default_illegal_workflow_calls ||= begin
239
+ hash = {
240
+ 'BasicSocket' => :all,
241
+ 'Date' => %i[initialize today],
242
+ 'DateTime' => %i[initialize now],
243
+ 'Dir' => :all,
244
+ 'Fiber' => [:set_scheduler],
245
+ 'File' => :all,
246
+ 'FileTest' => :all,
247
+ 'FileUtils' => :all,
248
+ 'Find' => :all,
249
+ 'GC' => :all,
250
+ 'IO' => [
251
+ :read
252
+ # Intentionally leaving out write so puts will work. We don't want to add heavy logic replacing stdout or
253
+ # trying to derive whether it's file vs stdout write.
254
+ #:write
255
+ ],
256
+ 'Kernel' => %i[abort at_exit autoload autoload? eval exec exit fork gets load open rand readline readlines
257
+ spawn srand system test trap],
258
+ 'Net::HTTP' => :all,
259
+ 'Pathname' => :all,
260
+ # TODO(cretz): Investigate why clock_gettime called from Timeout thread affects this code at all. Stack trace
261
+ # test executing activities inside a timeout will fail if clock_gettime is blocked.
262
+ 'Process' => %i[abort argv0 daemon detach exec exit exit! fork kill setpriority setproctitle setrlimit setsid
263
+ spawn times wait wait2 waitall warmup],
264
+ # TODO(cretz): Allow Ractor.current since exception formatting in error_highlight references it
265
+ # 'Ractor' => :all,
266
+ 'Random::Base' => [:initialize],
267
+ 'Resolv' => :all,
268
+ 'SecureRandom' => :all,
269
+ 'Signal' => :all,
270
+ 'Socket' => :all,
271
+ 'Tempfile' => :all,
272
+ 'Thread' => %i[abort_on_exception= exit fork handle_interrupt ignore_deadlock= kill new pass
273
+ pending_interrupt? report_on_exception= start stop initialize join name= priority= raise run
274
+ terminate thread_variable_set wakeup],
275
+ 'Time' => %i[initialize now]
276
+ } #: Hash[String, :all | Array[Symbol]]
277
+ hash.each_value(&:freeze)
278
+ hash.freeze
279
+ end
280
+ end
281
+
282
+ # @return [Options] Options for this worker which has the same attributes as {initialize}.
283
+ attr_reader :options
284
+
285
+ # Create a new worker. At least one activity or workflow must be present.
286
+ #
287
+ # @param client [Client] Client for this worker.
288
+ # @param task_queue [String] Task queue for this worker.
289
+ # @param activities [Array<Activity::Definition, Class<Activity::Definition>, Activity::Definition::Info>]
290
+ # Activities for this worker.
291
+ # @param workflows [Array<Class<Workflow::Definition>>] Workflows for this worker.
292
+ # @param tuner [Tuner] Tuner that controls the amount of concurrent activities/workflows that run at a time.
293
+ # @param activity_executors [Hash<Symbol, Worker::ActivityExecutor>] Executors that activities can run within.
294
+ # @param workflow_executor [WorkflowExecutor] Workflow executor that workflow tasks run within. This must be a
295
+ # {WorkflowExecutor::ThreadPool} currently.
296
+ # @param interceptors [Array<Interceptor::Activity, Interceptor::Workflow>] Interceptors specific to this worker.
297
+ # Note, interceptors set on the client that include the {Interceptor::Activity} or {Interceptor::Workflow} module
298
+ # are automatically included here, so no need to specify them again.
299
+ # @param build_id [String] Unique identifier for the current runtime. This is best set as a unique value
300
+ # representing all code and should change only when code does. This can be something like a git commit hash. If
301
+ # unset, default is hash of known Ruby code.
302
+ # @param identity [String, nil] Override the identity for this worker. If unset, client identity is used.
303
+ # @param logger [Logger] Logger to override client logger with. Default is the client logger.
304
+ # @param max_cached_workflows [Integer] Number of workflows held in cache for use by sticky task queue. If set to 0,
305
+ # workflow caching and sticky queuing are disabled.
306
+ # @param max_concurrent_workflow_task_polls [Integer] Maximum number of concurrent poll workflow task requests we
307
+ # will perform at a time on this worker's task queue.
308
+ # @param nonsticky_to_sticky_poll_ratio [Float] `max_concurrent_workflow_task_polls` * this number = the number of
309
+ # max pollers that will be allowed for the nonsticky queue when sticky tasks are enabled. If both defaults are
310
+ # used, the sticky queue will allow 4 max pollers while the nonsticky queue will allow one. The minimum for either
311
+ # poller is 1, so if `max_concurrent_workflow_task_polls` is 1 and sticky queues are enabled, there will be 2
312
+ # concurrent polls.
313
+ # @param max_concurrent_activity_task_polls [Integer] Maximum number of concurrent poll activity task requests we
314
+ # will perform at a time on this worker's task queue.
315
+ # @param no_remote_activities [Boolean] If true, this worker will only handle workflow tasks and local activities,
316
+ # it will not poll for activity tasks.
317
+ # @param sticky_queue_schedule_to_start_timeout [Float] How long a workflow task is allowed to sit on the sticky
318
+ # queue before it is timed out and moved to the non-sticky queue where it may be picked up by any worker.
319
+ # @param max_heartbeat_throttle_interval [Float] Longest interval for throttling activity heartbeats.
320
+ # @param default_heartbeat_throttle_interval [Float] Default interval for throttling activity heartbeats in case
321
+ # per-activity heartbeat timeout is unset. Otherwise, it's the per-activity heartbeat timeout * 0.8.
322
+ # @param max_activities_per_second [Float, nil] Limits the number of activities per second that this worker will
323
+ # process. The worker will not poll for new activities if by doing so it might receive and execute an activity
324
+ # which would cause it to exceed this limit.
325
+ # @param max_task_queue_activities_per_second [Float, nil] Sets the maximum number of activities per second the task
326
+ # queue will dispatch, controlled server-side. Note that this only takes effect upon an activity poll request. If
327
+ # multiple workers on the same queue have different values set, they will thrash with the last poller winning.
328
+ # @param graceful_shutdown_period [Float] Amount of time after shutdown is called that activities are given to
329
+ # complete before their tasks are canceled.
330
+ # @param use_worker_versioning [Boolean] If true, the `build_id` argument must be specified, and this worker opts
331
+ # into the worker versioning feature. This ensures it only receives workflow tasks for workflows which it claims
332
+ # to be compatible with. For more information, see https://docs.temporal.io/workers#worker-versioning.
333
+ # @param disable_eager_activity_execution [Boolean] If true, disables eager activity execution. Eager activity
334
+ # execution is an optimization on some servers that sends activities back to the same worker as the calling
335
+ # workflow if they can run there. This should be set to true for `max_task_queue_activities_per_second` to work
336
+ # and in a future version of this API may be implied as such (i.e. this setting will be ignored if that setting is
337
+ # set).
338
+ # @param illegal_workflow_calls [Hash<String, [:all, Array<Symbol>]>] Set of illegal workflow calls that are
339
+ # considered unsafe/non-deterministic and will raise if seen. The key of the hash is the fully qualified string
340
+ # class name (no leading `::`). The value is either `:all` which means any use of the class, or an array of
341
+ # symbols for methods on the class that cannot be used. The methods refer to either instance or class methods,
342
+ # there is no way to differentiate at this time.
343
+ # @param workflow_failure_exception_types [Array<Class<Exception>>] Workflow failure exception types. This is the
344
+ # set of exception types that, if a workflow-thrown exception extends, will cause the workflow/update to fail
345
+ # instead of suspending the workflow via task failure. These are applied in addition to the
346
+ # `workflow_failure_exception_type` on the workflow definition class itself. If {::Exception} is set, it
347
+ # effectively will fail a workflow/update in all user exception cases.
348
+ # @param workflow_payload_codec_thread_pool [ThreadPool, nil] Thread pool to run payload codec encode/decode within.
349
+ # This is required if a payload codec exists and the worker is not fiber based. Codecs can potentially block
350
+ # execution which is why they need to be run in the background.
351
+ # @param unsafe_workflow_io_enabled [Boolean] If false, the default, workflow code that invokes io_wait on the fiber
352
+ # scheduler will fail. Instead of setting this to true, users are encouraged to use {Workflow::Unsafe.io_enabled}
353
+ # with a block for narrower enabling of IO.
354
+ # @param debug_mode [Boolean] If true, deadlock detection is disabled. Deadlock detection will fail workflow tasks
355
+ # if they block the thread for too long. This defaults to true if the `TEMPORAL_DEBUG` environment variable is
356
+ # `true` or `1`.
357
+ def initialize(
358
+ client:,
359
+ task_queue:,
360
+ activities: [],
361
+ workflows: [],
362
+ tuner: Tuner.create_fixed,
363
+ activity_executors: ActivityExecutor.defaults,
364
+ workflow_executor: WorkflowExecutor::ThreadPool.default,
365
+ interceptors: [],
366
+ build_id: Worker.default_build_id,
367
+ identity: nil,
368
+ logger: client.options.logger,
369
+ max_cached_workflows: 1000,
370
+ max_concurrent_workflow_task_polls: 5,
371
+ nonsticky_to_sticky_poll_ratio: 0.2,
372
+ max_concurrent_activity_task_polls: 5,
373
+ no_remote_activities: false,
374
+ sticky_queue_schedule_to_start_timeout: 10,
375
+ max_heartbeat_throttle_interval: 60,
376
+ default_heartbeat_throttle_interval: 30,
377
+ max_activities_per_second: nil,
378
+ max_task_queue_activities_per_second: nil,
379
+ graceful_shutdown_period: 0,
380
+ use_worker_versioning: false,
381
+ disable_eager_activity_execution: false,
382
+ illegal_workflow_calls: Worker.default_illegal_workflow_calls,
383
+ workflow_failure_exception_types: [],
384
+ workflow_payload_codec_thread_pool: nil,
385
+ unsafe_workflow_io_enabled: false,
386
+ debug_mode: %w[true 1].include?(ENV['TEMPORAL_DEBUG'].to_s.downcase)
387
+ )
388
+ raise ArgumentError, 'Must have at least one activity or workflow' if activities.empty? && workflows.empty?
389
+
390
+ Internal::ProtoUtils.assert_non_reserved_name(task_queue)
391
+
392
+ @options = Options.new(
393
+ client:,
394
+ task_queue:,
395
+ activities:,
396
+ workflows:,
397
+ tuner:,
398
+ activity_executors:,
399
+ workflow_executor:,
400
+ interceptors:,
401
+ build_id:,
402
+ identity:,
403
+ logger:,
404
+ max_cached_workflows:,
405
+ max_concurrent_workflow_task_polls:,
406
+ nonsticky_to_sticky_poll_ratio:,
407
+ max_concurrent_activity_task_polls:,
408
+ no_remote_activities:,
409
+ sticky_queue_schedule_to_start_timeout:,
410
+ max_heartbeat_throttle_interval:,
411
+ default_heartbeat_throttle_interval:,
412
+ max_activities_per_second:,
413
+ max_task_queue_activities_per_second:,
414
+ graceful_shutdown_period:,
415
+ use_worker_versioning:,
416
+ disable_eager_activity_execution:,
417
+ illegal_workflow_calls:,
418
+ workflow_failure_exception_types:,
419
+ workflow_payload_codec_thread_pool:,
420
+ unsafe_workflow_io_enabled:,
421
+ debug_mode:
422
+ ).freeze
423
+
424
+ # Preload workflow definitions and some workflow settings for the bridge
425
+ workflow_definitions = Internal::Worker::WorkflowWorker.workflow_definitions(workflows)
426
+ nondeterminism_as_workflow_fail, nondeterminism_as_workflow_fail_for_types =
427
+ Internal::Worker::WorkflowWorker.bridge_workflow_failure_exception_type_options(
428
+ workflow_failure_exception_types:, workflow_definitions:
429
+ )
430
+
431
+ # Create the bridge worker
432
+ @bridge_worker = Internal::Bridge::Worker.new(
433
+ client.connection._core_client,
434
+ Internal::Bridge::Worker::Options.new(
435
+ activity: !activities.empty?,
436
+ workflow: !workflows.empty?,
437
+ namespace: client.namespace,
438
+ task_queue:,
439
+ tuner: tuner._to_bridge_options,
440
+ build_id:,
441
+ identity_override: identity,
442
+ max_cached_workflows:,
443
+ max_concurrent_workflow_task_polls:,
444
+ nonsticky_to_sticky_poll_ratio:,
445
+ max_concurrent_activity_task_polls:,
446
+ # For shutdown to work properly, we must disable remote activities
447
+ # ourselves if there are no activities
448
+ no_remote_activities: no_remote_activities || activities.empty?,
449
+ sticky_queue_schedule_to_start_timeout:,
450
+ max_heartbeat_throttle_interval:,
451
+ default_heartbeat_throttle_interval:,
452
+ max_worker_activities_per_second: max_activities_per_second,
453
+ max_task_queue_activities_per_second:,
454
+ graceful_shutdown_period:,
455
+ use_worker_versioning:,
456
+ nondeterminism_as_workflow_fail:,
457
+ nondeterminism_as_workflow_fail_for_types:
458
+ )
459
+ )
460
+
461
+ # Collect interceptors from client and params
462
+ @activity_interceptors = (client.options.interceptors + interceptors).select do |i|
463
+ i.is_a?(Interceptor::Activity)
464
+ end
465
+ @workflow_interceptors = (client.options.interceptors + interceptors).select do |i|
466
+ i.is_a?(Interceptor::Workflow)
467
+ end
468
+
469
+ # Cancellation for the whole worker
470
+ @worker_shutdown_cancellation = Cancellation.new
471
+
472
+ # Create workers
473
+ unless activities.empty?
474
+ @activity_worker = Internal::Worker::ActivityWorker.new(worker: self,
475
+ bridge_worker: @bridge_worker)
476
+ end
477
+ unless workflows.empty?
478
+ @workflow_worker = Internal::Worker::WorkflowWorker.new(
479
+ bridge_worker: @bridge_worker,
480
+ namespace: client.namespace,
481
+ task_queue:,
482
+ workflow_definitions:,
483
+ workflow_executor:,
484
+ logger:,
485
+ data_converter: client.data_converter,
486
+ metric_meter: client.connection.options.runtime.metric_meter,
487
+ workflow_interceptors: @workflow_interceptors,
488
+ disable_eager_activity_execution:,
489
+ illegal_workflow_calls:,
490
+ workflow_failure_exception_types:,
491
+ workflow_payload_codec_thread_pool:,
492
+ unsafe_workflow_io_enabled:,
493
+ debug_mode:
494
+ )
495
+ end
496
+
497
+ # Validate worker
498
+ @bridge_worker.validate
499
+
500
+ # Mutex needed for accessing and replacing a client
501
+ @client_mutex = Mutex.new
502
+ end
503
+
504
+ # @return [String] Task queue set on the worker options.
505
+ def task_queue
506
+ @options.task_queue
507
+ end
508
+
509
+ # @return [Client] Client for this worker. This is the same as {Options.client} in {options}, but surrounded by a
510
+ # mutex to be safe for client replacement in {client=}.
511
+ def client
512
+ @client_mutex.synchronize { @options.client }
513
+ end
514
+
515
+ # Replace the worker's client. When this is called, the client is replaced on the internal worker which means any
516
+ # new calls will be made on the new client (but existing calls will still complete on the previous one). This is
517
+ # commonly used for providing a new client with updated authentication credentials.
518
+ #
519
+ # @param new_client [Client] New client to use for new calls.
520
+ def client=(new_client)
521
+ @client_mutex.synchronize do
522
+ @bridge_worker.replace_client(new_client.connection._core_client)
523
+ @options = @options.with(client: new_client)
524
+ new_client
525
+ end
526
+ end
527
+
528
+ # Run this worker until cancellation or optional block completes. When the cancellation or block is complete, the
529
+ # worker is shut down. This will return the block result if everything successful or raise an error if not.
530
+ #
531
+ # Upon shutdown (either via cancellation, block completion, or worker fatal error), the worker immediately stops
532
+ # accepting new work. Then, after an optional grace period, all activities are canceled. This call then waits for
533
+ # every activity and workflow task to complete before returning.
534
+ #
535
+ # @param cancellation [Cancellation] Cancellation that can be canceled to shut down this worker.
536
+ # @param shutdown_signals [Array] Signals to trap and cause worker shutdown.
537
+ # @param raise_in_block_on_shutdown [Exception, nil] Exception to {::Thread.raise} or {::Fiber.raise} if a block is
538
+ # present and still running on shutdown. If nil, `raise` is not used.
539
+ # @param wait_block_complete [Boolean] If block given and shutdown caused by something else (e.g. cancellation
540
+ # canceled), whether to wait on the block to complete before returning.
541
+ # @yield Optional block. This will be run in a new background thread or fiber. Worker will shut down upon completion
542
+ # of this and, assuming no other failures, return/bubble success/exception of the block.
543
+ # @return [Object] Return value of the block or nil of no block given.
544
+ def run(
545
+ cancellation: Cancellation.new,
546
+ shutdown_signals: [],
547
+ raise_in_block_on_shutdown: Error::CanceledError.new('Workers finished'),
548
+ wait_block_complete: true,
549
+ &block
550
+ )
551
+ Worker.run_all(self, cancellation:, shutdown_signals:, raise_in_block_on_shutdown:, wait_block_complete:, &block)
552
+ end
553
+
554
+ # @!visibility private
555
+ def _worker_shutdown_cancellation
556
+ @worker_shutdown_cancellation
557
+ end
558
+
559
+ # @!visibility private
560
+ def _initiate_shutdown
561
+ _bridge_worker.initiate_shutdown
562
+ _, cancel_proc = _worker_shutdown_cancellation
563
+ cancel_proc.call
564
+ end
565
+
566
+ # @!visibility private
567
+ def _wait_all_complete
568
+ @activity_worker&.wait_all_complete
569
+ end
570
+
571
+ # @!visibility private
572
+ def _bridge_worker
573
+ @bridge_worker
574
+ end
575
+
576
+ # @!visibility private
577
+ def _activity_interceptors
578
+ @activity_interceptors
579
+ end
580
+
581
+ # @!visibility private
582
+ def _on_poll_bytes(runner, worker_type, bytes)
583
+ case worker_type
584
+ when :activity
585
+ @activity_worker.handle_task(Internal::Bridge::Api::ActivityTask::ActivityTask.decode(bytes))
586
+ when :workflow
587
+ @workflow_worker.handle_activation(
588
+ runner:,
589
+ activation: Internal::Bridge::Api::WorkflowActivation::WorkflowActivation.decode(bytes),
590
+ decoded: false
591
+ )
592
+ else
593
+ raise "Unrecognized worker type #{worker_type}"
594
+ end
595
+ end
596
+
597
+ # @!visibility private
598
+ def _on_shutdown_complete
599
+ @workflow_worker&.on_shutdown_complete
600
+ @workflow_worker = nil
601
+ end
602
+ end
603
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'temporalio/internal/bridge/api'
4
+
5
+ module Temporalio
6
+ module Workflow
7
+ # Cancellation types for activities.
8
+ module ActivityCancellationType
9
+ # Initiate a cancellation request and immediately report cancellation to the workflow.
10
+ TRY_CANCEL = Internal::Bridge::Api::WorkflowCommands::ActivityCancellationType::TRY_CANCEL
11
+ # Wait for activity cancellation completion. Note that activity must heartbeat to receive a cancellation
12
+ # notification. This can block the cancellation for a long time if activity doesn't heartbeat or chooses to ignore
13
+ # the cancellation request.
14
+ WAIT_CANCELLATION_COMPLETED =
15
+ Internal::Bridge::Api::WorkflowCommands::ActivityCancellationType::WAIT_CANCELLATION_COMPLETED
16
+ # Do not request cancellation of the activity and immediately report cancellation to the workflow.
17
+ ABANDON = Internal::Bridge::Api::WorkflowCommands::ActivityCancellationType::ABANDON
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,21 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'temporalio/internal/bridge/api'
4
+
5
+ module Temporalio
6
+ module Workflow
7
+ # Cancellation types for child workflows.
8
+ module ChildWorkflowCancellationType
9
+ # Do not request cancellation of the child workflow if already scheduled.
10
+ ABANDON = Internal::Bridge::Api::ChildWorkflow::ChildWorkflowCancellationType::ABANDON
11
+ # Initiate a cancellation request and immediately report cancellation to the parent.
12
+ TRY_CANCEL = Internal::Bridge::Api::ChildWorkflow::ChildWorkflowCancellationType::TRY_CANCEL
13
+ # Wait for child cancellation completion.
14
+ WAIT_CANCELLATION_COMPLETED =
15
+ Internal::Bridge::Api::ChildWorkflow::ChildWorkflowCancellationType::WAIT_CANCELLATION_COMPLETED
16
+ # Request cancellation of the child and wait for confirmation that the request was received.
17
+ WAIT_CANCELLATION_REQUESTED =
18
+ Internal::Bridge::Api::ChildWorkflow::ChildWorkflowCancellationType::WAIT_CANCELLATION_REQUESTED
19
+ end
20
+ end
21
+ end