Search code examples
ruby-on-railssidekiqpublic-activity

disable public_activity tracking for sidekiq jobs that require current_user


In short I get an error that public_activity is looking for the current_user while updating a model during a sidekiq job. That is expected since there is no current_user.... how do I disable public_activity tracking while the job is running? I've tried change PublicActivity.enabled = false and Prospect.public_activity_off during the job, but the tracked owner: Proc.new{ |controller, model| controller.current_user } still seems to be picking it up and looking for the current_user

How can I have the sidekiq (or any job) make updates without public_activity interfering ?

Would it be better to have sidekiq set the current_user to a dummy user during the job? I don't think this would be a good idea, because then we would need to filter that out later.

Error:

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'

Full error: https://gist.github.com/cdesch/190c46c69213a37e4162e50727531761

Model:

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

I have also posted this same question in the public_activity issues


Solution

  • This issue ultimately was not sidekiq or public_activity gems. Doing Prospect.public_activity_off worked (below), even though I previously stated it did not. The issue was that the sidekiq process was caching the models and workers, thus any changes I was making to the to the models were not taking effect until sidekiq was recycled/restarted. After which, the following functions worked fine.

    def perform(account_id)
    
       Prospect.public_activity_off
       ...
    end 
    

    or the following:

    class ApplicationWorker
      include Sidekiq::Worker
    
       def perform(*args)
         PublicActivity.without_tracking { super(*args) }
       end
    end
    

    The issue at heart was not a code issue, but a developer issue. :-) Restart sidekiq after making worker changes!