Search code examples
ruby-on-railsrubyresque

send object as parameter in resque


sorry for my newbie question.

I'm sending current_user helper that have User object type as resque job runner parameter like this

      JobRunner.run LikerCommenterAnalyzerJob, current_user, session['basic_instagram_data']['access_token'], 505

but when I try to get current_user parameter in perform method of resque it changed to Hash type

def self.perform (current_user, access_token, archive_id)

account = current_user.InstagramAccount.where('access_token', access_token).first

end

and my error is

undefined method `InstagramAccount' for #<Hash:0x007f9e39c27458>

Solution

  • Basically, when you push a task to resque, it will store somewhere to run later (Redis for example). So it can't store your object, it just stores the description of your passed parameters, the Hash above is the description.

    Then Resque pulls down the description to run it.

    So the solution will be, put the information that you use to retrieve the object instead (this is best practice)

    When pushing the task:

    JobRunner.run LikerCommenterAnalyzerJob, current_user.id, session['basic_instagram_data']['access_token'], 505
    

    When runing the task

    def self.perform (user_id, access_token, archive_id)
      user = User.find(user_id)
      account = user.InstagramAccount.where('access_token', access_token).first
    end