Class: BatchKit::Job

Inherits:
Object
  • Object
show all
Includes:
Arguments, Configurable, Loggable
Defined in:
lib/batch-kit/framework/job.rb,
lib/batch-kit/framework/job_run.rb,
lib/batch-kit/framework/job_definition.rb

Defined Under Namespace

Classes: Definition, Run

Constant Summary collapse

@@enabled =

A class variable for controlling whether jobs run; defaults to true. Provides a means for orchestration programs to prevent the running of jobs on require when jobs need to be runnable as standalone progs.

true

Instance Attribute Summary

Attributes included from Arguments

#arguments

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Loggable

#log

Methods included from Configurable

#config, included

Methods included from Arguments

included, #parse_arguments

Class Method Details

.enabled=(val) ⇒ Object



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

def self.enabled=(val)
    @@enabled = val
end

.inherited(sub_class) ⇒ Object

Include ActsAsJob into any inheriting class



20
21
22
23
24
# File 'lib/batch-kit/framework/job.rb', line 20

def self.inherited(sub_class)
    sub_class.class_eval do
        include ActsAsJob
    end
end

.run(args = ARGV) ⇒ Object

A method that instantiates an instance of this job, parses arguments from the command-line, and then executes the job.



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

def self.run(args = ARGV)
    if @@enabled
        if args.length == 0 && self.args_def.keys.length > 0
            shell
        else
            run_once(args)
        end
    end
end

.run_once(args, show_usage_on_error = true) ⇒ Object

Instantiates and executes a job, using the supplied arguments args.



53
54
55
56
57
58
59
60
# File 'lib/batch-kit/framework/job.rb', line 53

def self.run_once(args, show_usage_on_error = true)
    job = self.new
    job.parse_arguments(args, show_usage_on_error)
    unless self.job.method_name
        raise "No job entry method has been defined; use job :<method_name> or job do ... end in your class"
    end
    job.send(self.job.method_name)
end

.shell(prompt = '> ') ⇒ Object

Starts an interactive shell for this job. Each command line entered is passed to a new instance of the job for execution.



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

def self.shell(prompt = '> ')
    require 'readline'
    require 'csv'
    puts "Starting interactive shell... enter 'exit' to quit"
    while true do
        args = Readline.readline(prompt, true)
        break if args == 'exit' || args == 'quit'
        begin
            run_once(CSV.parse_line(args, col_sep: ' '), false)
        rescue Exception
        end
    end
end

Instance Method Details

#with_lock(lock_name, lock_timeout, wait_timeout = nil, &blk) ⇒ Object

Convenience method for using a lock within a job method



88
89
90
# File 'lib/batch-kit/framework/job.rb', line 88

def with_lock(lock_name, lock_timeout, wait_timeout = nil, &blk)
    self.job_run.with_lock(lock_name, lock_timeout, wait_timeout, &blk)
end