route.rb下添加 require 'sidekiq/cron/web',通过corn UI界面就可以管理定时任务
config下新建:schedule.yml
Job Name: cron: "* * * * *" class: "HardWorker" queue: default
initialize下sidekiq.rb中添加以下内容,这样启动sidekiq 定时任务也一块启动
Sidekiq.configure_server do |config| config.redis = { url: 'redis://localhost:6379/0' } schedule_file = "config/schedule.yml" if File.exists?(schedule_file) && Sidekiq.server? Sidekiq::Cron::Job.load_from_hash YAML.load_file(schedule_file) end end
常用方法
Finding jobs
#return array of all jobs
Sidekiq::Cron::Job.all
#return one job by its unique name - case sensitive
Sidekiq::Cron::Job.find "Job Name"
#return one job by its unique name - you can use hash with 'name' key
Sidekiq::Cron::Job.find name: "Job Name"
#if job can't be found nil is returned
Destroy jobs:
#destroys all jobs
Sidekiq::Cron::Job.destroy_all!
#destroy job by its name
Sidekiq::Cron::Job.destroy "Job Name"
#destroy found job
Sidekiq::Cron::Job.find('Job name').destroy
Work with job:
job = Sidekiq::Cron::Job.find('Job name')
#disable cron scheduling
job.disable!
#enable cron scheduling
job.enable!
#get status of job:
job.status
# => enabled/disabled
#enqueue job right now!
job.enque!