Class: TaliaCore::BackgroundJobs::ProgressJob

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/talia_core/background_jobs/progress_job.rb

Overview

Helper table to track the current status of a long-running task

Constant Summary collapse

DB_UPDATE_INTERVAL =

Minimum interval for database updates.

2

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.clearObject

Clears the progress for processes no longer active



22
23
24
25
26
# File 'lib/talia_core/background_jobs/progress_job.rb', line 22

def self.clear
  find(:all).each do |job_prog|
    delete(job_prog.id) if(!Bg.table.job.exists?(job_id) || Bg.tablejob.find(job_id).finished?)
  end
end

.create_progress!(job_id, message = '', item_count = 1) ⇒ Object

Create a new progress job



15
16
17
18
19
# File 'lib/talia_core/background_jobs/progress_job.rb', line 15

def self.create_progress!(job_id, message = '', item_count = 1)
  job_prog = new(:job_id => job_id, :progress_message => message, :item_count => item_count, :processed_count => 0)
  job_prog.save!
  job_prog
end

Instance Method Details

#elapsedObject

Elapsed time in seconds



54
55
56
57
# File 'lib/talia_core/background_jobs/progress_job.rb', line 54

def elapsed
  return unless(self.started_at)
  Time.now - started_at
end

#etaObject

Returns the estimated time remaining on the current job



60
61
62
# File 'lib/talia_core/background_jobs/progress_job.rb', line 60

def eta
  ((elapsed * 100) / percentage) - elapsed
end

#finishObject



43
44
45
46
# File 'lib/talia_core/background_jobs/progress_job.rb', line 43

def finish
  self.processed_count = self.item_count
  save!
end

#inc(inc_value = 1) ⇒ Object

Increments the number of processed items. To avoid flooding the db, the same object will only save this value at most all DB_UPDATE_INTERVAL seconds. Will return true if the element was saved, false otherwise.



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/talia_core/background_jobs/progress_job.rb', line 31

def inc(inc_value = 1)
  pc_old = self.processed_count
  self.processed_count = pc_old + inc_value
  if(!@last_update || ((Time.now - @last_update) > DB_UPDATE_INTERVAL))
    save!
    @last_update = Time.now
    true
  else
    false
  end
end

#percentageObject

The percentage completed



49
50
51
# File 'lib/talia_core/background_jobs/progress_job.rb', line 49

def percentage
  [((self.processed_count * 100) / self.item_count), 100].min
end