如何访问活动作业中的延迟作业实例 - Rails 4.2
How to access Delayed Job instance inside Active Job - Rails 4.2
我在后台使用 delayed_job (4.0.6) 的 ActiveJob,我想找到一个计划的作业来删除它。
例如,如果我有
class MyClass
def my_method
perform_stuff
MyJob.set(wait: 1.month.from_now).perform_later(current_user)
end
end
然后,如果我编辑 MyClass 实例并再次调用 my_method,我想取消该作业并安排一个新作业。
按照此 post http://www.sitepoint.com/delayed-jobs-best-practices 中的建议,我向延迟作业添加了两列 Table:
table.integer :delayed_reference_id
table.string :delayed_reference_type
add_index :delayed_jobs, [:delayed_reference_id], :name => 'delayed_jobs_delayed_reference_id'
add_index :delayed_jobs, [:delayed_reference_type], :name => 'delayed_jobs_delayed_reference_type'
所以这样我可能会找到延迟的工作并将其销毁。但我想在 ActiveJob class 中执行此操作,以维护我项目中的作业模式。
我想做这样的事情:
class MyJob < ActiveJob::Base
after_enqueue do |job|
user = self.arguments.first
job.delayed_reference_id = user.id,
job.delayed_reference_type = "User"
end
def perform(user)
delete_previous_job_if_exists(user_id)
end
def delete_previous_job_if_exists(user_id)
Delayed::Job.find_by(delayed_reference_id: 1, delayed_reference_type: 'User').delete
end
end
但这不起作用。
有人遇到过这种问题吗?
两个变化:
1. 更新了 after_enqueue 回调,以便您可以更新
delayed_jobs table 直接
2. 修复了 delayed_reference_id
被硬编码为 1
的拼写错误
这应该有效:
class MyJob < ActiveJob::Base
after_enqueue do |job|
user = self.arguments.first
delayed_job = Delayed::Job.find(job.provider_job_id)
delayed_job.update(delayed_reference_id:user.id,delayed_reference_type:'User')
end
def perform(user)
delete_previous_job_if_exists(user.id)
end
def delete_previous_job_if_exists(user_id)
Delayed::Job.find_by(delayed_reference_id: user_id, delayed_reference_type: 'User').delete
end
end
我在后台使用 delayed_job (4.0.6) 的 ActiveJob,我想找到一个计划的作业来删除它。
例如,如果我有
class MyClass
def my_method
perform_stuff
MyJob.set(wait: 1.month.from_now).perform_later(current_user)
end
end
然后,如果我编辑 MyClass 实例并再次调用 my_method,我想取消该作业并安排一个新作业。
按照此 post http://www.sitepoint.com/delayed-jobs-best-practices 中的建议,我向延迟作业添加了两列 Table:
table.integer :delayed_reference_id
table.string :delayed_reference_type
add_index :delayed_jobs, [:delayed_reference_id], :name => 'delayed_jobs_delayed_reference_id'
add_index :delayed_jobs, [:delayed_reference_type], :name => 'delayed_jobs_delayed_reference_type'
所以这样我可能会找到延迟的工作并将其销毁。但我想在 ActiveJob class 中执行此操作,以维护我项目中的作业模式。
我想做这样的事情:
class MyJob < ActiveJob::Base
after_enqueue do |job|
user = self.arguments.first
job.delayed_reference_id = user.id,
job.delayed_reference_type = "User"
end
def perform(user)
delete_previous_job_if_exists(user_id)
end
def delete_previous_job_if_exists(user_id)
Delayed::Job.find_by(delayed_reference_id: 1, delayed_reference_type: 'User').delete
end
end
但这不起作用。
有人遇到过这种问题吗?
两个变化:
1. 更新了 after_enqueue 回调,以便您可以更新
delayed_jobs table 直接
2. 修复了 delayed_reference_id
被硬编码为 1
这应该有效:
class MyJob < ActiveJob::Base
after_enqueue do |job|
user = self.arguments.first
delayed_job = Delayed::Job.find(job.provider_job_id)
delayed_job.update(delayed_reference_id:user.id,delayed_reference_type:'User')
end
def perform(user)
delete_previous_job_if_exists(user.id)
end
def delete_previous_job_if_exists(user_id)
Delayed::Job.find_by(delayed_reference_id: user_id, delayed_reference_type: 'User').delete
end
end