Module: BatchKit::ActsAsJob::ClassMethods

Defined in:
lib/batch-kit/framework/acts_as_job.rb

Overview

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

Instance Method Summary collapse

Instance Method Details

#desc(desc) ⇒ Object

Captures a description for the following task or job definition.

Parameters:

  • desc (String)

    The description to associate with the next task or job that is defined.


39
40
41
# 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) ⇒ Object

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.

Parameters:

  • job_method (Symbol) (defaults to: nil)

    The name of an existing method that is to be the job entry point.

  • job_opts (Hash) (defaults to: @__desc__)

    Options that affect the job definition.

Options Hash (job_opts):

  • :method_name (Symbol)

    The name to be assigned to the job method created from the supplied block. Default is :execute.

  • :description (String)

    A description for the job.


57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# 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_definitionObject Also known as: definition

Returns The Job::Definition object used to hold attributes of this job.

Returns:

  • The Job::Definition object used to hold attributes of this job.


29
30
31
# File 'lib/batch-kit/framework/acts_as_job.rb', line 29

def job_definition
    @__job__
end

#on_completion(mthd = nil, &blk) ⇒ Object

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.

Parameters:

  • mthd (Symbol) (defaults to: nil)

    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.


160
161
162
163
# 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) ⇒ Object

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


138
139
140
141
# 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) ⇒ Object

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


145
146
147
148
# 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) ⇒ Object

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.

Parameters:

  • 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.

  • task_opts (Hash) (defaults to: @__desc__)

    A hash containing options for the task being defined.

  • job_opts (Hash)

    a customizable set of options

Options Hash (task_opts):

  • :method_name (Symbol)

    The name for the method if no symbol was provided as the first argument.

Raises:

  • (ArgumentError)

105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
# 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