standard_procedure_operations 0.7.2 → 0.7.3

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: 462512e945573c5809f480e318e03c17a15604e8f8362934d2c730f2827f54a5
4
- data.tar.gz: 3446e47c75e17db5384b8ba8a86a5740fbd095ea6a3f57ae6eb9d05dbac50e1c
3
+ metadata.gz: 24336966a7874886f00207073fbe8f5f3f53d55bb7594c8c36d085f4c74dd369
4
+ data.tar.gz: 3bdf232858859b1003c7b90c911932aa5e4e7c321a0fefbcbcfc62c49801cda2
5
5
  SHA512:
6
- metadata.gz: 442ba282285358dc7baa1bbd9f00a4fa2aaa76ee4009a0638073f95e6e099c993d7eb0b9381884b781432874bd542a2a8fde4a6c150bd7c3fdf5028f94ac6951
7
- data.tar.gz: 6458262e8f3efd03256c1a4e6efe7be32d925ce88852afc93869d28b6ed7e77e6fb0108020ea1c39ab15ca79feaaa5eb2d45e129ce0829064c1b779d727e39eb
6
+ metadata.gz: 1398774410e430157a8cd45e874ca1cd631ccc3159d554d32b573d4e6f261504727b31aceaf05340c4a75f60d87309977485f163bcf79b1dc15fb7fa0a1d20d2
7
+ data.tar.gz: 7ee18d5f9889f902f74bc5277f71e72d1d029676bb39fe2dae7d3185accded3d3573f2b784233b21c4830b6422771ea9d47e3281975230c97eddd931c61623d2
data/README.md CHANGED
@@ -217,6 +217,18 @@ If you want the task to be run completely in the background (so it sleeps immedi
217
217
 
218
218
  [Example wait and interaction handlers](spec/examples/waiting_and_interactions_spec.rb)
219
219
 
220
+ Because background tasks are woken using ActiveJob, you may wish to control exactly how these jobs are handled.
221
+
222
+ You can specify which ActiveJob queue they are placed on (with the default value being `:default`) - this is the equivalent of setting `queue_as :my_queue` in ActiveJob. And you can even specify which queue adapter they use (if, for example, you want to use SolidQueue for most of your background jobs, but Sidekiq for a certain subset of tasks).
223
+
224
+ ```ruby
225
+ class MyBackgroundOperation < Operations::Task
226
+ queue :low_priority
227
+ runs_on :sidekiq
228
+ # ...
229
+ end
230
+ ```
231
+
220
232
  ### Sub tasks
221
233
 
222
234
  If your task needs to start sub-tasks, it can use the `start` method, passing the sub-task class and arguments.
@@ -298,10 +310,10 @@ class WeekendChecker < Operations::Task
298
310
  result :weekday
299
311
  end
300
312
 
301
- task = WeekendChecker.verify :is_it_the_weekend?, day_of_week: "Saturday"
313
+ task = WeekendChecker.test :is_it_the_weekend?, day_of_week: "Saturday"
302
314
  expect(task).to be_in :weekend
303
315
 
304
- task = WeekendChecker.verify :is_it_the_weekend?, day_of_week: "Wednesday"
316
+ task = WeekendChecker.test :is_it_the_weekend?, day_of_week: "Wednesday"
305
317
  expect(task).to be_in :weekday
306
318
  ```
307
319
 
@@ -1,5 +1,5 @@
1
1
  class Operations::WakeTaskJob < ApplicationJob
2
- queue_as :default
2
+ queue_as { arguments.first.class.queue_as }
3
3
 
4
4
  def perform(task) = task.wake_up!
5
5
  end
@@ -32,6 +32,12 @@ module Operations::Task::Plan
32
32
 
33
33
  def timeout(value) = @execution_timeout = value
34
34
 
35
+ def queue(value) = @queue_as = value
36
+
37
+ def runs_on(value) = @queue_adapter ||= value
38
+ alias_method :runs, :runs_on
39
+ alias_method :runs_using, :runs_on
40
+
35
41
  def delete_after(value) = @deletion_time = value
36
42
 
37
43
  def on_timeout(&handler) = @on_timeout = handler
@@ -40,6 +46,10 @@ module Operations::Task::Plan
40
46
 
41
47
  def execution_timeout = @execution_timeout ||= 24.hours
42
48
 
49
+ def queue_as = @queue_as ||= :default
50
+
51
+ def queue_adapter = @queue_adapter ||= Operations::WakeTaskJob.queue_adapter
52
+
43
53
  def timeout_handler = @on_timeout
44
54
 
45
55
  def deletion_time = @deletion_time ||= 90.days
@@ -5,17 +5,18 @@ module Operations
5
5
  include Index
6
6
  include Testing
7
7
 
8
+ scope :active, -> { where(status: %w[active waiting]) }
8
9
  scope :ready_to_wake, -> { ready_to_wake_at(Time.current) }
9
- scope :ready_to_wake_at, ->(time) { where(wakes_at: ..time) }
10
+ scope :ready_to_wake_at, ->(time) { waiting.where(wakes_at: ..time) }
10
11
  scope :expired, -> { expires_at(Time.current) }
11
- scope :expired_at, ->(time) { where(expires_at: ..time) }
12
+ scope :expired_at, ->(time) { waiting.where(expires_at: ..time) }
12
13
  scope :ready_to_delete, -> { ready_to_delete_at(Time.current) }
13
14
  scope :ready_to_delete_at, ->(time) { where(delete_at: ..time) }
14
15
 
15
16
  # Task hierarchy relationships
16
17
  belongs_to :parent, class_name: "Operations::Task", optional: true
17
18
  has_many :sub_tasks, class_name: "Operations::Task", foreign_key: "parent_id", dependent: :nullify
18
- has_many :active_sub_tasks, -> { where(status: ["active", "waiting"]) }, class_name: "Operations::Task", foreign_key: "parent_id"
19
+ has_many :active_sub_tasks, -> { active }, class_name: "Operations::Task", foreign_key: "parent_id"
19
20
  has_many :failed_sub_tasks, -> { failed }, class_name: "Operations::Task", foreign_key: "parent_id"
20
21
  has_many :completed_sub_tasks, -> { completed }, class_name: "Operations::Task", foreign_key: "parent_id"
21
22
 
@@ -30,14 +31,14 @@ module Operations
30
31
  while active? && (state != current_state)
31
32
  state = current_state
32
33
  Rails.logger.debug { "--- #{self}: #{current_state}" }
33
- (handler_for(current_state).immediate? || immediate) ? call_handler : go_to_sleep!
34
+ (immediate || state_is_immediate?(current_state)) ? call_handler : go_to_sleep!
34
35
  end
35
36
  rescue => ex
36
37
  record_error! ex
37
38
  raise ex
38
39
  end
39
40
 
40
- def go_to(next_state) = update! current_state: next_state
41
+ def go_to(next_state) = update! current_state: next_state, task_status: (state_is_immediate?(next_state) ? "active" : "waiting")
41
42
 
42
43
  def wake_up! = timeout_expired? ? call_timeout_handler : activate_and_call
43
44
 
@@ -47,6 +48,8 @@ module Operations
47
48
 
48
49
  def call_handler = handler_for(current_state).call(self)
49
50
 
51
+ private def state_is_immediate?(state) = handler_for(state).immediate?
52
+
50
53
  private def go_to_sleep! = update!(default_times.merge(task_status: "waiting"))
51
54
 
52
55
  private def activate_and_call
@@ -61,16 +64,34 @@ module Operations
61
64
  raise ex
62
65
  end
63
66
 
64
- def self.call(task_status: "active", **attributes) = create!(attributes.merge(task_status: task_status, current_state: initial_state).merge(default_times)).tap { |t| t.call }
65
-
66
- def self.later(**attributes) = call(task_status: "waiting", **attributes)
67
-
68
- def self.perform_now(...) = call(...)
69
-
70
- def self.perform_later(...) = later(...)
71
-
72
- def self.wake_sleeping = Task.ready_to_wake.find_each { |t| Operations::WakeTaskJob.perform_later(t) }
67
+ class << self
68
+ def call(task_status: "active", **attributes)
69
+ create!(attributes.merge(task_status: task_status, current_state: initial_state).merge(default_times)).tap do |t|
70
+ t.call
71
+ end
72
+ end
73
+ alias_method :perform_now, :call
74
+
75
+ def later(**attributes) = call(task_status: "waiting", **attributes)
76
+ alias_method :perform_later, :later
77
+
78
+ def wake_sleeping
79
+ adapter = Operations::WakeTaskJob.queue_adapter
80
+ begin
81
+ Task.ready_to_wake.find_each do |task|
82
+ Operations::WakeTaskJob.queue_adapter = task.class.queue_adapter
83
+ Operations::WakeTaskJob.perform_later task
84
+ end
85
+ ensure
86
+ Operatives::WakeTaskJob.queue_adapter = adapter
87
+ end
88
+ end
73
89
 
74
- def self.delete_old = Task.ready_to_delete.find_each { |t| Operations::DeleteOldTaskJob.perform_later(t) }
90
+ def delete_old
91
+ Task.ready_to_delete.find_each do |t|
92
+ Operations::DeleteOldTaskJob.perform_later(t)
93
+ end
94
+ end
95
+ end
75
96
  end
76
97
  end
@@ -1,3 +1,3 @@
1
1
  module Operations
2
- VERSION = "0.7.2"
2
+ VERSION = "0.7.3"
3
3
  end
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: standard_procedure_operations
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.7.2
4
+ version: 0.7.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rahoul Baruah
8
8
  bindir: bin
9
9
  cert_chain: []
10
- date: 2025-07-10 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
11
  dependencies:
12
12
  - !ruby/object:Gem::Dependency
13
13
  name: rails
@@ -98,7 +98,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
98
98
  - !ruby/object:Gem::Version
99
99
  version: '0'
100
100
  requirements: []
101
- rubygems_version: 3.6.2
101
+ rubygems_version: 3.6.7
102
102
  specification_version: 4
103
103
  summary: Operations
104
104
  test_files: []