Class: TaliaCore::BackgroundJobs::Job
- Inherits:
-
Object
- Object
- TaliaCore::BackgroundJobs::Job
- Defined in:
- lib/talia_core/background_jobs/job.rb
Overview
A Background job run from Talia. The Job class is designed to be able to run long-running tasks both from the command line and Talia’s background job runner.
Class Method Summary collapse
-
.run_progress_job ⇒ Object
Runs the block with an active progress meter, creating the progress object before starting, and deleting it from the db after completion.
-
.run_with_progress(message, item_count) ⇒ Object
Runs the block with the progress meter for the current job.
-
.submit_with_progress(jobs, options = {}) ⇒ Object
Creates a background job with progress metering.
Class Method Details
.run_progress_job ⇒ Object
Runs the block with an active progress meter, creating the progress object before starting, and deleting it from the db after completion. This way the progress_jobs table should remain mostly clean.
34 35 36 37 38 39 40 41 42 |
# File 'lib/talia_core/background_jobs/job.rb', line 34 def self.run_progress_job job_id = ENV['JOB_ID'] raise(RuntimeError, "Cannot run job: Job id not given or non-existent (#{job_id})") unless(job_id && Bj.table.job.exists?(job_id)) ProgressJob.create_progress!(job_id) unless(ProgressJob.exists?(:job_id => job_id)) yield ensure job_id = ENV['JOB_ID'] ProgressJob.delete(:job_id => job_id) end |
.run_with_progress(message, item_count) ⇒ Object
Runs the block with the progress meter for the current job. This may be used multiple times.
45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/talia_core/background_jobs/job.rb', line 45 def self.run_with_progress(, item_count) # Wrap this in the progress job call. This way it will work fine standalone run_progress_job do job_id = ENV['JOB_ID'] # Create the progress meter progress = ProgressJob.find(:first, :conditions => {:job_id => job_id}) raise(RuntimeError, 'Progress meter not found for job.') unless progress progress.update_attributes(:item_count => item_count, :progress_message => , :processed_count => 0, :started_at => Time.now) yield(progress) progress.finish end end |
.submit_with_progress(jobs, options = {}) ⇒ Object
Creates a background job with progress metering. If a tag is given, it will attempt to block the creation of further jobs with the same tag. This will also use the runner script, called with the current ruby binary
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/talia_core/background_jobs/job.rb', line 15 def self.submit_with_progress(jobs, = {}) # add the runner script and ruby call to the jobs jobs = make_jobs(jobs) Bj.submit(jobs, ) do |job| if(tag = job.tag) tagged = Bj.table.job.find(:all, :conditions => ["(state != 'finished' and state != 'dead' and tag = ?)", tag]) # The error will break the transation and leave the db in a clean state raise(JobBlockedError, "Tried to create another job with tag #{tag}.") unless(tagged.size == 1) end # Update the environment so the runner can find the job id job.env['JOB_ID'] ||= job.id.to_s job.save! job end end |