Sidekiq: Periodic jobs: adding arguments

Created on 18 Mar 2017  路  4Comments  路  Source: mperham/sidekiq

We just this week upgraded from Sidekiq Pro to Sidekiq Enterprise and are looking to switch from clockwork for our scheduled jobs to Enterprise's periodic jobs feature. In the wiki page you note:

The perform method for periodic workers must take NO parameters (or have default values for all parameters).

Our current clockwork configuration pushes all work into Sidekiq background jobs already, but a bunch of our jobs take arguments even when enqueued periodically. For example, we have a bunch of different notifications (email, push, SMS) that get all triggered the exact same way, and rather than have a different job class for every type, we have a job class that looks like this:

class PostalEventBatchJob < ApplicationJob
  def perform(event)
    PostalEvents.const_get(event.camelize).enqueue_batch!
  end
end

Our clockwork configuration then looks like this:

every(1.hour, "Foo", at: "**:00") do
  PostalEventBatchJob.perform_async("foo")
end

every(1.hour, "Bar", at: "**:30") do
  PostalEventBatchJob.perform_async("bar")
end

I'm curious what the reason is for not supporting arguments in the periodic job configuration. When I was looking at the sidekiq-ent source code (I won't paste it here) it looks like something that could be reasonably easy to add support for. Something similar to this maybe?

Sidekiq.configure_server do |config|
  config.periodic do |mgr|
    mgr.register('0 * * * *', PostalEventBatchJob, args: ["foo"])
    mgr.register('30 * * * *', PostalEventBatchJob, args: ["bar"])
  end
end

I'm not sure if you accept pull requests to sidekiq-ent, but if so I'd be happy to take a stab at adding support for something like this!

Most helpful comment

馃挜 thanks for the quick turn-around @mperham!

All 4 comments

Yep, that will work. You can pass in args like that as long as you use String keys:

mgr.register('0 * * * *', PostalEventBatchJob, 'args' => ["foo"])

Oh, even better that it doesn't require any changes! Should the wiki page be updated to note that it's doable?

馃挜 thanks for the quick turn-around @mperham!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

smanolloff picture smanolloff  路  3Comments

fatcatt316 picture fatcatt316  路  4Comments

sandstrom picture sandstrom  路  3Comments

homanchou picture homanchou  路  3Comments

davidcelis picture davidcelis  路  3Comments