Class: BatchKit::Job::Definition

Inherits:
Definable show all
Defined in:
lib/batch-kit/framework/job_definition.rb

Overview

Captures details about a job definition - the class of the job, the server it runs on, the file it is defined in, 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, job_file, job_name = nil) ⇒ Definition

Create a new job Definition object for the job defined in job_class in job_file.

Raises:

  • (ArgumentError)


38
39
40
41
42
43
44
45
46
47
48
# File 'lib/batch-kit/framework/job_definition.rb', line 38

def initialize(job_class, job_file, job_name = nil)
    raise ArgumentError, "job_class must be a Class" unless job_class.is_a?(Class)
    @job_class = job_class
    @file = job_file
    @name = job_name || job_class.name.gsub(/([^A-Z ])([A-Z])/, '\1 \2').
        gsub(/_/, ' ').gsub('::', ':').gsub(/\b([a-z])/) { $1.upcase }
    @computer = Socket.gethostname
    @method_name = nil
    @tasks = {}
    super()
end

Instance Attribute Details

#:computer(: computer) ⇒ Object (readonly)

The name of the machine on which the job was instantiated.



28
29
30
31
32
33
# File 'lib/batch-kit/framework/job_definition.rb', line 28

add_properties(
    # Properties from job/task declarations
    :job_class, :method_name, :computer, :file, :do_not_track, :tasks,
    # Properties provided by persistence layer
    :job_id, :job_version
)

#:file(: file) ⇒ Object (readonly)

The name of the file containing the job code.



28
29
30
31
32
33
# File 'lib/batch-kit/framework/job_definition.rb', line 28

add_properties(
    # Properties from job/task declarations
    :job_class, :method_name, :computer, :file, :do_not_track, :tasks,
    # Properties provided by persistence layer
    :job_id, :job_version
)

Instance Method Details

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

By default, job executions may be recorded (if a persistence layer is available). This attribute can be used by jobs to indicate that runs of this job should not be recorded.



28
29
30
31
32
33
# File 'lib/batch-kit/framework/job_definition.rb', line 28

add_properties(
    # Properties from job/task declarations
    :job_class, :method_name, :computer, :file, :do_not_track, :tasks,
    # Properties provided by persistence layer
    :job_id, :job_version
)

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

The class that defines the job.



28
29
30
31
32
33
# File 'lib/batch-kit/framework/job_definition.rb', line 28

add_properties(
    # Properties from job/task declarations
    :job_class, :method_name, :computer, :file, :do_not_track, :tasks,
    # Properties provided by persistence layer
    :job_id, :job_version
)

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

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



28
29
30
31
32
33
# File 'lib/batch-kit/framework/job_definition.rb', line 28

add_properties(
    # Properties from job/task declarations
    :job_class, :method_name, :computer, :file, :do_not_track, :tasks,
    # Properties provided by persistence layer
    :job_id, :job_version
)

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

A version number for the job.



28
29
30
31
32
33
# File 'lib/batch-kit/framework/job_definition.rb', line 28

add_properties(
    # Properties from job/task declarations
    :job_class, :method_name, :computer, :file, :do_not_track, :tasks,
    # Properties provided by persistence layer
    :job_id, :job_version
)

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

The method that is run to execute the job.



28
29
30
31
32
33
# File 'lib/batch-kit/framework/job_definition.rb', line 28

add_properties(
    # Properties from job/task declarations
    :job_class, :method_name, :computer, :file, :do_not_track, :tasks,
    # Properties provided by persistence layer
    :job_id, :job_version
)

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

A hash of task method names to Task::Definition objects capturing details of each task that is defined for this Job::Definition.



28
29
30
31
32
33
# File 'lib/batch-kit/framework/job_definition.rb', line 28

add_properties(
    # Properties from job/task declarations
    :job_class, :method_name, :computer, :file, :do_not_track, :tasks,
    # Properties provided by persistence layer
    :job_id, :job_version
)

#<<(task) ⇒ Object

Add a record of a run of the job, or details about a task that the job performs.



74
75
76
77
78
79
80
81
82
83
# File 'lib/batch-kit/framework/job_definition.rb', line 74

def <<(task)
    unless task.is_a?(Task::Definition)
        raise ArgumentError, "Only a Task::Definition can be added to a Job::Definition"
    end
    key = task.method_name
    if @tasks.has_key?(key)
        raise ArgumentError, "#{self} already has a task for ##{key}"
    end
    @tasks[key] = task
end

#create_run(job_obj, *args) ⇒ Object

Create a new Job::Run object for a run of thie job.

Parameters:

  • job_obj (Object)

    The job object that is running this job.

  • args (Array<Object>)

    The arguments passed to the job method.



90
91
92
93
94
# File 'lib/batch-kit/framework/job_definition.rb', line 90

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

#method_name=(mthd_name) ⇒ Object

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

Parameters:

  • mthd_name (Symbol)

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



58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/batch-kit/framework/job_definition.rb', line 58

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

    # Add an aspect for executing job
    add_aspect(job_class, mthd_name)
end

#to_sObject



97
98
99
# File 'lib/batch-kit/framework/job_definition.rb', line 97

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