temporalio 0.6.0 → 1.0.0

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.
@@ -125,6 +125,7 @@ module Temporalio
125
125
  continued_run_id: ProtoUtils.string_or(@init_job.continued_from_execution_run_id),
126
126
  cron_schedule: ProtoUtils.string_or(@init_job.cron_schedule),
127
127
  execution_timeout: ProtoUtils.duration_to_seconds(@init_job.workflow_execution_timeout),
128
+ first_execution_run_id: @init_job.first_execution_run_id,
128
129
  headers: ProtoUtils.headers_from_proto_map(@init_job.headers, @payload_converter) || {},
129
130
  last_failure: if @init_job.continued_failure
130
131
  @failure_converter.from_failure(@init_job.continued_failure, @payload_converter)
@@ -162,86 +163,9 @@ module Temporalio
162
163
  end
163
164
 
164
165
  def activate(activation)
165
- # Run inside of scheduler
166
- run_in_scheduler { activate_internal(activation) }
167
- end
168
-
169
- def add_command(command)
170
- raise Workflow::InvalidWorkflowStateError, 'Cannot add commands in this context' if @context_frozen
171
-
172
- @commands << command
173
- end
174
-
175
- def instance
176
- @instance or raise 'Instance accessed before created'
177
- end
178
-
179
- def search_attributes
180
- # Lazy on first access
181
- @search_attributes ||= SearchAttributes._from_proto(
182
- @init_job.search_attributes, disable_mutations: true, never_nil: true
183
- ) || raise
184
- end
185
-
186
- def memo
187
- # Lazy on first access
188
- @memo ||= ExternallyImmutableHash.new(ProtoUtils.memo_from_proto(@init_job.memo, payload_converter) || {})
189
- end
190
-
191
- def now
192
- # Create each time
193
- ProtoUtils.timestamp_to_time(@now_timestamp) or raise 'Time unexpectedly not present'
194
- end
195
-
196
- def illegal_call_tracing_disabled(&)
197
- @tracer.disable(&)
198
- end
199
-
200
- def patch(patch_id:, deprecated:)
201
- # Use memoized result if present. If this is being deprecated, we can still use memoized result and skip the
202
- # command.
203
- patch_id = patch_id.to_s
204
- @patches_memoized ||= {}
205
- @patches_memoized.fetch(patch_id) do
206
- patched = !replaying || @patches_notified.include?(patch_id)
207
- @patches_memoized[patch_id] = patched
208
- if patched
209
- add_command(
210
- Bridge::Api::WorkflowCommands::WorkflowCommand.new(
211
- set_patch_marker: Bridge::Api::WorkflowCommands::SetPatchMarker.new(patch_id:, deprecated:)
212
- )
213
- )
214
- end
215
- patched
216
- end
217
- end
218
-
219
- def metric_meter
220
- @metric_meter ||= ReplaySafeMetric::Meter.new(
221
- @runtime_metric_meter.with_additional_attributes(
222
- {
223
- namespace: info.namespace,
224
- task_queue: info.task_queue,
225
- workflow_type: info.workflow_type
226
- }
227
- )
228
- )
229
- end
230
-
231
- private
232
-
233
- def run_in_scheduler(&)
166
+ # Run inside of scheduler (removed on ensure)
234
167
  Fiber.set_scheduler(@scheduler)
235
- if @tracer
236
- @tracer.enable(&)
237
- else
238
- yield
239
- end
240
- ensure
241
- Fiber.set_scheduler(nil)
242
- end
243
168
 
244
- def activate_internal(activation)
245
169
  # Reset some activation state
246
170
  @commands = []
247
171
  @current_activation_error = nil
@@ -266,8 +190,12 @@ module Temporalio
266
190
  # the first activation)
267
191
  @primary_fiber ||= schedule(top_level: true) { run_workflow }
268
192
 
269
- # Run the event loop
270
- @scheduler.run_until_all_yielded
193
+ # Run the event loop in the tracer if it exists
194
+ if @tracer
195
+ @tracer.enable { @scheduler.run_until_all_yielded }
196
+ else
197
+ @scheduler.run_until_all_yielded
198
+ end
271
199
  rescue Exception => e # rubocop:disable Lint/RescueException
