为什么 perform-method 有效,但其他 delayed_job 的事件未被调用?
Why perform-method works, but other delayed_job's events not invoked?
我将 delayed_job-gem 包含在我的项目中:
# Gemfile
gem 'daemons'
gem 'delayed_job_active_record'
# bash
bundle
rails generate delayed_job:active_record
rake db:migrate
rails g job MyFirstDelayedJob
并像这样编辑 app/jobs/my_first_delayed_job_job.rb
文件:
class MyFirstDelayedJobJob < ApplicationJob
queue_as :default
def perform(*args)
JOB_LOGGER.info "perform() happened with args: #{args.inspect}"
end
def success(job)
JOB_LOGGER.info "success() happened. Job object: #{job.inspect}"
end
def error(job, ex)
JOB_LOGGER.info "error() happened. Job object: #{job.inspect}, Exception: #{ex.message}"
end
def failure(job)
JOB_LOGGER.info "failure() happened. Job object: #{job.inspect}"
end
end
从 rails 控制台启动 delayed_job worker 以查看调试日志:
Delayed::Worker.new.start
已创建 rake 任务:
# lib/tasks/run_my_first_delayed_job.rake
desc 'Run job'
task my_first_delayed_job_job: :environment do
JOB_LOGGER.info 'run_my_first_delayed_job.rake: Starting delayed job...'
MyFirstDelayedJobJob.set(wait: 30.seconds).perform_later 'arg1', 'arg2', 333
JOB_LOGGER.info 'run_my_first_delayed_job.rake: Finished.'
end
并在shell执行:rake my_first_delayed_job_job
rake 任务执行后,我有这个日志:
# log/job.log
I, [2017-12-01T19:18:14.591918 #11664] INFO -- : run_my_first_delayed_job.rake: Starting delayed job...
I, [2017-12-01T19:18:14.655178 #11664] INFO -- : run_my_first_delayed_job.rake: Finished.
I, [2017-12-01T19:18:44.785494 #11591] INFO -- : perform() happened with args: ["arg1", "arg2", 333]
为什么 success
、error
、failure
回调没有被调用?
您创建的新工作是这样的:
MyFirstDelayedJobJob.set(wait: 30.seconds).perform_later 'arg1', 'arg2', 333
您的 active-job. But delayed_job 变体 gem 为 运行 个作业添加了 delay
方法,因此您需要使用它,而不是 perform_later
。像这样重写你的代码:
# lib/tasks/run_my_first_delayed_job.rake
MyFirstDelayedJobJob.new.delay(run_at: 30.seconds.from_now).perform 'arg1', 'arg2', 333
我将 delayed_job-gem 包含在我的项目中:
# Gemfile
gem 'daemons'
gem 'delayed_job_active_record'
# bash
bundle
rails generate delayed_job:active_record
rake db:migrate
rails g job MyFirstDelayedJob
并像这样编辑 app/jobs/my_first_delayed_job_job.rb
文件:
class MyFirstDelayedJobJob < ApplicationJob
queue_as :default
def perform(*args)
JOB_LOGGER.info "perform() happened with args: #{args.inspect}"
end
def success(job)
JOB_LOGGER.info "success() happened. Job object: #{job.inspect}"
end
def error(job, ex)
JOB_LOGGER.info "error() happened. Job object: #{job.inspect}, Exception: #{ex.message}"
end
def failure(job)
JOB_LOGGER.info "failure() happened. Job object: #{job.inspect}"
end
end
从 rails 控制台启动 delayed_job worker 以查看调试日志:
Delayed::Worker.new.start
已创建 rake 任务:
# lib/tasks/run_my_first_delayed_job.rake
desc 'Run job'
task my_first_delayed_job_job: :environment do
JOB_LOGGER.info 'run_my_first_delayed_job.rake: Starting delayed job...'
MyFirstDelayedJobJob.set(wait: 30.seconds).perform_later 'arg1', 'arg2', 333
JOB_LOGGER.info 'run_my_first_delayed_job.rake: Finished.'
end
并在shell执行:rake my_first_delayed_job_job
rake 任务执行后,我有这个日志:
# log/job.log
I, [2017-12-01T19:18:14.591918 #11664] INFO -- : run_my_first_delayed_job.rake: Starting delayed job...
I, [2017-12-01T19:18:14.655178 #11664] INFO -- : run_my_first_delayed_job.rake: Finished.
I, [2017-12-01T19:18:44.785494 #11591] INFO -- : perform() happened with args: ["arg1", "arg2", 333]
为什么 success
、error
、failure
回调没有被调用?
您创建的新工作是这样的:
MyFirstDelayedJobJob.set(wait: 30.seconds).perform_later 'arg1', 'arg2', 333
您的 active-job. But delayed_job 变体 gem 为 运行 个作业添加了 delay
方法,因此您需要使用它,而不是 perform_later
。像这样重写你的代码:
# lib/tasks/run_my_first_delayed_job.rake
MyFirstDelayedJobJob.new.delay(run_at: 30.seconds.from_now).perform 'arg1', 'arg2', 333