我正在探索如何设置和使用sidekiq在后台执行进程的不同教程。我决定跟随this tutorial并完全遵循一切。但是,当我开始尝试执行作业时,我的sidekiq队列中没有添加任何内容,并且作业内联执行。
就像在教程中一样,我期待每次我尝试执行这个工作时,它应该反映在我的sidekiq中但是当我这样做时,它仍然是这样的:
以下是教程中所需的必要文件中的代码:
我的Gemfile:
# Gemfile.rb
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.6.0'
gem 'rails', '~> 5.2.2'
gem 'sqlite3'
gem 'puma', '~> 3.11'
gem 'sass-rails', '~> 5.0'
gem 'uglifier', '>= 1.3.0'
gem 'coffee-rails', '~> 4.2'
gem 'turbolinks', '~> 5'
gem 'jbuilder', '~> 2.5'
gem 'semantic-ui-sass', github: 'doabit/semantic-ui-sass'
gem 'slim-rails'
gem 'chartkick'
gem 'groupdate'
gem 'faker', :git => 'https://github.com/stympy/faker.git', :branch => 'master'
gem 'rest-client'
gem 'devise', '~> 4.6', '>= 4.6.1'
gem 'sidekiq'
gem 'bootsnap', '>= 1.1.0', require: false
group :development, :test do
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
gem 'chromedriver-helper'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
我的工作:
#generate_random_vehicle_job.rb
class GenerateRandomVehicleJob < ApplicationJob
queue_as :default
def perform(*args)
detected_vehicle = DetectedVehicle.new
detected_vehicle.detection_time = Faker::Date.between(30.days.ago, Date.today)
detected_vehicle.license_plate_text = Faker::Vehicle.license_plate
detected_vehicle.camera_id = rand(1..Camera.count)
#detected_vehicle = DetectedVehicle.create(detection_time: date, camera_id: camera_id, license_plate_text: license_plate)
detected_vehicle.license_plate_image.attach(io: File.open(Rails.root.join('public', 'sample_plate.png')), filename: 'sample_plate.png', content_type: 'image/png')
detected_vehicle.vehicle_image.attach(io: File.open(Rails.root.join('public', 'sample_vehicle.jpg')), filename: 'sample_vehicle.jpg', content_type: 'image/jpeg')
detected_vehicle.video_footage_snippet.attach(io: File.open(Rails.root.join('public', 'sample_video.mp4')), filename: 'sample_video.mp4', content_type: 'video/mp4')
sleep 2
end
end
我的application.rb:
# config/application.rb
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module CatchAllApp
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.2
config.active_job.queue_adapter = :sidekiq
# No need to run sidekiq and redis server in development
#config.active_job.queue_adapter = Rails.env.production? ? :sidekiq : :async
# Settings in config/environments/* take precedence over those specified here.
# Application configuration can go into files in config/initializers
# -- all .rb files in that directory are automatically loaded after loading
# the framework and any gems in your application.
end
end
我的sidekiq.rb
# config/initializers/sidekiq.rb
Sidekiq.configure_server do |config|
config.redis = { url: 'redis://localhost:6379/0' }
end
Sidekiq.configure_client do |config|
config.redis = { url: 'redis://localhost:6379/0' }
end
最后,我的控制器:
def create_random_vehicle
GenerateRandomVehicleJob.perform_later
redirect_to root_path
end
我确信我的redis-server,rails服务器和sidekiq都正常运行。
事实证明,我的active_job.queue_adapter
已被我的config/environment/development.rb
覆盖,正如@max pleaner所指出的那样。我刚刚删除了active_job.queue_adapter = :inline
,它解决了我的问题! :)