module BatchKit::ActsAsJob::ClassMethods

Define methods to be added to the class that includes this module.

Public Instance Methods

definition()
Alias for: job_definition
desc(desc) click to toggle source

Captures a description for the following task or job definition.

@param desc [String] The description to associate with the next

task or job that is defined.
# File lib/batch-kit/framework/acts_as_job.rb, line 39
def desc(desc)
    @__desc__ = desc
end
job(job_method = nil, job_opts = @__desc__, &body) click to toggle source

Defines the method that is used to run this job. This may be an existing method, in which case the name of the method must be passed as the first argument. Alternatively, a block may be supplied, which will be used to create the job method.

@param job_method [Symbol] The name of an existing method that is

to be the job entry point.

@param job_opts [Hash] Options that affect the job definition. @option job_opts [Symbol] :method_name The name to be assigned to

the job method created from the supplied block. Default is
:execute.

@option job_opts [String] :description A description for the job.

# File lib/batch-kit/framework/acts_as_job.rb, line 57
def job(job_method = nil, job_opts = @__desc__, &body)
    # If called as an accessor, just return the @__job__
    if  job_method || job_opts || body
        unless job_method.is_a?(Symbol)
            job_opts = job_method
            job_method = (job_opts && job_opts.is_a?(Hash) &&
                job_opts[:method_name]) || :execute
        end

        job_desc = nil
        if job_opts.is_a?(Hash)
            job_desc = @__desc__
        elsif job_opts.is_a?(String)
            job_desc = job_opts
            job_opts = {}
        elsif job_opts.nil?
            job_opts = {}
        end
        @__desc__ = nil

        # Define job method if a body block was supplied
        define_method(job_method, &body) if body

        opts = job_opts.clone
        opts[:description] = job_desc unless opts[:description]
        opts[:method_name] = job_method
        # The @__job__ instance variable is crated when this module is included
        @__job__.set_from_options(opts)
    end
    @__job__
end
job_definition() click to toggle source

@return The Job::Definition object used to hold attributes of this

job.
# File lib/batch-kit/framework/acts_as_job.rb, line 29
def job_definition
    @__job__
end
Also aliased as: definition
on_completion(mthd = nil, &blk) click to toggle source

Defines a handler to be invoked on completion of the job, whether the job completes successfully or fails. The handler may be specified as either a method name and/or via a block. Multiple calls to this method can be made to register multiple callbacks if desired.

@param mthd [Symbol] The name of an existing method on the including

class. This method will be called with the Job::Run object that
represents the completing job run.
# File lib/batch-kit/framework/acts_as_job.rb, line 160
def on_completion(mthd = nil, &blk)
    Events.subscribe(self, 'job_run.post-execute'){ |jr, obj, ok| obj.send(mthd, jr) } if mthd
    Events.subscribe(self, 'job_run.post-execute'){ |jr, obj, ok| obj.instance_exec(jr, &blk) } if blk
end
on_failure(mthd = nil, &blk) click to toggle source

Defines a handler to be invoked if the job encounters an unhandled exception.

# File lib/batch-kit/framework/acts_as_job.rb, line 138
def on_failure(mthd = nil, &blk)
    Events.subscribe(self, 'job_run.failure'){ |jr, obj, ex| obj.send(mthd, ex) } if mthd
    Events.subscribe(self, 'job_run.failure'){ |jr, obj, ex| obj.instance_exec(ex, &blk) } if blk
end
on_success(mthd = nil, &blk) click to toggle source

Defines a handler to be invoked if the job ends successfully.

# File lib/batch-kit/framework/acts_as_job.rb, line 145
def on_success(mthd = nil, &blk)
    Events.subscribe(self, 'job_run.success'){ |jr, obj| obj.send(mthd) } if mthd
    Events.subscribe(self, 'job_run.success'){ |jr, obj| obj.instance_exec(&blk) } if blk
end
task(task_method, task_opts = @__desc__, &body) click to toggle source

Defines the method that is used to run a task. This may be an existing method, in which case the name of the method must be passed as the first argument. Alternatively, a block may be supplied, which will be used to create the task method.

@param task_method [Symbol] The name for the method that is to be

this task. May be the name of an existing method (in which case
no block should be supplied), or the name to give to the method
that will be created from the supplied block.

@param task_opts [Hash] A hash containing options for the task

being defined.

@option task_opts [Symbol] :method_name The name for the method

if no symbol was provided as the first argument.

@option job_opts [String] :description A description for the task.

# File lib/batch-kit/framework/acts_as_job.rb, line 105
def task(task_method, task_opts = @__desc__, &body)
    unless task_method.is_a?(Symbol)
        task_opts = task_method
        task_method = task_opts && task_opts[:method_name]
    end
    raise ArgumentError, "No method name specified for task" unless task_method

    task_desc = nil
    if task_opts.is_a?(Hash)
        task_desc = @__desc__
    elsif task_opts.is_a?(String)
        task_desc = task_opts
        task_opts = {}
    elsif task_opts.nil?
        task_opts = {}
    end
    @__desc__ = nil

    # Define task method if a body block was supplied
    define_method(task_method, &body) if body

    opts = task_opts.clone
    opts[:description] = task_desc unless opts[:description]

    # Create a new TaskDefinition class for the task
    task_defn = Task::Definition.new(self, task_method)
    task_defn.set_from_options(opts)
    task_defn
end