Class: BatchKit::Task::Definition

Inherits:
Definable
  • Object
show all
Defined in:
lib/batch-kit/framework/task_definition.rb

Overview

Captures details about a task definition - the job that it belongs to, the method name that performs the task work, etc.

Instance Attribute Summary collapse

Attributes inherited from Definable

#:description, #:instance, #:lock_name, #:name, #:runs

Instance Method Summary collapse

Methods inherited from Definable

#:lock_timeout=, #:lock_wait_timeout=, #add_aspect, add_properties, #event_name, inherited, properties, #set_from_options

Constructor Details

#initialize(job_class, method_name, task_name = nil) ⇒ Definition

Create a new Task::Definition object for the task defined in job_class in method_name.

Raises:

  • (ArgumentError)


26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/batch-kit/framework/task_definition.rb', line 26

def initialize(job_class, method_name, task_name = nil)
    raise ArgumentError, "job_class must be a Class" unless job_class.is_a?(Class)
    raise ArgumentError, "method_name must be a Symbol" unless method_name.is_a?(Symbol)
    job_defn = job_class.job
    raise ArgumentError, "job_class must have a Job::Definition" unless job_defn

    @name = task_name || method_name.to_s.gsub(/([^A-Z ])([A-Z])/, '\1 \2').
        gsub(/_/, ' ').gsub('::', ':').gsub(/\b([a-z])/) { $1.upcase }
    @job = job_defn
    @method_name = nil
    self.method_name = method_name
    @job << self
    super()
end

Instance Attribute Details

#:task_name(: task_name) ⇒ Object (readonly)

The name of the task (defaults to the method name).



16
17
18
19
20
21
# File 'lib/batch-kit/framework/task_definition.rb', line 16

add_properties(
    # Properties defined by a task declaration
    :job, :method_name,
    # Properties defined by persistence layer
    :task_id
)

Instance Method Details

#:job=(: job=(value)) ⇒ Object

The job that this task belongs to.



16
17
18
19
20
21
# File 'lib/batch-kit/framework/task_definition.rb', line 16

add_properties(
    # Properties defined by a task declaration
    :job, :method_name,
    # Properties defined by persistence layer
    :task_id
)

#:method_name=(: method_name=(value)) ⇒ Object

The name of the method that performs the work for this task.



16
17
18
19
20
21
# File 'lib/batch-kit/framework/task_definition.rb', line 16

add_properties(
    # Properties defined by a task declaration
    :job, :method_name,
    # Properties defined by persistence layer
    :task_id
)

#:task_id=(: task_id=(value)) ⇒ Object

A unique id for this Task::Definition, assigned by the persistence layer.



16
17
18
19
20
21
# File 'lib/batch-kit/framework/task_definition.rb', line 16

add_properties(
    # Properties defined by a task declaration
    :job, :method_name,
    # Properties defined by persistence layer
    :task_id
)

#create_run(job_obj, *args) ⇒ Object

Create a new Task::Run object for a run of this task.

Parameters:

  • job_obj (Object)

    The job object that is running this task.

  • args (Array<Object>)

    The arguments passed to the task method.



73
74
75
76
77
# File 'lib/batch-kit/framework/task_definition.rb', line 73

def create_run(job_obj, *args)
    task_run = Task::Run.new(self, job_obj, job_obj.job_run, *args)
    @runs << task_run
    task_run
end

#method_name=(mthd_name) ⇒ Object

Define a task method - the method to be run to trigger the execution of a task.

Parameters:

  • mthd_name (Symbol)

    The name of a method on the task class that is executed to begin the task processing. Note: This method must already exist on the task class when this setter is called, so that it can be wrapped in an aspect with before/after processing.



55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/batch-kit/framework/task_definition.rb', line 55

def method_name=(mthd_name)
    unless task_class.instance_methods.include?(mthd_name)
        raise ArgumentError, "Task class #{task_class.name} does not define a ##{mthd_name} method"
    end
    if @method_name
        raise "Task class #{task_class.name} already has a task method defined for ##{@method_name}"
    end
    @method_name = mthd_name

    # Add an aspect for executing task
    add_aspect(task_class, mthd_name)
end

#task_classObject

Return the class that defines the task.



43
44
45
# File 'lib/batch-kit/framework/task_definition.rb', line 43

def task_class
    @job.job_class
end

#to_sObject



80
81
82
# File 'lib/batch-kit/framework/task_definition.rb', line 80

def to_s
    "<BatchKit::Task::Definition #{task_class.name}##{@method_name}>"
end