class BatchKit::Job

Public Class Methods

enabled=(val) click to toggle source
# File lib/batch-kit/framework/job.rb, line 31
def self.enabled=(val)
    @@enabled = val
end
inherited(sub_class) click to toggle source

Include ActsAsJob into any inheriting class

# 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) click to toggle source

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

# 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) click to toggle source

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

@param args [Array<String>] an array containin§g the command-line to

be processed by the job.
# 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 = '> ') click to toggle source

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

# 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

Public Instance Methods

with_lock(lock_name, lock_timeout, wait_timeout = nil, &blk) click to toggle source

Convenience method for using a lock within a job method

@param lock_name [String] The name of the lock to obtain during

execution of the block.

@param lock_timeout [Fixnum] The maximum time (in seconds) until the

lock should expire.

@param wait_timeout [Fixnum] An optional time (in seconds) to wait for

the lock to become available if it is already in use.
# 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