1

I'm running a rails function as a delayed job. The problem is the session info that I had in place doesn't come across to the delayed job (I didn't expect it would). So my user is logged in and that log-in tells rails what postgres schema to hit.

Say user "dave" is assigned to schem 1.
He hits "run long process" with the id of the process as a parameter (say :id => 18). DJ gets the action, but can't find the long_process record find(18) as it returns nil. There's no record in the public tables so it won't be found there.

I tried to pass in my current user token, and I could get it across to the function. But I can't do much with it there. I want to switch user for the local scope of current action, as it's the only one that has rights to the tables. The next action the DJ hits is likely not for the same user, so it shouldn't have rights to the previous users tables.

Edit...


in Upload module
def self.find_ex(id)
    return ( (id.class == 'String' && id.count('-') > 0) ? 
                          find_by_guid(id) : find(id) );
end

in a Upload Controller

 def run_now
    @upload = Upload.find_ex(params[:id]);
    render :json=>@upload.to_hash(), status: 200  
    ew = EDMWorker.new
    ew.runUploadJob(params[:id])
  end

In it's own file

class EDMWorker
    def runUploadJob(id)
       upload = Upload.find_ex(id);
       upload.run_job();
    end 
    handle_asynchronously :runUploadJob, :priority => 10, :queue => 'public'  
end

TOP OF ERROR REPORT IN DELAYED JOB TABLE

Couldn't find Upload with id=68


The first bit works. The object is found and rendered perfectly.
The data is in "1".uploads.
The call in the delayed job "action" is (I suspect) being called against "public".uploads which should always be empty.
I can't call sign_in(user) in the delay job class, because the function is undefined. So I'm at a loss. How can I get the delayed job worker to know which session created its task?
baash05
  • 4,394
  • 11
  • 59
  • 97
  • Which class is the method belonging to? Or did you write a dedicated background process class? Can you post some code? – moritz Jun 13 '12 at 08:21
  • wrote dedicated process class. For now with one method. – baash05 Jun 13 '12 at 22:44
  • Can you post the code of Upload (specifically the find_ex method)? Is it a model? – moritz Jun 14 '12 at 11:03
  • It is a module.. And I did include it. the find_ex is there so the system can find an Upload based on guid or id, depending on what parameter is in the URL. – baash05 Jun 15 '12 at 01:52
  • So let me get this straight: You have multiple postgres databases and depending on which user is logged in to your rails app, ActiveRecord connects to a different database? – moritz Jun 15 '12 at 07:50
  • @mosch I've just noticed the application controller before_filter code isn't called during delayed job.. Does this make sense to you? – baash05 Jun 18 '12 at 00:40
  • No (controller) filters are called by delayed job. The `Upload.find_ex` method seems to be using the logged in user to scope its results. Is this the case? – moritz Jun 18 '12 at 08:22

0 Answers0