Skip to content

Commit

Permalink
Make ActiveJobScheduler accepts ActiveJob::ConfiguredJob instances
Browse files Browse the repository at this point in the history
It allows for enriched subscription configuration, for example:

event_store.subscribe(NotifyJob.set(wait: 5.minutes), to: [StatusChanged])
event_store.subscribe(NotifyJob.set(priority: 100), to: [SomeCriticalEvent])
event_store.subscribe(NotifyJob.set(queue: 'critical'), to: [SomeCriticalEvent])

So that we can reuse the same worker to configure event subscriptions
which get scheduled differently for different event types.
  • Loading branch information
pjurewicz committed Aug 28, 2023
1 parent b82d61b commit 331c16b
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,11 @@ def call(klass, record)
end

def verify(subscriber)
Class === subscriber && !!(subscriber < ActiveJob::Base)
if Class === subscriber
!!(subscriber < ActiveJob::Base)
else
subscriber.is_a?(ActiveJob::ConfiguredJob)
end
end

private
Expand Down
39 changes: 33 additions & 6 deletions rails_event_store/spec/active_job_scheduler_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,32 +29,35 @@ module RailsEventStore
let(:record) { RubyEventStore::Mappers::Default.new.event_to_record(event) }

describe "#verify" do
let(:scheduler) { ActiveJobScheduler.new(serializer: RubyEventStore::NULL) }
let(:proper_handler) { Class.new(ActiveJob::Base) }

specify do
scheduler = ActiveJobScheduler.new(serializer: RubyEventStore::NULL)
proper_handler = Class.new(ActiveJob::Base)
expect(scheduler.verify(proper_handler)).to eq(true)
end

specify "ActiveJob::ConfiguredJob is also acceptable" do
expect(scheduler.verify(proper_handler.set({}))).to eq(true)
end

specify do
scheduler = ActiveJobScheduler.new(serializer: RubyEventStore::NULL)
some_class = Class.new
expect(scheduler.verify(some_class)).to eq(false)
end

specify do
scheduler = ActiveJobScheduler.new(serializer: RubyEventStore::NULL)
expect(scheduler.verify(ActiveJob::Base)).to eq(false)
end

specify do
scheduler = ActiveJobScheduler.new(serializer: RubyEventStore::NULL)
expect(scheduler.verify(Object.new)).to eq(false)
end
end

describe "#call" do
let(:scheduler) { ActiveJobScheduler.new(serializer: RubyEventStore::Serializers::YAML) }

specify do
scheduler = ActiveJobScheduler.new(serializer: RubyEventStore::Serializers::YAML)
scheduler.call(MyAsyncHandler, record)

enqueued_jobs = ActiveJob::Base.queue_adapter.enqueued_jobs
Expand All @@ -77,6 +80,30 @@ module RailsEventStore
}
)
end

specify "pass ActiveJob::ConfiguredJob" do
scheduler.call(MyAsyncHandler.set(queue: 'non-default'), record)

enqueued_jobs = ActiveJob::Base.queue_adapter.enqueued_jobs
expect(enqueued_jobs.size).to eq(1)
expect(enqueued_jobs[0]).to include(
{
job: MyAsyncHandler,
args: [
{
"event_id" => "83c3187f-84f6-4da7-8206-73af5aca7cc8",
"event_type" => "RubyEventStore::Event",
"data" => "--- {}\n",
"metadata" => "--- {}\n",
"timestamp" => "2019-09-30T00:00:00.000000Z",
"valid_at" => "2019-09-30T00:00:00.000000Z",
"_aj_symbol_keys" => []
}
],
queue: "non-default"
}
)
end
end

class MyAsyncHandler < ActiveJob::Base
Expand Down

0 comments on commit 331c16b

Please sign in to comment.