activejob-temporal 0.5.1 → 0.6.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f88f38d49d2e111cfcbdfe5bc2f45fb4ab9f202478e8e248dda74bce56925205
4
- data.tar.gz: a560b2d0e446dd867915298e75faabe60eb2c1f07bd0592cf7b3be4011ad07d9
3
+ metadata.gz: 19c8c05e4a84c73f492cdf9cf4e036cc9c8d594a1c4f056fda311a3f6cd94b81
4
+ data.tar.gz: a960f7c2847189c3d98be6c24ab3257ca10cc136119d541e45093dc5a0eff496
5
5
  SHA512:
6
- metadata.gz: 885994ed9a18b4b071736502f73e9cd379a212a23739eea1a1d3308b2634261f273b82070aa154504f99a757b30ba2135b05696253816dbac2a67ea02334403a
7
- data.tar.gz: '092927525f27f3ce13f07608f098dd53b87c2daf2665d98d3d6fc11ad4f7e243cd9701edf7dfa6a14130a89097ed04408706932ab5c59f5484803fb399b477e5'
6
+ metadata.gz: eea84b5517715db5a075937ad8dccf982502b7eddb4f4c398a6d206bca73532b678aa5133dabc9b0330c02168f0f16081d4f0b728346d062e0263206203a12eb
7
+ data.tar.gz: c7becbbfa48650bf42a0b86aa9d91229d7183aed7749219b7746c7721d483f4f61b8397f627569faed245572573a8802df4ebf89935bd29d36de040657557150
data/CHANGELOG.md CHANGED
@@ -7,6 +7,20 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
7
7
 
8
8
  ## [Unreleased]
9
9
 
