Class: BatchKit::Runnable
- Inherits:
-
Object
- Object
- BatchKit::Runnable
- 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
Instance Attribute Summary collapse
-
#definition ⇒ Object
readonly
The definition object for this runnable.
-
#end_time ⇒ Object
readonly
Time at which processing completed (or nil).
-
#exception ⇒ Object
Exception thrown that caused process to fail.
-
#exit_code ⇒ Object
readonly
Exit code of the process.
-
#instance ⇒ Object
readonly
The instance qualifier for this runnable, if it has an instance qualifier.
-
#lock_name ⇒ Object
readonly
Name of any exclusive lock needed by this run.
-
#lock_timeout ⇒ Object
readonly
Number of seconds before the lock times out.
-
#lock_wait_timeout ⇒ Object
readonly
Number of seconds to wait for the lock to be released before giving up.
-
#object ⇒ Object
readonly
The object instance that is running this runnable.
-
#start_time ⇒ Object
readonly
Time at which processing began (or nil).
-
#status ⇒ Object
readonly
Current status of this process.
Class Method Summary collapse
-
.add_delegated_properties(*props) ⇒ Object
Add delegates for each specified property in
props
.
Instance Method Summary collapse
-
#abort(process_obj) ⇒ Object
Called if a batch process is aborted.
-
#around_execute(process_obj, *args) { ... } ⇒ Object
Called as the process is executing.
-
#elapsed ⇒ Object
Returns the elapsed time in seconds.
-
#event_name(event) ⇒ Object
Returns an event name for publication, based on the sub-class of Runnable that is triggering the event.
-
#failure(process_obj, exception) ⇒ Object
Called after the process executes and fails.
-
#initialize(definition, obj, run_args) ⇒ Runnable
constructor
Sets the state of the runnable to :initialized.
-
#label ⇒ Object
A label consisting of the name and any instance qualifier.
-
#post_execute(process_obj, success) ⇒ Object
Called after the process executes.
-
#pre_execute(process_obj, *args) ⇒ Boolean
A pre-execute pointcut for execution of a process.
-
#success(process_obj, result) ⇒ Object
Called after the process executes and completes successfully.
Methods included from Lockable
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
#definition ⇒ Object (readonly)
The definition object for this runnable
31 32 33 |
# File 'lib/batch-kit/framework/runnable.rb', line 31 def definition @definition end |
#end_time ⇒ Object (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 |
#exception ⇒ Object
Exception thrown that caused process to fail
53 54 55 |
# File 'lib/batch-kit/framework/runnable.rb', line 53 def exception @exception end |
#exit_code ⇒ Object (readonly)
Exit code of the process
51 52 53 |
# File 'lib/batch-kit/framework/runnable.rb', line 51 def exit_code @exit_code end |
#instance ⇒ Object (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_name ⇒ Object (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_timeout ⇒ Object (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_timeout ⇒ Object (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 |
#object ⇒ Object (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_time ⇒ Object (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 |
#status ⇒ Object (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.
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.
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 |
#elapsed ⇒ Object
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.
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 |
#label ⇒ Object
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.
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.
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.
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 |