0

简而言之,我收到一个错误,public_activity正在寻找在工作current_user期间更新模型时sidekiq。这是意料之中的,因为没有current_user....如何public_activity在作业运行时禁用跟踪?我在工作期间尝试过改变PublicActivity.enabled = falseProspect.public_activity_offtracked 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 工作/工人

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

我也在public_activity 问题中发布了同样的问题

4

2 回答 2

0

如果您挽救错误,则可以在此问题得到解决之前通过该问题:

tracked owner: Proc.new do |controller, model| 
  controller.current_user rescue nil_or_User_new_or_something_else
end
于 2016-04-20T18:11:46.020 回答
0

这个问题最终不是sidekiq宝石public_activity。执行 Prospect.public_activity_off 有效(如下),尽管我之前说过它没有。问题是该sidekiq过程正在缓存模型和工作人员,因此我对模型所做的任何更改都不会生效,直到sidekiq被回收/重新启动。之后,以下功能运行良好。

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更改工人后重新启动!

于 2016-04-27T14:48:13.423 回答