禁用 public_activity 跟踪需要 current_user 的 sidekiq 作业
disable public_activity tracking for sidekiq jobs that require current_user
简而言之,我在 sidekiq
作业期间更新模型时收到 public_activity
正在寻找 current_user
的错误。这是预料之中的,因为没有 current_user
... 我如何在作业 运行 时禁用 public_activity
跟踪?我已经尝试在工作期间更改 PublicActivity.enabled = false
和 Prospect.public_activity_off
,但 tracked owner: Proc.new{ |controller, model| controller.current_user }
似乎仍在捡起它并寻找 current_user
如何让 sidekiq
(或任何作业)在没有 public_activity
干扰的情况下进行更新?
在作业期间让 sidekiq
将 current_user
设置为虚拟用户会更好吗?我认为这不是一个好主意,因为我们稍后需要过滤掉它。
错误:
2016-04-20T17:24:53.728Z 20447 TID-owpsaw07w ProspectDataAggregationWorker JID-bad79ab6a073d57d7388e931 INFO: start
2016-04-20T17:24:53.739Z 20447 TID-owpsaw07w ProspectDataAggregationWorker JID-bad79ab6a073d57d7388e931 INFO: fail: 0.01 sec
2016-04-20T17:24:53.739Z 20447 TID-owpsaw07w WARN: {"class"=>"ProspectDataAggregationWorker", "args"=>[1], "retry"=>false, "queue"=>"default", "jid"=>"bad79ab6a073d57d7388e931", "created_at"=>1461173093.728181, "enqueued_at"=>1461173093.7282372}
2016-04-20T17:24:53.739Z 20447 TID-owpsaw07w WARN: NoMethodError: undefined method `current_user' for nil:NilClass
2016-04-20T17:24:53.739Z 20447 TID-owpsaw07w WARN: /Users/cj/RubymineProjects/revenue_management_system/app/models/prospect.rb:3:in `block in <class:Prospect>'
/Users/cj/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/public_activity-1.5.0/lib/public_activity/common.rb:14:in `resolve_value'
/Users/cj/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/public_activity-1.5.0/lib/public_activity/common.rb:315:in `prepare_relation'
/Users/cj/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/public_activity-1.5.0/lib/public_activity/common.rb:281:in `prepare_settings'
/Users/cj/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/public_activity-1.5.0/lib/public_activity/common.rb:250:in `create_activity'
/Users/cj/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/public_activity-1.5.0/lib/public_activity/actions/update.rb:14:in `activity_on_update'
/Users/cj/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:432:in `block in make_lambda'
完整错误:https://gist.github.com/cdesch/190c46c69213a37e4162e50727531761
型号:
class Prospect < ActiveRecord::Base
include PublicActivity::Model
tracked owner: Proc.new{ |controller, model| controller.current_user }
...
end
Sidekiq Job/worker
class ProspectDataAggregationWorker
include Sidekiq::Worker
sidekiq_options :retry => false # job will be discarded immediately if failed
def perform(account_id)
#Prospect.public_activity_off
PublicActivity.enabled = false
account = Account.where(id: account_id).first
prospects = account.prospects
prospects.each do |prospect|
prospect.update_based_on_primary_assessment!
end
PublicActivity.enabled = true
#Prospect.public_activity_on
end
end
中发布了同样的问题
如果你挽救了你的错误,你可以在这个问题得到解决之前通过这个问题:
tracked owner: Proc.new do |controller, model|
controller.current_user rescue nil_or_User_new_or_something_else
end
这个问题最终不是 sidekiq
或 public_activity
gems。做 Prospect.public_activity_off 有效(如下),尽管我之前说过它没有。问题是 sidekiq
进程正在缓存模型和工作人员,因此我对模型所做的任何更改都不会生效,直到 sidekiq
为 recycled/restarted。之后,以下功能正常工作。
def perform(account_id)
Prospect.public_activity_off
...
end
或以下内容:
class ApplicationWorker
include Sidekiq::Worker
def perform(*args)
PublicActivity.without_tracking { super(*args) }
end
end
核心问题不是代码问题,而是开发人员问题。 :-) 更改工作人员后重新启动 sidekiq
!
简而言之,我在 sidekiq
作业期间更新模型时收到 public_activity
正在寻找 current_user
的错误。这是预料之中的,因为没有 current_user
... 我如何在作业 运行 时禁用 public_activity
跟踪?我已经尝试在工作期间更改 PublicActivity.enabled = false
和 Prospect.public_activity_off
,但 tracked owner: Proc.new{ |controller, model| controller.current_user }
似乎仍在捡起它并寻找 current_user
如何让 sidekiq
(或任何作业)在没有 public_activity
干扰的情况下进行更新?
在作业期间让 sidekiq
将 current_user
设置为虚拟用户会更好吗?我认为这不是一个好主意,因为我们稍后需要过滤掉它。
错误:
2016-04-20T17:24:53.728Z 20447 TID-owpsaw07w ProspectDataAggregationWorker JID-bad79ab6a073d57d7388e931 INFO: start
2016-04-20T17:24:53.739Z 20447 TID-owpsaw07w ProspectDataAggregationWorker JID-bad79ab6a073d57d7388e931 INFO: fail: 0.01 sec
2016-04-20T17:24:53.739Z 20447 TID-owpsaw07w WARN: {"class"=>"ProspectDataAggregationWorker", "args"=>[1], "retry"=>false, "queue"=>"default", "jid"=>"bad79ab6a073d57d7388e931", "created_at"=>1461173093.728181, "enqueued_at"=>1461173093.7282372}
2016-04-20T17:24:53.739Z 20447 TID-owpsaw07w WARN: NoMethodError: undefined method `current_user' for nil:NilClass
2016-04-20T17:24:53.739Z 20447 TID-owpsaw07w WARN: /Users/cj/RubymineProjects/revenue_management_system/app/models/prospect.rb:3:in `block in <class:Prospect>'
/Users/cj/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/public_activity-1.5.0/lib/public_activity/common.rb:14:in `resolve_value'
/Users/cj/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/public_activity-1.5.0/lib/public_activity/common.rb:315:in `prepare_relation'
/Users/cj/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/public_activity-1.5.0/lib/public_activity/common.rb:281:in `prepare_settings'
/Users/cj/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/public_activity-1.5.0/lib/public_activity/common.rb:250:in `create_activity'
/Users/cj/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/public_activity-1.5.0/lib/public_activity/actions/update.rb:14:in `activity_on_update'
/Users/cj/.rbenv/versions/2.3.0/lib/ruby/gems/2.3.0/gems/activesupport-4.2.6/lib/active_support/callbacks.rb:432:in `block in make_lambda'
完整错误:https://gist.github.com/cdesch/190c46c69213a37e4162e50727531761
型号:
class Prospect < ActiveRecord::Base
include PublicActivity::Model
tracked owner: Proc.new{ |controller, model| controller.current_user }
...
end
Sidekiq Job/worker
class ProspectDataAggregationWorker
include Sidekiq::Worker
sidekiq_options :retry => false # job will be discarded immediately if failed
def perform(account_id)
#Prospect.public_activity_off
PublicActivity.enabled = false
account = Account.where(id: account_id).first
prospects = account.prospects
prospects.each do |prospect|
prospect.update_based_on_primary_assessment!
end
PublicActivity.enabled = true
#Prospect.public_activity_on
end
end
中发布了同样的问题
如果你挽救了你的错误,你可以在这个问题得到解决之前通过这个问题:
tracked owner: Proc.new do |controller, model|
controller.current_user rescue nil_or_User_new_or_something_else
end
这个问题最终不是 sidekiq
或 public_activity
gems。做 Prospect.public_activity_off 有效(如下),尽管我之前说过它没有。问题是 sidekiq
进程正在缓存模型和工作人员,因此我对模型所做的任何更改都不会生效,直到 sidekiq
为 recycled/restarted。之后,以下功能正常工作。
def perform(account_id)
Prospect.public_activity_off
...
end
或以下内容:
class ApplicationWorker
include Sidekiq::Worker
def perform(*args)
PublicActivity.without_tracking { super(*args) }
end
end
核心问题不是代码问题,而是开发人员问题。 :-) 更改工作人员后重新启动 sidekiq
!