其他分享
首页 > 其他分享> > sidekiq-cron定时任务

sidekiq-cron定时任务

作者:互联网

参考

gem "sidekiq-cron", "~> 1.1"
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!

标签:Cron,name,Sidekiq,sidekiq,cron,job,Job,定时
来源: https://www.cnblogs.com/znsongshu/p/10350360.html