Class: Job
- Inherits:
-
Object
- Object
- Job
- Defined in:
- lib/job.rb
Overview
Job is a simplistic, atomic job processor that works off of the filesystem to get it’s list of tasks. It executes these tasks in order of time, oldest first. Each task is named and assigned a procedure to execute when it is encountered, but named tasks do not necessarily require execution at any point.
Example:
jt1 = JobTask.new do
puts "This is job one"
0
end
jt2 = JobTask.new do
puts "This is job two"
1
end
# jobs require a directory where they can place their files.
job = Job.new '/Users/erikh/tmp'
# naming a task
job.name_task jt1, :one
job.name_task jt2, :two
# adding jobs
job.add_job :two
# this is necessary in this example to create a delay of time
sleep 10
job.add_job :one
# run returns the results for further evaluation and
# short-circuiting.
job.run do |result|
puts result
end
# Note that this does not clobber names
job.delete_all_jobs
job.add_job :one
sleep 10
job.add_job :two
# run_obliviously does not care about the result.
job.run_obliviously
Instance Attribute Summary collapse
-
#jobdir ⇒ Object
readonly
Returns the value of attribute jobdir.
Instance Method Summary collapse
-
#add_job(name) ⇒ Object
Adds a job.
-
#delete_all_jobs ⇒ Object
Deletes all jobs.
-
#delete_job_by_name(name) ⇒ Object
This deletes a job by its name.
-
#initialize(jobdir) ⇒ Job
constructor
Creates a new Job object.
-
#name_task(task, name) ⇒ Object
Names a task.
-
#run ⇒ Object
Runs the tasks in order yielding for each result, allowing the user to back out if problems occur processing the job chain.
-
#run_obliviously ⇒ Object
This runs without checking the job results, but will return the result of the last task executed.
Constructor Details
#initialize(jobdir) ⇒ Job
Creates a new Job object. Requires that the jobdir be defined – it does not necessarily have to exist already, it will be created.
96 97 98 99 100 101 |
# File 'lib/job.rb', line 96 def initialize(jobdir) fail "Must define jobdir" unless jobdir @jobdir = jobdir.dup @jobdir.freeze @jobnames = Hash.new end |
Instance Attribute Details
#jobdir ⇒ Object (readonly)
Returns the value of attribute jobdir.
90 91 92 |
# File 'lib/job.rb', line 90 def jobdir @jobdir end |
Instance Method Details
#add_job(name) ⇒ Object
Adds a job. This does not check to see if the job is named yet, so run will ignore this job if there is no named task to go along with it.
108 109 110 111 112 113 |
# File 'lib/job.rb', line 108 def add_job(name) name = @jobdir + '/' + name.to_s FileUtils.mkdir_p name FileUtils.touch name end |
#delete_all_jobs ⇒ Object
Deletes all jobs.
137 138 139 140 141 |
# File 'lib/job.rb', line 137 def delete_all_jobs Dir[@jobdir + '/*'].each do |name| FileUtils.rm_r(name) end end |
#delete_job_by_name(name) ⇒ Object
This deletes a job by its name.
129 130 131 |
# File 'lib/job.rb', line 129 def delete_job_by_name(name) FileUtils.rm_r(@jobdir + '/' + name.to_s) end |
#name_task(task, name) ⇒ Object
Names a task. Given a JobTask object and a name (string or symbol), it enters it into the task table.
120 121 122 123 |
# File 'lib/job.rb', line 120 def name_task(task, name) fail "Task must have an execute method" unless task.respond_to? :execute @jobnames[name] = task end |
#run ⇒ Object
Runs the tasks in order yielding for each result, allowing the user to back out if problems occur processing the job chain.
148 149 150 151 152 |
# File 'lib/job.rb', line 148 def run yield_tasks do |task| yield task.execute end end |
#run_obliviously ⇒ Object
This runs without checking the job results, but will return the result of the last task executed.
159 160 161 162 163 164 165 166 |
# File 'lib/job.rb', line 159 def run_obliviously result = nil yield_tasks do |task| result = task.execute end result end |