我正在使用sidekiq gem处理后台作业,而sidekiq-cron则以特定时间间隔安排作业。
在config / routes.rb中,我添加了以下内容以向sidekiq UI端点提供身份验证。
authentication = ->req { req.env["warden"].authenticate!}
constraints authentication do
mount Sidekiq::Web => '/sidekiq'
end
当我点击Enqueue now按钮(参见附图)时,它会抛出以下错误并且作业未添加到队列中。它没有认证就工作了。
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'
我正在使用Sidekiq Pro和cron gem。也许我的配置可以帮助你(虽然我使用基本的HTTP身份验证表单)。
在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
然后在routes.rb上我只有这个:
mount Sidekiq::Web => '/sidekiq'
因此,每次我去some-url / sidekiq时,都会出现一个身份验证表单。
希望这可以帮到你。