Ruby‎ > ‎

Queue

To queue up a worker job in the cloud, just create a worker object and then use the queue method.

    worker = ReportWorker.new
    worker.queue

This will send the job off to the SimpleWorker cloud. It's that simple.

You can set local variables within the worker. 

    worker = ReportWorker.new
    worker.user_id = user_id
    worker.queue

As well as loop through and send off many workers at a time. (In this case, we do one username per worker but for simple actions, you may want to include a larger set so as to better amortize the worker setup time.) 

    usernames.each do |username|
      worker.username = username
      worker.queue
    end

You can also queue up workers from within workers. (Make sure you use the merge_worker command so that the worker gets uploaded but cached separately.)

    class TaskControlWorker < SimpleWorker::Base

      merge_worker "task_worker", "Task_Worker"

      def run

        @users = User.find(:all)
        @users.each do |user|
          user_worker = TaskWorker.new
          user_worker.user_id = user.id
          user_worker.queue
        end

      end

    end


Arguments


Optional:


-  priority: —  Priority queue to run the job in (0, 1, 2). p0 is default. 
-  timeout: — Time in seconds after which to terminate the task. 

Priority

Passing a priority of 1 or 2 will cause the job to move through the queue faster. Note that higher priorities cost more, see Pricing for details.

    worker.queue(:priority=>1)

    worker.queue(:priority=>2)


We recommend using higher priorities for tasks where you'd like an action performed or a response back that's time-critical as well as for test/development. For test/dev you can also use the run_local command especially during initial development/debugging of the worker.

    #worker.queue
    worker.run_local

Timeout

By default, each job has 60 minutes (3600 seconds to complete) after which it is terminated. If you want to set a specific time less than 3600 seconds, pass in an explicit timeout value. In the example, below the task will terminate if it runs longer than 1800 seconds (30 minutes).

    worker.queue(:timeout=>1800)

The maximum value for timeout is 3600 seconds.  This is to ensure that lots of long running or stalled jobs do not overload the system affecting quality of service.  We encourage you to break up long running tasks into separate workers and run them in parallel.  Please let us know if you have a reason for a timeout of longer than 60 minutes.



Queuing Workers from a Different Application/System


This is useful in situations when you're worker does something that is entirely separate from your application. Let's say, for example, you want to do some image encoding or something equally as extensive and you don't want the gems, binary code, and other files as dependencies in your Rails app. 

To do this, you first create your workers and get them working on SimpleWorker. After they are working and your code is ready to be run on SimpleWorker, you can queue it up from a different application by using the name and passing the correct data payload. For instance:

    data[:attr_encoded] = Base64.encode64({'@name'=>'Travis'}.to_json)
    data[:sw_config] = SimpleWorker.config.get_atts_to_send
    SimpleWorker.service.queue('HelloWorker', data)



Comments