10
+ ## [0.6.0] - 2026-11-18
11
+
12
+ ### Added
13
+ - `priority_resolver` sets native Temporal task queue priority and fairness (`Temporalio::Priority`) for enqueued jobs and recurring schedules. Activities, chain steps and child jobs inherit the root workflow's priority. Fairness needs `matching.enableFairness` on a self-hosted server.
14
+ - `ActiveJob::Temporal.stuck_jobs` and `count_stuck_jobs` find running job workflows with repeated workflow task failures or timeouts, filtered by job class and queue. Requires Temporal Server 1.30+ with `system.numConsecutiveWorkflowTaskProblemsToTriggerSearchAttribute` set to a positive value. See [troubleshooting](docs/troubleshooting.md#jobs-stuck-after-a-deploy-workflow-task-failures).
15
+ - `temporal_standalone_activity` runs a job class directly as a Temporal standalone activity, with no wrapping workflow. Requires Temporal Server 1.32+ with `activity.enableStandalone`, and temporalio 1.5+ on enqueuers and workers. The SDK API is experimental. Standalone jobs skip the dead letter queue and reject rate limits, chains, dependencies, child workflows, custom workflow IDs, signals, queries, updates and pause. See [usage patterns](docs/usage_patterns.md#standalone-activities).
16
+
17
+ ### Changed
18
+ - The temporalio constraint is now `>= 1.4.1, < 1.12`. Schedule actions need 1.4.1 to carry priority.
19
+ - Local development and CI run Temporal Server 1.32.2 (`temporalio/temporal` CLI dev server on SQLite) instead of 1.29 on Postgres. `tools/provision_temporal_test_namespace.sh` creates the `test` namespace for integration tests.
20
+
21
+ ### Documentation
22
+ - [Nexus guide](docs/nexus.md): the Ruby SDK cannot serve Nexus operations yet. Serve them from another SDK with a workflow-run operation that starts the Ruby workflow on its task queue.
23
+
10
24
  ## [0.5.1] - 2026-11-11
11
25
 
12
26
  ### Changed
data/CLAUDE.md CHANGED
@@ -82,13 +82,16 @@ Use YARD annotations extensively:
82
82
  ### Temporal Setup (Local Development)
83
83
 
84
84
  ```bash
85
- # Start Temporal server, UI, and PostgreSQL
86
- docker-compose up
85
+ # Start Temporal Server 1.32.2, Temporal UI, and Toxiproxy
86
+ docker compose up -d
87
87
 
88
88
  # Temporal UI: http://localhost:8080
89
89
  # Server: localhost:7233
90
90
  ```
91
91
 
92
+ The root stack stores Temporal state in a SQLite volume. It starts with a fresh database when upgrading from the Postgres stack.
93
+ Before running integration tests, run `tools/provision_temporal_test_namespace.sh`. It creates the `test` namespace, registers its search attributes, and waits until workflows can use it.
94
+
92
95
  ### Running a Worker
93
96
 
94
97
  The `temporal-worker` executable uses smart Rails auto-detection:
data/README.md CHANGED
@@ -13,6 +13,9 @@ This gem is under active development. Expect breaking changes until v1.0.0.
13
13
 
14
14
  `activejob-temporal` runs Rails ActiveJob work through [Temporal](https://temporal.io) workflows and activities. It is intended for jobs where durable execution, crash recovery, long-running retries, cancellation visibility, and Temporal UI history matter more than queue simplicity.
15
15
 
16
+ - Set [native task queue priority and fairness](docs/configuration_reference.md#task-queue-priority-and-fairness) for jobs and recurring schedules.
17
+ - [Detect stuck jobs](docs/usage_patterns.md#alert-on-stuck-jobs) after repeated workflow task failures or timeouts.
18
+
16
19
  Use a traditional ActiveJob backend when the work is short, simple, and does not justify operating Temporal.
17
20
 
18
21
  ## Requirements
@@ -21,7 +24,7 @@ Use a traditional ActiveJob backend when the work is short, simple, and does not
21
24
  - Rails >= 7.2 through ActiveJob 8.x
22
25
  - Temporal cluster, either self-hosted or [Temporal Cloud](https://temporal.io/cloud)
23
26
 
24
- Temporal Ruby SDK 1.4.x compatibility is contract-tested against 1.4.0 and 1.4.1.
27
+ Temporal Ruby SDK 1.4.1 is the minimum supported version and runs in the SDK contract tests.
25
28
 
26
29
  ## Install
27
30
 
@@ -130,6 +133,29 @@ Open Temporal UI and look for workflows named `ajwf:SendInvoiceJob:<job_id>`.
130
133
 
131
134
  Baseline behavior also includes transaction-aware enqueueing through ActiveJob, GlobalID-compatible argument serialization, structured JSON logs, searchable `set(tags:)` metadata, and JSON payloads with opt-in MessagePack or Marshal envelopes.
132
135
 
136
+ ## Standalone Activities
137
+
138
+ Opt in per class to run the job directly as a Temporal activity. Subclasses inherit the flag and can opt out with `temporal_standalone_activity false`.
139
+
140
+ ```ruby
141
+ class WebhookJob < ApplicationJob
142
+ temporal_standalone_activity
143
+ temporal_options start_to_close_timeout: 30.seconds
144
+ retry_on Net::ReadTimeout, attempts: 5
145
+ end
146
+
147
+ WebhookJob.set(wait: 5.minutes).perform_later(webhook_id)
148
+ ```
149
+
150
+ | Standalone support | Features |
151
+ | --- | --- |
152
+ | Works | `perform_later`, batch enqueue, `set(wait:)`, `retry_on`, `discard_on`, timeouts, priority, idempotency keys, search attributes, `cancel`, `Inspect.status` |
153
+ | Skipped | Dead letter queue and its attempt limit. The adapter logs one warning per job class per process. |
154
+ | Rejected | Rate limits, chains, dependencies, child workflows, custom workflow identity, signals, queries, updates, and pause |
155
+ | Workflow only | Recurring schedules, `cancel_all`, and `cancel_where` |
156
+
157
+ See [worker requirements](docs/worker_setup.md#standalone-activity-requirements).
158
+
133
159
  ## Configuration
134
160
 
135
161
  The full configuration surface lives in [Configuration Reference](docs/configuration_reference.md). The machine-readable schema is [docs/config_schema.yaml](docs/config_schema.yaml).
@@ -37,7 +37,7 @@ Gem::Specification.new do |spec|
37
37
  spec.add_dependency "activemodel", ">= 7.2", "< 9"
38
38
  spec.add_dependency "concurrent-ruby", "~> 1.1"
39
39
  spec.add_dependency "globalid", ">= 0.3"
40
- spec.add_dependency "temporalio", ">= 1.4.0", "< 1.12"
40
+ spec.add_dependency "temporalio", ">= 1.4.1", "< 1.12"
41
41
 
42
42
  # listen is an optional runtime dependency, required lazily only when credential_file_events is
43
43
  # enabled. Without it credentials still rotate, just on the poll interval instead of instantly.
@@ -22,13 +22,12 @@ module ActiveJob
22
22
  # Activities may be re-executed on transient failures due to Temporal's retry logic.
23
23
  # Job implementations MUST be idempotent. This activity sets an execution-local
24
24
  # idempotency key (`Fiber[:aj_temporal_idempotency_key]`) derived from the
25
- # workflow ID to assist with idempotent external operations (e.g., API requests).
25
+ # workflow or standalone activity ID to assist with idempotent external operations.
26
26
  #
27
27
  # @note Execution-Local Idempotency Key
28
28
  # Jobs can access `Fiber[:aj_temporal_idempotency_key]` to generate unique
29
29
  # idempotency tokens for external API calls. `Thread.current[...]` remains
30
- # populated for existing synchronous jobs. The key format is "workflow_id/runner"
31
- # and persists across retries for the same workflow execution.
30
+ # populated for existing synchronous jobs. The key ends in "/runner" and persists across retries.
32
31
  #
33
32
  # @note Exception Handling
34
33
  # If the job raises an exception that matches a `discard_on` declaration,
@@ -195,8 +194,10 @@ module ActiveJob
195
194
  def apply_schedule_execution_identity(payload)
196
195
  return unless payload_value(payload, :schedule_id)
197
196
 
198
- execution_job_id = payload_value(payload, :schedule_execution_job_id) || activity_workflow_execution_identity
199
- return unless execution_job_id
197
+ execution_identity = activity_workflow_execution_identity
198
+ return unless execution_identity
199
+
200
+ execution_job_id = payload_value(payload, :schedule_execution_job_id) || execution_identity
200
201
 
201
202
  payload[:job_id] = execution_job_id
202
203
  active_job_payload = payload[:active_job] || payload["active_job"]
@@ -341,8 +342,10 @@ module ActiveJob
341
342
 
342
343
  # @api private
343
344
  def set_idempotency_key(payload = nil)
344
- identity = payload && payload_value(payload, :schedule_execution_job_id)
345
- identity ||= activity_workflow_id || "unknown-workflow"
345
+ identity = activity_workflow_id || "unknown-workflow"
346
+ if payload && payload_value(payload, :schedule_id) && activity_workflow_execution_identity
347
+ identity = payload_value(payload, :schedule_execution_job_id) || identity
348
+ end
346
349
  idempotency_key = "#{identity}/runner"
347
350
  Thread.current[IDEMPOTENCY_KEY] = idempotency_key
348
351
  Fiber[IDEMPOTENCY_KEY] = idempotency_key
@@ -359,18 +362,20 @@ module ActiveJob
359
362
  return unless defined?(Temporalio::Activity::Context) && Temporalio::Activity::Context.exist?
360
363
 
361
364
  info = Temporalio::Activity::Context.current.info
362
- namespace = if info.respond_to?(:workflow_namespace)
363
- info.workflow_namespace
364
- else
365
- ActiveJob::Temporal.config.namespace
366
- end
367
- { namespace: namespace, workflow_id: encryption_context_workflow_id(payload) }
365
+ { namespace: activity_namespace(info), workflow_id: encryption_context_workflow_id(info, payload) }
366
+ end
367
+
368
+ def activity_namespace(info)
369
+ workflow_namespace = info.workflow_namespace if info.respond_to?(:workflow_namespace)
370
+ workflow_namespace || (info.namespace if info.respond_to?(:namespace)) || ActiveJob::Temporal.config.namespace
368
371
  end
369
372
 
370
373
  # Scheduled payloads are encrypted once against the schedule's workflow-ID prefix, while
371
374
  # each occurrence runs under a longer workflow ID derived from that prefix.
372
- def encryption_context_workflow_id(payload)
373
- workflow_id = activity_workflow_id
375
+ def encryption_context_workflow_id(info, payload)
376
+ workflow_id = info.workflow_id.presence
377
+ return info.activity_id unless workflow_id
378
+
374
379
  prefix = payload && payload_value(payload, :schedule_workflow_id_prefix)
375
380
  return workflow_id unless prefix && workflow_id.to_s.start_with?(prefix.to_s)
376
381
 
@@ -380,13 +385,16 @@ module ActiveJob
380
385
  def activity_workflow_id
381
386
  return unless defined?(Temporalio::Activity::Context) && Temporalio::Activity::Context.exist?
382
387
 
383
- Temporalio::Activity::Context.current.info.workflow_id
388
+ info = Temporalio::Activity::Context.current.info
389
+ info.workflow_id.presence || info.activity_id
384
390
  end
385
391
 
386
392
  def activity_workflow_execution_identity
387
393
  return unless defined?(Temporalio::Activity::Context) && Temporalio::Activity::Context.exist?
388
394
 
389
395
  info = Temporalio::Activity::Context.current.info
396
+ return unless info.workflow_id.present?
397
+
390
398
  [info.workflow_id, activity_workflow_run_id(info)].compact.join(":")
391
399
  end
392
400
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "active_job"
4
4
  require "active_job/queue_adapters/abstract_adapter"
5
+ require "temporalio/priority"
5
6
  require_relative "workflow_id_builder"
6
7
 
7
8
  module ActiveJob
@@ -72,6 +73,22 @@ module ActiveJob
72
73
  "#{prefix}#{queue_name}"
73
74
  end
74
75
 
76
+ # Resolves native Temporal priority for a workflow start or schedule action.
77
+ #
78
+ # @param job [ActiveJob::Base] Job to enqueue or schedule
79
+ # @param config [ActiveJob::Temporal::Configuration] Adapter configuration
80
+ # @return [Temporalio::Priority, nil] Resolved priority or nil for server defaults
81
+ # @raise [ConfigurationError] If the resolver returns an unsupported value
82
+ def resolve_priority(job, config: ActiveJob::Temporal.config)
83
+ resolver = config.priority_resolver if config.respond_to?(:priority_resolver)
84
+ return if resolver.nil?
85
+
86
+ priority = resolver.call(job)
87
+ return priority if priority.nil? || priority.is_a?(Temporalio::Priority)
88
+
89
+ raise ConfigurationError, "priority_resolver must return Temporalio::Priority or nil, got #{priority.class}"
90
+ end
91
+
75
92
  def priority_task_queue(job, config)
76
93
  priority_task_queues = config.respond_to?(:priority_task_queues) ? config.priority_task_queues : {}
77
94
  return unless priority_task_queues.is_a?(Hash) && priority_task_queues.any?
@@ -88,8 +88,8 @@ module ActiveJob
88
88
 
89
89
  activity_info = Temporalio::Activity::Context.current.info
90
90
  {
91
- workflow_id: context_value(activity_info, :workflow_id),
92
- run_id: context_value(activity_info, :workflow_run_id, :run_id),
91
+ workflow_id: context_value(activity_info, :workflow_id, :activity_id),
92
+ run_id: context_value(activity_info, :workflow_run_id, :activity_run_id, :run_id),
93
93
  attempt: context_value(activity_info, :attempt)
94
94
  }.compact
95
95
  rescue StandardError
@@ -98,7 +98,8 @@ module ActiveJob
98
98
 
99
99
  def context_value(object, *methods)
100
100
  methods.each do |method_name|
101
- return object.public_send(method_name) if object.respond_to?(method_name)
101
+ value = object.public_send(method_name) if object.respond_to?(method_name)
102
+ return value if value.present?
102
103
  end
103
104
 
104
105
  nil
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "temporalio/error"
4
4
  require_relative "job_id_validation"
5
+ require_relative "standalone_activity"
5
6
  require_relative "visibility_query"
6
7
  require_relative "workflow_id_builder"
7
8
 
@@ -111,38 +112,76 @@ module ActiveJob
111
112
  def cancel(job_class, job_id)
112
113
  validate_job_id!(job_id)
113
114
  client = ActiveJob::Temporal.client
115
+ if StandaloneActivity.enabled?(job_class)
116
+ result = cancel_activity(client, job_class, job_id)
117
+ return false if result == :closed
118
+ return nil if result == :requested
119
+
120
+ cancel_workflow(client, job_class, job_id)
121
+ else
122
+ cancel_workflow_or_activity(client, job_class, job_id)
123
+ end
124
+ end
125
+
126
+ # Cancels workflows that match a job class.
127
+ # @note Standalone activities require an individual cancel request.
128
+ def cancel_all(job_class)
129
+ validate_job_class!(job_class)
130
+
131
+ cancel_where(ajClass: job_class.name)
132
+ end
133
+
134
+ # @note Filters apply to workflows only.
135
+ def cancel_where(filters)
136
+ BatchCanceller.new(ActiveJob::Temporal.client).cancel_where(filters)
137
+ end
138
+ end
139
+
140
+ class << self
141
+ private
142
+
143
+ def cancel_workflow_or_activity(client, job_class, job_id)
144
+ cancel_workflow(client, job_class, job_id)
145
+ rescue WorkflowNotFoundError
146
+ case cancel_activity(client, job_class, job_id)
147
+ when :closed then false
148
+ when :requested then nil
149
+ else raise
150
+ end
151
+ end
152
+
153
+ def cancel_workflow(client, job_class, job_id)
114
154
  return nil if cancel_schedule_execution(client, job_class, job_id)
115
155
 
116
156
  workflow_state = find_workflow(client, job_class, job_id)
117
157
  workflow_id = workflow_state[:workflow_id]
118
-
119
158
  case workflow_state[:status]
120
159
  when :closed
121
160
  log_workflow_already_completed(job_class, job_id, workflow_id)
122
161
  false
123
- when :not_found
124
- raise ActiveJob::Temporal::WorkflowNotFoundError,
125
- "No workflow found for job_id #{job_id}. The job may have never existed."
126
162
  when :running
127
163
  request_cancellation(client, job_id, workflow_id)
128
- log_cancellation_requested(job_class, job_id, workflow_id)
129
- log_audit_cancellation_requested(job_class, job_id, workflow_id)
164
+ cancellation_requested(job_class, job_id, workflow_id)
130
165
  nil
166
+ when :not_found
167
+ raise WorkflowNotFoundError, "No workflow found for job_id #{job_id}. The job may have never existed."
131
168
  end
132
169
  end
133
170
 
134
- def cancel_all(job_class)
135
- validate_job_class!(job_class)
136
-
137
- cancel_where(ajClass: job_class.name)
138
- end
171
+ def cancel_activity(client, job_class, job_id)
172
+ StandaloneActivity.with_handle(client, job_class, job_id) do |handle|
173
+ next :closed unless StandaloneActivity.state(handle.describe.status) == :running
139
174
 
140
- def cancel_where(filters)
141
- BatchCanceller.new(ActiveJob::Temporal.client).cancel_where(filters)
175
+ handle.cancel
176
+ cancellation_requested(job_class, job_id, handle.id)
177
+ :requested
178
+ end
179
+ rescue StandardError => e
180
+ raise ActiveJob::Temporal::TemporalConnectionError.new(
181
+ "Failed to cancel Temporal standalone activity for job_id #{job_id}: #{e.message}"
182
+ ), cause: e
142
183
  end
143
184
 
144
- private
145
-
146
185
  def validate_job_class!(job_class)
147
186
  return if job_class.respond_to?(:name) && !job_class.name.to_s.empty?
148
187
 
@@ -162,8 +201,7 @@ module ActiveJob
162
201
 
163
202
  workflow_id = schedule_reference.fetch(:workflow_id)
164
203
  request_cancellation(client, job_id, workflow_id, run_id: schedule_reference.fetch(:run_id))
165
- log_cancellation_requested(job_class, job_id, workflow_id)
166
- log_audit_cancellation_requested(job_class, job_id, workflow_id)
204
+ cancellation_requested(job_class, job_id, workflow_id)
167
205
  true
168
206
  rescue ActiveJob::Temporal::WorkflowNotFoundError
169
207
  false
@@ -235,13 +273,20 @@ module ActiveJob
235
273
 
236
274
  # Logs cancellation request event.
237
275
  # @api private
238
- def log_cancellation_requested(job_class, job_id, workflow_id)
276
+ def cancellation_requested(job_class, job_id, workflow_id)
239
277
  ActiveJob::Temporal::Logger.info(
240
278
  "cancellation_requested",
241
279
  workflow_id: workflow_id,
242
280
  job_class: job_class.name,
243
281
  job_id: job_id
244
282
  )
283
+ ActiveJob::Temporal::AuditLog.record(
284
+ "job.cancelled",
285
+ workflow_id: workflow_id,
286
+ job_class: job_class.name,
287
+ job_id: job_id,
288
+ status: "requested"
289
+ )
245
290
  end
246
291
 
247
292
  # Logs event when attempting to cancel an already-completed workflow.
@@ -255,16 +300,6 @@ module ActiveJob
255
300
  status: "completed"
256
301
  )
257
302
  end
258
-
259
- def log_audit_cancellation_requested(job_class, job_id, workflow_id)
260
- ActiveJob::Temporal::AuditLog.record(
261
- "job.cancelled",
262
- workflow_id: workflow_id,
263
- job_class: job_class.name,
264
- job_id: job_id,
265
- status: "requested"
266
- )
267
- end
268
303
  end
269
304
  end
270
305
  end
@@ -181,6 +181,12 @@ module ActiveJob
181
181
  description: "Optional callable for custom Temporal workflow IDs"
182
182
  },
183
183
 
184
+ priority_resolver: {
185
+ default: nil,
186
+ type: :callable,
187
+ description: "Optional callable(job) returning Temporalio::Priority for task queue priority and fairness"
188
+ },
189
+
184
190
  rate_limiter: {
185
191
  default: nil,
186
192
  type: :object,
@@ -642,6 +648,7 @@ module ActiveJob
642
648
  # - `max_concurrent_activities`: Numericality > 0
643
649
  # - `max_concurrent_workflow_tasks`: Numericality > 0
644
650
  # - `workflow_id_generator`: Optional callable
651
+ # - `priority_resolver`: Optional callable
645
652
  # - `middleware_chain`: Callable chain with registration support
646
653
  #
647
654
  # @example Using ConfigValidator directly
@@ -752,6 +759,7 @@ module ActiveJob
752
759
  validate :validate_namespace_format
753
760
  validate :validate_duration_values
754
761
  validate :validate_workflow_id_generator
762
+ validate :validate_priority_resolver
755
763
  validate :validate_middleware_chain
756
764
  validate :validate_priority_task_queues
757
765
  validate :validate_rate_limit_settings
@@ -852,6 +860,21 @@ module ActiveJob
852
860
  errors.add(:workflow_id_generator, :wrong_arity, value: workflow_id_generator.inspect)
853
861
  end
854
862
 
863
+ def validate_priority_resolver
864
+ return if priority_resolver.nil?
865
+
866
+ unless priority_resolver.respond_to?(:call)
867
+ errors.add(:priority_resolver, :not_callable, value: priority_resolver.inspect)
868
+ return
869
+ end
870
+
871
+ accepts_job = callable_accepts_positional_job?(priority_resolver)
872
+ requires_keywords = callable_parameters(priority_resolver)&.any? { |type, _name| type == :keyreq }
873
+ return if accepts_job && !requires_keywords
874
+
875
+ errors.add(:priority_resolver, :wrong_arity, value: priority_resolver.inspect)
876
+ end
877
+
855
878
  def validate_middleware_chain
856
879
  return if middleware_chain.respond_to?(:add) && middleware_chain.respond_to?(:call)
857
880
 
@@ -20,7 +20,7 @@ module ActiveJob
20
20
  end
21
21
 
22
22
  def entries(queue: nil, limit: DEFAULT_ENTRIES_LIMIT, client: ActiveJob::Temporal.client)
23
- validate_limit!(limit)
23
+ VisibilityQuery.validate_limit!(limit)
24
24
 
25
25
  workflows = client.list_workflows(entries_query(queue)).first(limit)
26
26
  query_workflow_entries(client, workflows)
@@ -186,13 +186,6 @@ module ActiveJob
186
186
  end
187
187
  private_class_method :workflow_run_id
188
188
 
189
- def validate_limit!(limit)
190
- return if limit.is_a?(Integer) && limit.positive?
191
-
192
- raise ArgumentError, "limit must be a positive integer"
193
- end
194
- private_class_method :validate_limit!
195
-
196
189
  def workflow_already_started?(error)
197
190
  (defined?(Temporalio::Error::WorkflowAlreadyStartedError) &&
198
191
  error.is_a?(Temporalio::Error::WorkflowAlreadyStartedError)) ||
@@ -3,6 +3,7 @@
3
3
  require "temporalio/client/workflow_execution_status"
4
4
  require "temporalio/error"
5
5
  require_relative "job_id_validation"
6
+ require_relative "standalone_activity"
6
7
  require_relative "visibility_query"
7
8
  require_relative "workflow_id_builder"
8
9
 
@@ -26,9 +27,11 @@ module ActiveJob
26
27
  validate_job_id!(job_id)
27
28
 
28
29
  client = ActiveJob::Temporal.client
29
- describe_schedule_execution_workflow(client, job_id) ||
30
- describe_default_workflow(client, job_class, job_id) ||
31
- describe_search_attribute_workflow(client, job_class, job_id)
30
+ if StandaloneActivity.enabled?(job_class)
31
+ StandaloneActivity.status(client, job_class, job_id) || workflow_status(client, job_class, job_id)
32
+ else
33
+ workflow_status(client, job_class, job_id) || StandaloneActivity.status(client, job_class, job_id)
34
+ end
32
35
  rescue ArgumentError
33
36
  raise
34
37
  rescue StandardError => e
@@ -64,6 +67,12 @@ module ActiveJob
64
67
  JobIdValidation.validate!(job_id)
65
68
  end
66
69
 
70
+ def workflow_status(client, job_class, job_id)
71
+ describe_schedule_execution_workflow(client, job_id) ||
72
+ describe_default_workflow(client, job_class, job_id) ||
73
+ describe_search_attribute_workflow(client, job_class, job_id)
74
+ end
75
+
67
76
  def describe_default_workflow(client, job_class, job_id)
68
77
  describe_workflow(client, default_workflow_reference(job_class, job_id))
69
78
  rescue StandardError => e
@@ -1,5 +1,8 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "concurrent/map"
4
+
5
+ require_relative "logger"
3
6
  require_relative "payload"
4
7
  require_relative "job_payload_child_workflows"
5
8
  require_relative "job_payload_chain_builder"
@@ -26,33 +29,74 @@ module ActiveJob
26
29
  default_heartbeat_timeout: :heartbeat_timeout
27
30
  }.freeze
28
31
 
32
+ STANDALONE_INCOMPATIBLE_KEYS = %i[
33
+ rate_limits chain child_workflows dependencies workflow_interactions workflow_identity
34
+ ].freeze
35
+ DLQ_WARNED_JOB_CLASSES = Concurrent::Map.new
36
+
29
37
  def initialize(config)
30
38
  @config = config
31
39
  end
32
40
 
33
- def build(job, scheduled_at: nil, encryption_context: nil)
41
+ def build(job, scheduled_at: nil, encryption_context: nil, standalone: false)
34
42
  payload = base_payload_for(job, scheduled_at)
35
43
  payload[:default_activity_options] = default_activity_options
36
44
  Observability.inject_trace_context(payload, observability_attributes_for(job, encryption_context))
37
45
 
38
- apply_retry_policy(payload, job)
46
+ apply_retry_policy(payload, job, standalone: standalone)
39
47
  apply_temporal_options(payload, job.class)
40
48
  apply_workflow_identity(payload, job.class)
41
- apply_rate_limits(payload, job)
49
+ apply_rate_limits(payload, job, standalone: standalone)
42
50
  apply_workflow_interactions(payload, job.class)
43
51
  apply_child_workflows(payload, job)
44
52
  apply_chain(payload, job)
45
53
  apply_dependencies(payload, job)
46
- apply_continue_as_new(payload)
47
- apply_local_activity_helpers(payload)
54
+ apply_execution_options(payload, job.class, standalone: standalone)
48
55
 
49
56
  payload = transport_payload(payload, job, scheduled_at, encryption_context)
50
57
  Payload.enforce_size!(payload, metrics_payload: metrics_payload_for(job), config: @config)
51
58
  payload
52
59
  end
53
60
 
61
+ # @return [Hash] Options for Client#start_activity
62
+ def standalone_activity_options(job)
63
+ options = default_activity_options
64
+ options[:start_to_close_timeout] = 900.0 if options.empty?
65
+ options.merge!(extract_temporal_options(job.class))
66
+ options[:retry_policy] = RetryMapper.temporal_policy(RetryMapper.for(job.class, config: @config))
67
+ options
68
+ end
69
+
54
70
  private
55
71
 
72
+ def apply_execution_options(payload, job_class, standalone:)
73
+ return validate_standalone!(payload, job_class) if standalone
74
+
75
+ apply_continue_as_new(payload)
76
+ apply_local_activity_helpers(payload)
77
+ end
78
+
79
+ def validate_standalone!(payload, job_class)
80
+ incompatible_keys = STANDALONE_INCOMPATIBLE_KEYS & payload.keys
81
+ incompatible_keys |= [:workflow_identity] if custom_workflow_identity?(job_class)
82
+ return if incompatible_keys.empty?
83
+
84
+ raise ConfigurationError,
85
+ "#{job_class.name} standalone activities do not support: #{incompatible_keys.join(', ')}"
86
+ end
87
+
88
+ def custom_workflow_identity?(job_class)
89
+ extract_temporal_workflow_id_prefix(job_class) ||
90
+ (job_class.respond_to?(:temporal_workflow_id) && job_class.temporal_workflow_id) ||
91
+ (@config.respond_to?(:workflow_id_generator) && @config.workflow_id_generator)
92
+ end
93
+
94
+ def warn_standalone_dlq(job_class)
95
+ return if DLQ_WARNED_JOB_CLASSES.put_if_absent(job_class.name, true)
96
+
97
+ Logger.log_to(@config.logger, :warn, "standalone_activity_dlq_skipped", job_class: job_class.name)
98
+ end
99
+
56
100
  def base_payload_for(job, scheduled_at)
57
101
  Payload.from_job(
58
102
  job,
@@ -64,7 +108,12 @@ module ActiveJob
64
108
  )
65
109
  end
66
110
 
67
- def apply_retry_policy(payload, job)
111
+ def apply_retry_policy(payload, job, standalone:)
112
+ if standalone
113
+ warn_standalone_dlq(job.class) if dead_letter_enabled?
114
+ return
115
+ end
116
+
68
117
  retry_policy = retry_policy_for(job.class)
69
118
  payload[:retry_policy] = retry_policy
70
119
  return unless dead_letter_enabled?
@@ -7,17 +7,17 @@ module ActiveJob
7
7
  module JobPayloadRateLimits
8
8
  private
9
9
 
10
- def apply_rate_limits(payload, job)
11
- rate_limits = rate_limits_for(job.class)
10
+ def apply_rate_limits(payload, job, standalone: false)
11
+ rate_limits = rate_limits_for(job.class, validate_limiter: !standalone)
12
12
  payload[:rate_limits] = rate_limits if rate_limits.any?
13
13
  end
14
14
 
15
- def rate_limits_for(job_class)
15
+ def rate_limits_for(job_class, validate_limiter: true)
16
16
  rate_limits = [
17
17
  configured_global_rate_limit,
18
18
  configured_job_rate_limit(job_class)
19
19
  ].compact
20
- validate_rate_limiter!(rate_limits)
20
+ validate_rate_limiter!(rate_limits) if validate_limiter
21
21
  rate_limits
22
22
  end
23
23
 
@@ -73,6 +73,10 @@ en:
73
73
  not_callable: "must respond to #call (for example, ->(job) { \"custom-#{job.job_id}\" }), got: %{value}"
74
74
  wrong_arity: "must accept one positional ActiveJob argument (for example, ->(job) { \"custom-#{job.job_id}\" }), got: %{value}"
75
75
 
76
+ priority_resolver:
77
+ not_callable: "must respond to #call, got: %{value}"
78
+ wrong_arity: "must accept one positional ActiveJob argument without required keywords, got: %{value}"
79
+
76
80
  middleware_chain:
77
81
  invalid: "must respond to #add and #call, got: %{value}"
78
82