brandonhilkert / sucker_punch

Sucker Punch is a Ruby asynchronous processing library using concurrent-ruby, heavily influenced by Sidekiq and girl_friday.
MIT License
2.65k stars 114 forks source link

Assigning a user to an action #185

Closed nynhex closed 7 years ago

nynhex commented 7 years ago

I'm using the latest sucker_punch and it works great. I have a simple worker that updates an attribute based off of a AR scoped array.

When I fire this job or schedule it, it performs fine, however because current_user (devise) is not available in the worker/job when the attribute is updated and my auditing module (audited) records the change, I get a user_id with a nil value.

Is there a way to fire off this job and somehow pass a User object as an argument so that audited will record user_id somehow?

Here's my very basic worker, and how I'm calling it (note I'm using the deprecated syntax which I wrote the initializer for)

class ControlledSubstanceAutoDenyJob
  include SuckerPunch::Job

#Add some logging and error handling
  def perform
    ActiveRecord::Base.connection_pool.with_connection do
      ControlledSubstance.pending.each do |cs|
        cs.update_attribute :status, "Denied"
      end
    end
  end
end

And how I'm calling it from whenever or from the rails console:

ControlledSubstanceAutoDenyJob.new.async.perform

If I need to move this to stack, that's fine but I didn't see anything in the wiki regarding this.

brandonhilkert commented 7 years ago

The arguments to perform are up to you. You can certainly pass current_user as an arg, or perhaps the user_id. I don't regularly use Devise, but I'm guessing you'd have to set user somehow to make it work properly. Hope this helps.