0

I am using sidekiq gem to process the background jobs and sidekiq-cron to schedule the jobs at certain time interval.

In config/routes.rb, I have added the following to provide authentication to sidekiq UI endpoint.

  authentication = ->req { req.env["warden"].authenticate!}

  constraints authentication do
    mount Sidekiq::Web => '/sidekiq'
  end

When I hit Enqueue now button (Refer attached image), It throws following error and job not added to queue. It was working without authentication.

 ActionController::RoutingError (No route matches [GET] "/sidekiq/cron/my_report/enque"):
F, [2019-04-11T11:00:27.684536 #9365] FATAL -- :   
F, [2019-04-11T11:00:27.684733 #9365] FATAL -- : actionpack (5.2.3) lib/action_dispatch/middleware/debug_exceptions.rb:65:in `call'
web-console (3.7.0) lib/web_console/middleware.rb:135:in `call_app'
web-console (3.7.0) lib/web_console/middleware.rb:30:in `block in call'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `catch'
web-console (3.7.0) lib/web_console/middleware.rb:20:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/show_exceptions.rb:33:in `call'
railties (5.2.3) lib/rails/rack/logger.rb:38:in `call_app'
railties (5.2.3) lib/rails/rack/logger.rb:26:in `block in call'
activesupport (5.2.3) lib/active_support/tagged_logging.rb:71:in `block in tagged'
activesupport (5.2.3) lib/active_support/tagged_logging.rb:28:in `tagged'
activesupport (5.2.3) lib/active_support/tagged_logging.rb:71:in `tagged'
railties (5.2.3) lib/rails/rack/logger.rb:26:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/remote_ip.rb:81:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/request_id.rb:27:in `call'
rack (2.0.7) lib/rack/runtime.rb:22:in `call'
activesupport (5.2.3) lib/active_support/cache/strategy/local_cache_middleware.rb:29:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/executor.rb:14:in `call'
actionpack (5.2.3) lib/action_dispatch/middleware/static.rb:127:in `call'
rack (2.0.7) lib/rack/sendfile.rb:111:in `call'
railties (5.2.3) lib/rails/engine.rb:524:in `call'
puma (3.12.1) lib/puma/configuration.rb:227:in `call'
puma (3.12.1) lib/puma/server.rb:660:in `handle_request'
puma (3.12.1) lib/puma/server.rb:474:in `process_client'
puma (3.12.1) lib/puma/server.rb:334:in `block in run'
puma (3.12.1) lib/puma/thread_pool.rb:135:in `block in spawn_thread'

enter image description here

Galet
  • 5,853
  • 21
  • 82
  • 148

1 Answers1

0

I'm using Sidekiq Pro and cron gem. Maybe my configuration can help you (although I use a basic HTTP auth form).

In application.rb:

require 'sidekiq-pro'
require 'sidekiq/pro/web'
require 'sidekiq/cron/web'

class Application < Rails::Application
  Sidekiq::Web.use Rack::Auth::Basic do |username, password|
      ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(username), ::Digest::SHA256.hexdigest(Rails.application.secrets.sidekiq_username)) &
        ActiveSupport::SecurityUtils.secure_compare(::Digest::SHA256.hexdigest(password), ::Digest::SHA256.hexdigest(Rails.application.secrets.sidekiq_password))
    end if Rails.env.production?
end

Then on routes.rb I only have this:

  mount Sidekiq::Web => '/sidekiq'

So, every time I go to some-url/sidekiq, an auth form will appear.

Hope this can help you.

  • I am able to load some-url/sidekiq after my authentication code. I am getting error when I hit "Enqueue now" button to trigger the job manually instead of scheduled one. – Galet Apr 12 '19 at 04:42
  • @Galet maybe because your sidekiq routes are **wrapped** by the authentication constraint. Try to do that on the initializer using the _Sidekiq::Web.use authentication_ – Bernardo Graça Apr 12 '19 at 11:18