diff --git a/README.md b/README.md index e99b7d1..b7777be 100644 --- a/README.md +++ b/README.md @@ -408,6 +408,37 @@ class NotifyWorkflow < Gush::Workflow end ``` +### Customization of ActiveJob enqueueing + +There might be a case when you want to customize enqueing a job with more than just the above two options (`queue` and `wait`). + +To pass additional options to `ActiveJob.set`, override `Job#worker_options`, e.g.: + +```ruby + +class ScheduledJob < Gush::Job + + def worker_options + super.merge(wait_until: Time.at(params[:start_at])) + end + +end +``` + +Or to entirely customize the ActiveJob integration, override `Job#enqueue_worker!`, e.g.: + +```ruby + +class SynchronousJob < Gush::Job + + def enqueue_worker!(options = {}) + Gush::Worker.perform_now(workflow_id, name) + end + +end +``` + + ## Command line interface (CLI) ### Checking status diff --git a/lib/gush/client.rb b/lib/gush/client.rb index c8e36d6..6148920 100644 --- a/lib/gush/client.rb +++ b/lib/gush/client.rb @@ -155,14 +155,9 @@ def expire_job(workflow_id, job, ttl=nil) def enqueue_job(workflow_id, job) job.enqueue! persist_job(workflow_id, job) - queue = job.queue || configuration.namespace - wait = job.wait - - if wait.present? - Gush::Worker.set(queue: queue, wait: wait).perform_later(*[workflow_id, job.name]) - else - Gush::Worker.set(queue: queue).perform_later(*[workflow_id, job.name]) - end + + options = { queue: configuration.namespace }.merge(job.worker_options) + job.enqueue_worker!(options) end private diff --git a/lib/gush/job.rb b/lib/gush/job.rb index e432648..5677dfc 100644 --- a/lib/gush/job.rb +++ b/lib/gush/job.rb @@ -59,6 +59,14 @@ def enqueue! @failed_at = nil end + def enqueue_worker!(options = {}) + Gush::Worker.set(options).perform_later(workflow_id, name) + end + + def worker_options + { queue: queue, wait: wait }.compact + end + def finish! @finished_at = current_timestamp end diff --git a/spec/gush/job_spec.rb b/spec/gush/job_spec.rb index 5fd16d3..eb3a971 100644 --- a/spec/gush/job_spec.rb +++ b/spec/gush/job_spec.rb @@ -49,6 +49,45 @@ end end + describe "#enqueue_worker!" do + it "enqueues the job using Gush::Worker" do + job = described_class.new(name: "a-job", workflow_id: 123) + + expect { + job.enqueue_worker! + }.to change{ActiveJob::Base.queue_adapter.enqueued_jobs.size}.from(0).to(1) + end + + it "handles ActiveJob.set options" do + freeze_time = Time.utc(2023, 01, 21, 14, 36, 0) + + travel_to freeze_time do + job = described_class.new(name: "a-job", workflow_id: 123) + job.enqueue_worker!(wait_until: freeze_time + 5.minutes) + expect(Gush::Worker).to have_a_job_enqueued_at(123, job_with_id(job.class.name), 5.minutes) + end + end + end + + describe "#worker_options" do + it "returns a blank options hash by default" do + job = described_class.new + expect(job.worker_options).to eq({}) + end + + it "returns a hash with the queue setting" do + job = described_class.new + job.queue = 'my-queue' + expect(job.worker_options).to eq({ queue: 'my-queue' }) + end + + it "returns a hash with the wait setting" do + job = described_class.new + job.wait = 123 + expect(job.worker_options).to eq({ wait: 123 }) + end + end + describe "#start!" do it "resets flags and marks as running" do job = described_class.new(name: "a-job")