tags:

views:

172

answers:

2

In my apps/controllers/model_controller.rb I have (names of models/methods changed to protect the innocent):

def background_sync
 @background_task_uid = Model.async_process_model_cache({:name => 'name'})
 @model_sync = ModelSync.new  # Adds a new record in the queue of pending jobs
 @model_sync.process_id = @background_task_uid  # Puts the background process id into the new ModelSync record
 @model_sync.save
end

In app/workers/model_worker.rb:

def process_model_cache(options={})
     [long background task]
     result = Workling::Return::Store.set(options[:uid], 'done')
     result = Workling::Return::Store.get(options[:uid])  #=>  'done'      
end

Notice that the set and get are functioning properly here within this worker. The problem is later on...

Back in app/views/model/index.html.rb, I have a prototype helper polling a request to the same controller to determine whether the background job is complete:

<%= periodically_call_remote( :url => { :action => :background_complete }, :frequency => 5, :update => 'status_div') %>

And in apps/controllers/model_controller.rb, the function for checking the status of the background job:

def background_complete
 @background_task_uid = ModelSync.find(:last)
 if @background_task_uid
  @background_task_uid.each do |task|
    unless task.process_id == "" || task.process_id.nil?
      @result = Workling::Return::Store.get(task.process_id) #=> nil
      if @result.nil?
        task.destroy
      end
    else
      task.destroy
    end
    unless @result.nil?
      render :text => "<span style='font-size:12px;margin-left:20px;'>"+@result+"</span>"
    else
      @result = "none" if @result.nil?
      render :text => "<span style='font-size:12px;margin-left:20px;'>"+@result+"</span>"
    end
   end
  end
end

And finally, in config/environments/development.rb:

Workling::Return::Store.instance = Workling::Return::Store::MemoryReturnStore.new
Workling::Remote.dispatcher = Workling::Remote::Runners::StarlingRunner.new

(Note that I've tried running this with and without the last line commented. If commented out, Workling reverts to Spawn rather than Starling.)

So the problem is that I get nil from this line in background_complete:

@result = Workling::Return::Store.get(task.process_id) #=> nil
A: 

Found the answer to this question. The fix is to remove the Workling::Return::Store.instance line from config/environments/development.rb

Then replace the get AND set calls as follows:

In app/workers/model_worker.rb:

store = Workling::Return::Store::StarlingReturnStore.new
key, value = @uid, @progress
store.set(key, value)

In app/controllers/models_controller.rb:

store = Workling::Return::Store::StarlingReturnStore.new
@result = store.get(task.process_id)

Obviously, there is a way to declare a shortcut in environments.rb to avoid calling a new StarlingReturnStore each time, but I'm out of my depth because I can't make that work.

Anyway, this fix works for me. I'm getting the output from each background job to report via set to the get in the controller, which then gets captured by the AJAX call and reported to the page via RJS.

Nice!

A: 

I know it is a year since you asked this question, but just getting into Starling now, myself, so didn't see this till now.

But it looks like your problem was (from development.rb):

Workling::Return::Store.instance = Workling::Return::Store::MemoryReturnStore.new Workling::Remote.dispatcher = Workling::Remote::Runners::StarlingRunner.new

It needed to be:

Workling::Return::Store.instance = Workling::Return::Store::StarlingReturnStore.new Workling::Remote.dispatcher = Workling::Remote::Runners::StarlingRunner.new

At least for the benefit of those google searchers out there... :)

Dean Hallman