Class: BatchKit::Runnable

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Lockable
Defined in:
lib/batch-kit/framework/runnable.rb

Overview

Captures details of a single execution of a runnable batch process, e.g. a Task or Job.

Direct Known Subclasses

Job::Run, Sequence::Run, Task::Run

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Lockable

#lock, #unlock, #with_lock

Constructor Details

#initialize(definition, obj, run_args) ⇒ Runnable

Sets the state of the runnable to :initialized.



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/batch-kit/framework/runnable.rb', line 64

def initialize(definition, obj, run_args)
    @definition = definition
    @object = obj
    @instance = eval_property_expr(definition.instance, obj, run_args)
    @status = :initialized
    @lock_name = eval_property_expr(definition.lock_name, obj, run_args)
    @lock_timeout = case definition.lock_timeout
        when Numeric then definition.lock_timeout
        when String then eval_property_expr(definition.lock_timeout, obj, run_args, :to_i)
    end
    @lock_wait_timeout = case definition.lock_wait_timeout
        when Numeric then definition.lock_wait_timeout
        when String then eval_property_expr(definition.lock_wait_timeout, obj, run_args, :to_i)
    end
    Events.publish(self, event_name('initialized'))
end

Instance Attribute Details

#definitionObject (readonly)

The definition object for this runnable



31
32
33
# File 'lib/batch-kit/framework/runnable.rb', line 31

def definition
  @definition
end

#end_timeObject (readonly)

Time at which processing completed (or nil)



49
50
51
# File 'lib/batch-kit/framework/runnable.rb', line 49

def end_time
  @end_time
end

#exceptionObject

Exception thrown that caused process to fail



53
54
55
# File 'lib/batch-kit/framework/runnable.rb', line 53

def exception
  @exception
end

#exit_codeObject (readonly)

Exit code of the process



51
52
53
# File 'lib/batch-kit/framework/runnable.rb', line 51

def exit_code
  @exit_code
end

#instanceObject (readonly)

The instance qualifier for this runnable, if it has an instance qualifier.



36
37
38
# File 'lib/batch-kit/framework/runnable.rb', line 36

def instance
  @instance
end

#lock_nameObject (readonly)

Name of any exclusive lock needed by this run



55
56
57
# File 'lib/batch-kit/framework/runnable.rb', line 55

def lock_name
  @lock_name
end

#lock_timeoutObject (readonly)

Number of seconds before the lock times out



57
58
59
# File 'lib/batch-kit/framework/runnable.rb', line 57

def lock_timeout
  @lock_timeout
end

#lock_wait_timeoutObject (readonly)

Number of seconds to wait for the lock to be released before giving up



59
60
61
# File 'lib/batch-kit/framework/runnable.rb', line 59

def lock_wait_timeout
  @lock_wait_timeout
end

#objectObject (readonly)

The object instance that is running this runnable



33
34
35
# File 'lib/batch-kit/framework/runnable.rb', line 33

def object
  @object
end

#start_timeObject (readonly)

Time at which processing began (or nil)



47
48
49
# File 'lib/batch-kit/framework/runnable.rb', line 47

def start_time
  @start_time
end

#statusObject (readonly)

Current status of this process. One of the following states:

:initialized
:skipped
:executing
:completed
:failed
:aborted


45
46
47
# File 'lib/batch-kit/framework/runnable.rb', line 45

def status
  @status
end

Class Method Details

.add_delegated_properties(*props) ⇒ Object

Add delegates for each specified property in props.



22
23
24
25
# File 'lib/batch-kit/framework/runnable.rb', line 22

def add_delegated_properties(*props)
    del_props = props.reject{ |prop| self.instance_methods.include?(prop) }
    def_delegators :@definition, *del_props
end

Instance Method Details

#abort(process_obj) ⇒ Object

Called if a batch process is aborted.

Parameters:

  • process_obj (Object)

    Object that is executing the batch process.



179
180
181
182
# File 'lib/batch-kit/framework/runnable.rb', line 179

