如何使用 sidekiq 为工作人员设置文件追踪 whodunnit
How set paper trail whodunnit for workers using sidekiq
我正在尝试实现,当在 worker 内部更新具有纸迹的模型时,必须使用 'worker' 设置 whodunnit,我尝试了很多但没有成功,最后一个是尝试解决方案在 github thread 中发现问题是我有 perfom 的工人没有/有 1 个或多个参数(我将其更改为设置 PaperTrail.request.whodunnit = 'worker')
如何在每次执行之前或前后设置 whodunnit,以便版本中的 whodunnit 保存为 whodunnit: worker?
我的工人长这样:
module ModuleName
module Workers
class WorkerClass
include Sidekiq::Worker
sidekiq_options queue: 'default', retry: true
def perform # here with none, 1 or more params
# update some model
end
end
end
end
适合我的解决方案
base_worker.rb
class BaseWorker
extend ActiveModel::Callbacks
define_model_callbacks :perform
around_perform :set_paper_trail_whodunnit
def set_paper_trail_whodunnit
PaperTrail.request.whodunnit = "worker-#{self.class.name}"
yield
PaperTrail.request.whodunnit = nil
end
def perform(*args)
run_callbacks(:perform) do
perform!(*args)
end
end
def perform!(*_args)
raise 'Missing your #perform! method implementation'
end
end
工人:
module ModuleName
module Workers
class WorkerClass < ::BaseWorker
include Sidekiq::Worker
sidekiq_options queue: 'default', retry: true
def perform! # here with none, 1 or more params
# update some model
end
end
end
end
我正在尝试实现,当在 worker 内部更新具有纸迹的模型时,必须使用 'worker' 设置 whodunnit,我尝试了很多但没有成功,最后一个是尝试解决方案在 github thread 中发现问题是我有 perfom 的工人没有/有 1 个或多个参数(我将其更改为设置 PaperTrail.request.whodunnit = 'worker')
如何在每次执行之前或前后设置 whodunnit,以便版本中的 whodunnit 保存为 whodunnit: worker?
我的工人长这样:
module ModuleName
module Workers
class WorkerClass
include Sidekiq::Worker
sidekiq_options queue: 'default', retry: true
def perform # here with none, 1 or more params
# update some model
end
end
end
end
适合我的解决方案
base_worker.rb
class BaseWorker
extend ActiveModel::Callbacks
define_model_callbacks :perform
around_perform :set_paper_trail_whodunnit
def set_paper_trail_whodunnit
PaperTrail.request.whodunnit = "worker-#{self.class.name}"
yield
PaperTrail.request.whodunnit = nil
end
def perform(*args)
run_callbacks(:perform) do
perform!(*args)
end
end
def perform!(*_args)
raise 'Missing your #perform! method implementation'
end
end
工人:
module ModuleName
module Workers
class WorkerClass < ::BaseWorker
include Sidekiq::Worker
sidekiq_options queue: 'default', retry: true
def perform! # here with none, 1 or more params
# update some model
end
end
end
end