272
200
  on_top_level_exception(e)
273
201
  end
@@ -306,8 +234,77 @@ module Temporalio
306
234
  ensure
307
235
  @commands = nil
308
236
  @current_activation_error = nil
237
+ Fiber.set_scheduler(nil)
238
+ end
239
+
240
+ def add_command(command)
241
+ raise Workflow::InvalidWorkflowStateError, 'Cannot add commands in this context' if @context_frozen
242
+
243
+ @commands << command
244
+ end
245
+
246
+ def instance
247
+ @instance or raise 'Instance accessed before created'
248
+ end
249
+
250
+ def search_attributes
251
+ # Lazy on first access
252
+ @search_attributes ||= SearchAttributes._from_proto(
253
+ @init_job.search_attributes, disable_mutations: true, never_nil: true
254
+ ) || raise
255
+ end
256
+
257
+ def memo
258
+ # Lazy on first access
259
+ @memo ||= ExternallyImmutableHash.new(ProtoUtils.memo_from_proto(@init_job.memo, payload_converter) || {})
260
+ end
261
+
262
+ def now
263
+ # Create each time
264
+ ProtoUtils.timestamp_to_time(@now_timestamp) or raise 'Time unexpectedly not present'
309
265
  end
310
266
 
267
+ def illegal_call_tracing_disabled(&)
268
+ if @tracer
269
+ @tracer.disable_temporarily(&)
270
+ else
271
+ yield
272
+ end
273
+ end
274
+
275
+ def patch(patch_id:, deprecated:)
276
+ # Use memoized result if present. If this is being deprecated, we can still use memoized result and skip the
277
+ # command.
278
+ patch_id = patch_id.to_s
279
+ @patches_memoized ||= {}
280
+ @patches_memoized.fetch(patch_id) do
281
+ patched = !replaying || @patches_notified.include?(patch_id)
282
+ @patches_memoized[patch_id] = patched
283
+ if patched
284
+ add_command(
285
+ Bridge::Api::WorkflowCommands::WorkflowCommand.new(
286
+ set_patch_marker: Bridge::Api::WorkflowCommands::SetPatchMarker.new(patch_id:, deprecated:)
287
+ )
288
+ )
289
+ end
290
+ patched
291
+ end
292
+ end
293
+
294
+ def metric_meter
295
+ @metric_meter ||= ReplaySafeMetric::Meter.new(
296
+ @runtime_metric_meter.with_additional_attributes(
297
+ {
298
+ namespace: info.namespace,
299
+ task_queue: info.task_queue,
300
+ workflow_type: info.workflow_type
301
+ }
302
+ )
303
+ )
304
+ end
305
+
306
+ private
307
+
311
308
  def create_instance
312
309
  # Convert workflow arguments