def abort(process_obj)
    @status = :aborted
    Events.publish(process_obj, event_name('abort'), self)
end

#around_execute(process_obj, *args) { ... } ⇒ Object

Called as the process is executing.

Parameters:

  • process_obj (Object)

    Object that is executing the batch process.

  • args (*Object)

    Any arguments passed to the method that is executing the process.

Yields:

  • at the point when the process should execute.



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
# File 'lib/batch-kit/framework/runnable.rb', line 132

def around_execute(process_obj, *args, &blk)
    @start_time = Time.now
    @status = :executing
    @exit_code = nil
    Events.publish(process_obj, event_name('execute'), self, *args)
    begin
        if @lock_name
            self.with_lock(@lock_name, @lock_timeout, @lock_wait_timeout, &blk)
        else
            yield
        end
    ensure
        @end_time = Time.now
    end
end

#elapsedObject

Returns the elapsed time in seconds



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

def elapsed
    @start_time ? (@end_time || Time.now) - @start_time : 0
end

#event_name(event) ⇒ Object

Returns an event name for publication, based on the sub-class of Runnable that is triggering the event.



84
85
86
# File 'lib/batch-kit/framework/runnable.rb', line 84

def event_name(event)
    "#{self.class.name.split('::')[1..-1].join('_').downcase}.#{event}"
end

#failure(process_obj, exception) ⇒ Object

Called after the process executes and fails.

Parameters:

  • process_obj (Object)

    Object that is executing the batch process.

  • exception (Exception)

    The exception that caused this runnable to fail.



167
168
169
170
171
172
# File 'lib/batch-kit/framework/runnable.rb', line 167

def failure(process_obj, exception)
    @status = :failed
    @exit_code = 1 unless @exit_code
    @exception = exception
    Events.publish(process_obj, event_name('failure'), self, exception)
end

#labelObject

Returns a label consisting of the name and any instance qualifier.

Returns:

  • a label consisting of the name and any instance qualifier.



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

def label
    lbl = @definition.name.gsub(/_/, ' ').gsub(/\b([a-z])/) { $1.upcase }
    @instance ? "#{lbl} [#{@instance}]" : lbl
end

#post_execute(process_obj, success) ⇒ Object

Called after the process executes.

Parameters:

  • process_obj (Object)

    Object that is executing the batch process.

  • success (Boolean)

    True if the process completed without throwing an exception.



191
192
193
194
# File 'lib/batch-kit/framework/runnable.rb', line 191

def post_execute(process_obj, success)
    Events.publish(process_obj, event_name('post-execute'), self, success)
    @object = nil
end

#pre_execute(process_obj, *args) ⇒ Boolean

A pre-execute pointcut for execution of a process. Return value determines whether execution should proceed.

Parameters:

  • process_obj (Object)

    Object that is executing the batch process.

  • args (*Object)

    Any arguments passed to the method that is executing the process.

Returns:

  • (Boolean)

    True if the process should proceed, or false if it should be skipped.



111
112
113
114
115
116
117
118
119
120
121
122
# File 'lib/batch-kit/framework/runnable.rb', line 111

def pre_execute(process_obj, *args)
    if Events.has_subscribers?(process_obj, event_name('pre-execute'))
        run = Events.publish(process_obj, event_name('pre-execute'), self, *args)
    else
        run = true
    end
    unless run
        @status = :skipped unless run
        Events.publish(process_obj, event_name('skipped'), self, *args)
    end
    run
end

#success(process_obj, result) ⇒ Object

Called after the process executes and completes successfully.

Parameters:

  • process_obj (Object)

    Object that is executing the batch process.

  • result (Object)

    The return value of the process.



154
155
156
157
158
# File 'lib/batch-kit/framework/runnable.rb', line 154

def success(process_obj, result)
    @status = :completed
    @exit_code = 0 unless @exit_code
    Events.publish(process_obj, event_name('success'), self, result)
end