class Procrastinator::Scheduler

A Scheduler object provides the API for client applications to manage scheduled tasks.

Use Scheduler#defer to schedule new tasks, Scheduler#reschedule to alter existing tasks, and Scheduler#cancel to remove unwanted tasks.

@author Robin Miller

Public Class Methods

new(config) click to toggle source
# File lib/procrastinator/scheduler.rb, line 13
def initialize(config)
   @config = config
end

Public Instance Methods

cancel(queue, identifier) click to toggle source

Removes an existing task, as located by the given identifying information.

The identifier can include any data field stored in the task loader. Often this is the information in :data.

@param queue [Symbol] the symbol identifier for the queue to add a new task on @param identifier [Hash] Some identifying information to find the appropriate task.

@see TaskMetaData

# File lib/procrastinator/scheduler.rb, line 61
def cancel(queue, identifier)
   queue = @config.queue(name: queue)

   tasks = queue.read(identifier.merge(queue: queue.name.to_s))

   raise "no task matches search: #{ identifier }" if tasks.empty?
   raise "multiple tasks match search: #{ identifier }" if tasks.size > 1

   queue.delete(tasks.first[:id])
end
defer(queue_name = nil, data: nil, run_at: Time.now, expire_at: nil) click to toggle source

Records a new task to be executed at the given time.

@param queue_name [Symbol] the symbol identifier for the queue to add a new task on @param run_at [Time, Integer] Optional time when this task should be executed. Defaults to the current time. @param data [Hash, Array, String, Integer] Optional simple data object to be provided to the task on execution. @param expire_at [Time, Integer] Optional time when the task should be abandoned

# File lib/procrastinator/scheduler.rb, line 23
      def defer(queue_name = nil, data: nil, run_at: Time.now, expire_at: nil)
         raise ArgumentError, <<~ERR unless queue_name.nil? || queue_name.is_a?(Symbol)
            must provide a queue name as the first argument. Received: #{ queue_name }
         ERR

         queue = @config.queue(name: queue_name)

         queue.create(run_at: run_at, expire_at: expire_at, data: data)
      end
Also aliased as: delay
delay(queue_name = nil, data: nil, run_at: Time.now, expire_at: nil)
Alias for: defer
reschedule(queue, identifier) click to toggle source

Alters an existing task to run at a new time, expire at a new time, or both.

Call to on the result and pass in the new :run_at and/or :expire_at.

Example:

scheduler.reschedule(:alerts, data: {user_id: 5}).to(run_at: Time.now, expire_at: Time.now + 10)

The identifier can include any data field stored in the task loader. Often this is the information in :data.

@param queue [Symbol] the symbol identifier for the queue to add a new task on @param identifier [Hash] Some identifying information to find the appropriate task.

@see TaskMetaData

# File lib/procrastinator/scheduler.rb, line 49
def reschedule(queue, identifier)
   UpdateProxy.new(@config.queue(name: queue), identifier: identifier)
end
work(*queue_names) click to toggle source

Spawns a new worker thread for each queue defined in the config

@param queue_names [Array<String,Symbol>] Names of specific queues to act upon.

Omit or leave empty to act on all queues.
# File lib/procrastinator/scheduler.rb, line 76
def work(*queue_names)
   queue_names = @config.queues if queue_names.empty?

   workers = queue_names.collect do |queue_name|
      QueueWorker.new(queue: queue_name, config: @config)
   end

   WorkProxy.new(workers, @config)
end