Active Job

rails generate job guests_cleanup

  class GuestsCleanupJob < ApplicationJob
    queue_as :default
    def perform(*guests) 
        # 稍后做些事情
    end 
end
# 入队作业,作业在队列系统空闲时立即执行 GuestsCleanupJob.perform_later guest
# 入队作业,在明天中午执行
GuestsCleanupJob.set(wait_until: Date.tomorrow.noon).perform_later(guest)
# 入队作业,在一周以后执行
GuestsCleanupJob.set(wait: 1.week).perform_later(guest)
# `perform_now` 和 `perform_later` 会在幕后调用 `perform`
# 因此可以传入任意个参数
GuestsCleanupJob.perform_later(guest1, guest2, filter: 'some_filter')
#队列后端设置
  # config/application.rb
    module YourApp
      class Application < Rails::Application
        # 要把适配器的 gem 写入 Gemfile
        # 请参照适配器的具体安装和部署说明 
        config.active_job.queue_adapter = :sidekiq
    end 
# 如需想现在发送电子邮件,使用 #deliver_now
注意
确保队列后端“监听”着队列名称。某些后端要求指定要监听的队列。
 17.6 回调 - 437
UserMailer.welcome(@user).deliver_now
# 如果想通过 Active Job 发送电子邮件,使用 #deliver_later
UserMailer.welcome(@user).deliver_later
#sidekiq.yml

:concurrency: 5
:pidfile: tmp/pids/sidekiq.pid

development:
  :concurrency: 2
production:
  :concurrency: 5
#处理消息队列
:queues:
  - [default, 5]

:logfile: log/sidekiq.log
#启动sidekiq
sidekiq -C config/sidekiq.yml -e development
Rails.application.routes.draw do
  #sidekiq路由配置
  require 'sidekiq/web'
  mount Sidekiq::Web => '/sidekiq-stat'
end
  include Sidekiq::Worker
  sidekiq_options queue: :kuaidi100_subscribe, backtrace: true
  #backtrace: true

你可能感兴趣的:(ruby,on,rails,Ruby,on,Rails)