313
310
  @workflow_arguments = convert_args(payload_array: @init_job.arguments,
@@ -25,6 +25,7 @@ module Temporalio
25
25
  local?: false,
26
26
  priority: Temporalio::Priority.default,
27
27
  raw_heartbeat_details: [],
28
+ retry_policy: RetryPolicy.new,
28
29
  schedule_to_close_timeout: 1.0,
29
30
  scheduled_time: Time.at(0),
30
31
  start_to_close_timeout: 1.0,
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Temporalio
4
- VERSION = '0.6.0'
4
+ VERSION = '1.0.0'
5
5
  end
@@ -228,6 +228,7 @@ module Temporalio
228
228
  ExecuteLocalActivityInput = Data.define(
229
229
  :activity,
230
230
  :args,
231
+ :summary,
231
232
  :schedule_to_close_timeout,
232
233
  :schedule_to_start_timeout,
233
234
  :start_to_close_timeout,
@@ -22,10 +22,11 @@ module Temporalio
22
22
  end
23
23
 
24
24
  # @!visibility private
25
- def _to_bridge_options
25
+ def _to_bridge_options(_tuner)
26
26
  Internal::Bridge::Worker::TunerSlotSupplierOptions.new(
27
27
  fixed_size: slots,
28
- resource_based: nil
28
+ resource_based: nil,
29
+ custom: nil
29
30
  )
30
31
  end
31
32
  end
@@ -36,7 +37,7 @@ module Temporalio
36
37
  class ResourceBased < SlotSupplier
37
38
  attr_reader :tuner_options, :slot_options
38
39
 
39
- # Create a reosurce-based slot supplier.
40
+ # Create a resource-based slot supplier.
40
41
  #
41
42
  # @param tuner_options [ResourceBasedTunerOptions] General tuner options.
42
43
  # @param slot_options [ResourceBasedSlotOptions] Slot-supplier-specific tuner options.
@@ -46,7 +47,7 @@ module Temporalio
46
47
  end
47
48
 
48
49
  # @!visibility private
49
- def _to_bridge_options
50
+ def _to_bridge_options(_tuner)
50
51
  Internal::Bridge::Worker::TunerSlotSupplierOptions.new(
51
52
  fixed_size: nil,
52
53
  resource_based: Internal::Bridge::Worker::TunerResourceBasedSlotSupplierOptions.new(
@@ -55,14 +56,175 @@ module Temporalio
55
56
  min_slots: slot_options.min_slots,
56
57
  max_slots: slot_options.max_slots,
57
58
  ramp_throttle: slot_options.ramp_throttle
59
+ ),
60
+ custom: nil
61
+ )
62
+ end
63
+ end
64
+
65
+ # A slot supplier that has callbacks invoked to handle slot supplying.
66
+ #
67
+ # Users should be cautious when implementing this and make sure it is heavily tested and the documentation for
68
+ # every method is well understood.
69
+ #
70
+ # @note WARNING: This API is experimental.
71
+ class Custom < SlotSupplier
72
+ # Context provided for slot reservation on custom slot supplier.
73
+ #
74
+ # @!attribute slot_type
75
+ # @return [:workflow, :activity, :local_activity, :nexus] Slot type.
76
+ # @!attribute task_queue
77
+ # @return [String] Task queue.
78
+ # @!attribute worker_identity
79
+ # @return [String] Worker identity.
80
+ # @!attribute worker_deployment_name
81
+ # @return [String] Worker deployment name or empty string if not applicable.
82
+ # @!attribute worker_build_id
83
+ # @return [String] Worker build ID or empty string if not applicable.
84
+ # @!attribute sticky?
85
+ # @return [Boolean] True if this reservation is for a sticky workflow task.
86
+ ReserveContext = Data.define(
87
+ :slot_type,
88
+ :task_queue,
89
+ :worker_identity,
90
+ :worker_deployment_name,
91
+ :worker_build_id,
92
+ :sticky?
93
+ )
94
+
95
+ # Context provided for marking a slot used.
96
+ #
97
+ # @!attribute slot_info
98
+ # @return [SlotInfo::Workflow, SlotInfo::Activity, SlotInfo::LocalActivity, SlotInfo::Nexus] Information
99
+ # about the slot. This is never nil.
100
+ # @!attribute permit
101
+ # @return [Object] Object that was provided as the permit on reserve.
102
+ MarkUsedContext = Data.define(
103
+ :slot_info,
104
+ :permit
105
+ )
106
+
107
+ # Context provided for releasing a slot.
108
+ #
109
+ # @!attribute slot_info
110
+ # @return [SlotInfo::Workflow, SlotInfo::Activity, SlotInfo::LocalActivity, SlotInfo::Nexus, nil]
111
+ # Information about the slot. This may be nil if the slot was never used.
112
+ # @!attribute permit
113
+ # @return [Object] Object that was provided as the permit on reserve.
114
+ ReleaseContext = Data.define(
115
+ :slot_info,
116
+ :permit
117
+ )
118
+
119
+ # Reserve a slot.
120
+ #
121
+ # This can/should block and must provide the permit to the (code) block. The permit is any object (including
122
+ # nil) that will be given on the context for mark_slot_used and release_slot.
123
+ #
124
+ # Just returning from this call is not enough to reserve the slot, a permit must be provided to the block
125
+ # (e.g. via yield or block.call). If the call completes, the system will still wait on the block (so this can
126
+ # be backgrounded by passing the block to something else). Reservations may be canceled via the given
127
+ # cancellation. Users can do things like add_cancel_callback, but it is very important that the code in the
128
+ # callback is fast as it is run on the same reactor thread as many other Temporal Ruby worker calls.
129
+ #
130
+ # @note WARNING: This call should never raise an exception. Any exception raised is ignored and this is called
131
+ # again after 1 second.
132
+ #
133
+ # @param context [ReserveContext] Contextual information about this reserve call.
134
+ # @param cancellation [Cancellation] Cancellation that is canceled when the reservation is no longer being
135
+ # asked for.
136
+ # @yield [Object] Confirm reservation and provide a permit.
137
+ def reserve_slot(context, cancellation, &)
138
+ raise NotImplementedError
139
+ end
140
+
141
+ # Try to reserve a slot.
142
+ #
143
+ # @note WARNING: This should never block, this should return immediately with a permit, or nil if the
144
+ # reservation could not occur.
145
+ #
146
+ # @note WARNING: This call should never raise an exception. Any exception raised is ignored and the slot
147
+ # reservation attempt fails (i.e. same as if this method returned nil).
148
+ #
149
+ # @param context [ReserveContext] Contextual information about this reserve call.
150
+ # @return [Object, nil] A non-nil object to perform the reservation successfully, a nil to fail the
151
+ # reservation.
152
+ def try_reserve_slot(context)
153
+ raise NotImplementedError
154
+ end
155
+
156
+ # Mark a slot as used.
157
+ #
158
+ # Due to the nature of Temporal polling, slots are reserved before they are used and may never get used. This
159
+ # call is made as just a notification when a slot is actually used.
160
+ #
161
+ # @note WARNING: This should never block, this should return immediately.
162
+ #
163
+ # @note WARNING: This call should never raise an exception. Any exception raised is ignored.
164
+ #
165
+ # @param context [MarkUsedContext] Contextual information about this reserve call.
166
+ def mark_slot_used(context)
167
+ raise NotImplementedError
168
+ end
169
+
170
+ # Release a previously reserved slot.
171
+ #
172
+ # @note WARNING: This should never block, this should return immediately.
173
+ #
174
+ # @note WARNING: This call should never raise an exception. Any exception raised is ignored.
175
+ #
176
+ # @param context [ReleaseContext] Contextual information about this reserve call.
177
+ def release_slot(context)
178
+ raise NotImplementedError
179
+ end
180
+
181
+ # @!visibility private
182
+ def _to_bridge_options(tuner)
183
+ Internal::Bridge::Worker::TunerSlotSupplierOptions.new(
184
+ fixed_size: nil,
185
+ resource_based: nil,
186
+ custom: Internal::Bridge::Worker::CustomSlotSupplier.new(
187
+ slot_supplier: self,
188
+ thread_pool: tuner.custom_slot_supplier_thread_pool
58
189
  )
59
190
  )
60
191
  end
192
+
193
+ # Slot information.
194
+ module SlotInfo
195
+ # Information about a workflow slot.
196
+ #
197
+ # @!attribute workflow_type
198
+ # @return [String] Workflow type.
199
+ # @!attribute sticky?
200
+ # @return [Boolean] Whether the slot was for a sticky task.
201
+ Workflow = Data.define(:workflow_type, :sticky?)
202
+
203
+ # Information about an activity slot.
204
+ #
205
+ # @!attribute activity_type
206
+ # @return [String] Activity type.
207
+ Activity = Data.define(:activity_type)
208
+
209
+ # Information about a local activity slot.
210
+ #
211
+ # @!attribute activity_type
212
+ # @return [String] Activity type.
213
+ LocalActivity = Data.define(:activity_type)
214
+
215
+ # Information about a Nexus slot.
216
+ #
217
+ # @!attribute service
218
+ # @return [String] Nexus service.
219
+ # @!attribute operation
220
+ # @return [String] Nexus operation.
221
+ Nexus = Data.define(:service, :operation)
222
+ end
61
223
  end
62
224
 
63
225
  # @!visibility private
64
- def _to_bridge_options
65
- raise ArgumentError, 'Tuner slot suppliers must be instances of Fixed or ResourceBased'
226
+ def _to_bridge_options(_tuner)
227
+ raise ArgumentError, 'Tuner slot suppliers must be instances of Fixed, ResourceBased, or Custom'
66
228
  end
67
229
  end
68
230
 
@@ -75,10 +237,9 @@ module Temporalio
75
237
  # @!attribute target_cpu_usage
76
238
  # @return [Float] A value between 0 and 1 that represents the target (system) CPU usage. This can be set to 1.0
77
239
  # if desired, but it's recommended to leave some headroom for other processes.
78
- ResourceBasedTunerOptions = Struct.new(
240
+ ResourceBasedTunerOptions = Data.define(
79
241
  :target_memory_usage,
80
- :target_cpu_usage,
81
- keyword_init: true
242
+ :target_cpu_usage
82
243
  )
83
244
 
84
245
  # Options for a specific slot type being used with {SlotSupplier::ResourceBased}.
@@ -94,11 +255,10 @@ module Temporalio
94
255
  #
95
256
  # This value matters because how many resources a task will use cannot be determined ahead of time, and thus
96
257
  # the system should wait to see how much resources are used before issuing more slots.
97
- ResourceBasedSlotOptions = Struct.new(
258
+ ResourceBasedSlotOptions = Data.define(
98
259
  :min_slots,
99
260
  :max_slots,
100
- :ramp_throttle,
101
- keyword_init: true
261
+ :ramp_throttle
102
262
  )
103
263
 
104
264
  # Create a fixed-size tuner with the provided number of slots.
@@ -161,27 +321,36 @@ module Temporalio
161
321
  # @return [SlotSupplier] Slot supplier for local activities.
162
322
  attr_reader :local_activity_slot_supplier
163
323
 
324
+ # @return [ThreadPool, nil] Thread pool for custom slot suppliers.
325
+ attr_reader :custom_slot_supplier_thread_pool
326
+
164
327
  # Create a tuner from 3 slot suppliers.
165
328
  #
166
329
  # @param workflow_slot_supplier [SlotSupplier] Slot supplier for workflows.
167
330
  # @param activity_slot_supplier [SlotSupplier] Slot supplier for activities.
168
331
  # @param local_activity_slot_supplier [SlotSupplier] Slot supplier for local activities.
332
+ # @param custom_slot_supplier_thread_pool [ThreadPool, nil] Thread pool to make all custom slot supplier calls on.
333
+ # If there are no custom slot suppliers, this parameter is ignored. Technically users may set this to nil which
334
+ # will not use a thread pool to make slot supplier calls, but that is dangerous and not advised because even the
335
+ # slightest blocking call can slow down the system.
169
336
  def initialize(
170
337
  workflow_slot_supplier:,
171
338
  activity_slot_supplier:,
172
- local_activity_slot_supplier:
339
+ local_activity_slot_supplier:,
340
+ custom_slot_supplier_thread_pool: ThreadPool.default
173
341
  )
174
342
  @workflow_slot_supplier = workflow_slot_supplier
175
343
  @activity_slot_supplier = activity_slot_supplier
176
344
  @local_activity_slot_supplier = local_activity_slot_supplier
345
+ @custom_slot_supplier_thread_pool = custom_slot_supplier_thread_pool
177
346
  end
178
347
 
179
348
  # @!visibility private
180
349
  def _to_bridge_options
181
350
  Internal::Bridge::Worker::TunerOptions.new(
182
- workflow_slot_supplier: workflow_slot_supplier._to_bridge_options,
183
- activity_slot_supplier: activity_slot_supplier._to_bridge_options,
184
- local_activity_slot_supplier: local_activity_slot_supplier._to_bridge_options
351
+ workflow_slot_supplier: workflow_slot_supplier._to_bridge_options(self),
352
+ activity_slot_supplier: activity_slot_supplier._to_bridge_options(self),
353
+ local_activity_slot_supplier: local_activity_slot_supplier._to_bridge_options(self)
185
354
  )
186
355
  end
187
356
  end
@@ -7,6 +7,7 @@ module Temporalio
7
7
  :continued_run_id,
8
8
  :cron_schedule,
9
9
  :execution_timeout,
10
+ :first_execution_run_id,
10
11
  :headers,
11
12
  :last_failure,
12
13
  :last_result,
@@ -35,6 +36,8 @@ module Temporalio
35
36
  # @return [String, nil] Cron schedule if applicable.
36
37
  # @!attribute execution_timeout
37
38
  # @return [Float, nil] Execution timeout for the workflow.
39
+ # @!attribute first_execution_run_id
40
+ # @return [String] The very first run ID the workflow ever had, following continuation chains.
38
41
  # @!attribute headers
39
42
  # @return [Hash<String, Api::Common::V1::Payload>] Headers.
40
43
  # @!attribute last_failure
@@ -208,14 +208,16 @@ module Temporalio
208
208
  #
209
209
  # @param activity [Class<Activity::Definition>, Symbol, String] Activity definition class or name.
210
210
  # @param args [Array<Object>] Arguments to the activity.
211
+ # @param summary [String, nil] Single-line summary for this activity that may appear in CLI/UI. This can be in
212
+ # single-line Temporal markdown format. This is currently experimental.
211
213
  # @param schedule_to_close_timeout [Float, nil] Max amount of time the activity can take from first being scheduled
212
214
  # to being completed before it times out. This is inclusive of all retries.
213
215
  # @param schedule_to_start_timeout [Float, nil] Max amount of time the activity can take to be started from first
214
216
  # being scheduled.
215
217
  # @param start_to_close_timeout [Float, nil] Max amount of time a single activity run can take from when it starts
216
218
  # to when it completes. This is per retry.
217
- # @param retry_policy [RetryPolicy] How an activity is retried on failure. If unset, a server-defined default is
218
- # used. Set maximum attempts to 1 to disable retries.
219
+ # @param retry_policy [RetryPolicy, nil] How an activity is retried on failure. If unset, a default policy is used.
220
+ # Set maximum attempts to 1 to disable retries.
219
221
  # @param local_retry_threshold [Float, nil] If the activity is retrying and backoff would exceed this value, a timer
220
222
  # is scheduled and the activity is retried after. Otherwise, backoff will happen internally within the task.
221
223
  # Defaults to 1 minute.
@@ -238,6 +240,7 @@ module Temporalio
238
240
  def self.execute_local_activity(
239
241
  activity,
240
242
  *args,
243
+ summary: nil,
241
244
  schedule_to_close_timeout: nil,
242
245
  schedule_to_start_timeout: nil,
243
246
  start_to_close_timeout: nil,
@@ -251,7 +254,7 @@ module Temporalio
251
254
  )
252
255
  _current.execute_local_activity(
253
256
  activity, *args,
254
- schedule_to_close_timeout:, schedule_to_start_timeout:, start_to_close_timeout:,
257
+ summary:, schedule_to_close_timeout:, schedule_to_start_timeout:, start_to_close_timeout:,
255
258
  retry_policy:, local_retry_threshold:, cancellation:, cancellation_type:,
256
259
  activity_id:, arg_hints:, result_hint:
257
260
  )
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: temporalio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.6.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Temporal Technologies Inc
@@ -160,6 +160,7 @@ files:
160
160
  - lib/temporalio/converters/payload_converter/json_plain.rb
161
161
  - lib/temporalio/converters/payload_converter/json_protobuf.rb
162
162
  - lib/temporalio/converters/raw_value.rb
163
+ - lib/temporalio/env_config.rb
163
164
  - lib/temporalio/error.rb
164
165
  - lib/temporalio/error/failure.rb
165
166
  - lib/temporalio/internal.rb