Making edits to the sidekiq initialising config

As discussed, I’m trying to take advantage of this but have hit a snag, because I’m customising:

/config/initializers/100-sidekiq.rb

Here’s my altered code, I’m:

  • Giving each job an accessible retry count attribute which I’m using in a plugin to change workflow logic depending on the amount of times a job has been retried

In the file I’m amending it to

  • Create a custom Middleware class to retrieve the value
  • Adding the class to the chain so it is included
require "sidekiq/pausable"
require "sidekiq/discourse_event"
require "sidekiq_logster_reporter"
require "sidekiq_long_running_job_logger"
require "mini_scheduler_long_running_job_logger"

 module Sidekiq::Middleware::Server #<-- NEW
   class SetRetryCountMiddleware
     def call(worker, job, queue)
       worker.retry_count = job["retry_count"] if worker.respond_to?(:retry_count=)
       yield
     end
   end
 end


Sidekiq.configure_client { |config| config.redis = Discourse.sidekiq_redis_config }

Sidekiq.configure_server do |config|
  config.redis = Discourse.sidekiq_redis_config
  config[:skip_default_job_logging] = false

  config.server_middleware do |chain|
    chain.add Sidekiq::Pausable
    chain.add Sidekiq::Middleware::Server::SetRetryCountMiddleware #<-- NEW
    chain.add Sidekiq::DiscourseEvent
  end

Presently I’m achieving that with a file edit script in app.yml using sed

This breaks the hygeine of the repo, so I can’t use the bundled compiled assets.

Any suggestions on how to make these modifications from a plugin?

1 Like

Does the specific position in the stack matter? I tried this in a plugin.rb and it seemed to add the middleware :crossed_fingers:

class BreakEverythingMiddleware
  def call(worker, job, queue)
    puts "The middleware worked!"
    raise "THE MIDDLEWARE WORKED"
  end
end

Sidekiq.default_configuration.server_middleware { |chain| chain.add BreakEverythingMiddleware }
1 Like

Shouldn’t do!

Great suggestion, I’m going to try that out! Thanks!

2 Likes