Skip to content

Commit

Permalink
feat: return job ID from client push
Browse files Browse the repository at this point in the history
What did we change?

Have `SidekiqPublisher::Worker.client_push` return a job ID string, which
matches the sidekiq gem behavior. Currently `.client_push` returns the inserted
`SidekiqPublisher::Job` ActiveRecord instance.

Why are we doing this?

Having a consistent return value between the native sidekiq gem and the
sidekiq_publisher gem makes things easier to handle. Otherwise clients have to
juggle using `#job_id` method (for sidekiq_publisher gem) or the job ID string
(for sidekiq gem). For example:

```rb
job = ExampleJob.perform_async
logger.info msg: "some example", job_id: job.try(:job_id) || job
```
  • Loading branch information
juliantrueflynn committed Jul 17, 2024
1 parent 9540254 commit 3e28611
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/sidekiq_publisher/worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def self.included(base)
module ClassMethods
def client_push(item)
if SidekiqPublisher::DatabaseConnection.transaction_open?
SidekiqPublisher::Job.create_job!(item)
SidekiqPublisher::Job.create_job!(item).job_id
else
super
end
Expand Down
30 changes: 30 additions & 0 deletions spec/sidekiq_publisher/worker_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
expect(sidekiq_job.display_class).to eq("TestWorker")
expect(sidekiq_job.display_args).to eq(args)
end

it "returns job ID" do
fake_job_id = SecureRandom.hex(12)
allow(SidekiqPublisher::Job).to receive(:generate_sidekiq_jid).and_return(fake_job_id)

result = TestWorker.sidekiq_client_push(
"class" => TestWorker,
"args" => args
)

expect(result).to eq(fake_job_id)
end
end

describe ".perform_async" do
Expand All @@ -48,6 +60,15 @@
queue = Sidekiq::Queue.new("default")
expect(queue.size).to eq(0)
end

it "returns job ID" do
fake_job_id = SecureRandom.hex(12)
allow(SidekiqPublisher::Job).to receive(:generate_sidekiq_jid).and_return(fake_job_id)

result = TestWorker.perform_async(*args)

expect(result).to eq(fake_job_id)
end
end

context "when not in a transaction", skip_db_clean: true do
Expand All @@ -67,6 +88,15 @@
expect(sidekiq_job.display_class).to eq("TestWorker")
expect(sidekiq_job.display_args).to eq(args)
end

it "returns job ID" do
fake_job_id = SecureRandom.hex(12)
allow(SidekiqPublisher::Job).to receive(:generate_sidekiq_jid).and_return(fake_job_id)

result = TestWorker.perform_async(*args)

expect(result).to eq(fake_job_id)
end
end

context "when Sidekiq::Testing mode is inline" do
Expand Down

0 comments on commit 3e28611

Please sign in to comment.