Class: AsyncResponse::Job

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/async_response/job.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.expire!(type, key) ⇒ Object



16
17
18
19
20
21
# File 'app/models/async_response/job.rb', line 16

def self.expire!(type, key)
  record = valid_job(type, key)
  return nil unless record
  record.expire!
  record
end

.valid_job(type, key) ⇒ Object



7
8
9
10
11
12
13
14
# File 'app/models/async_response/job.rb', line 7

def self.valid_job(type, key)
  record = where(job_type: type, job_key: key).last
  return nil unless record
  return nil if record.expired?
  return nil if record.errored?

  record
end

Instance Method Details

#expire!Object



35
36
37
38
# File 'app/models/async_response/job.rb', line 35

def expire!
  self.expires_at = Time.now - 1.minute
  save(validate: false)
end

#expired?Boolean

Returns:

  • (Boolean)


31
32
33
# File 'app/models/async_response/job.rb', line 31

def expired?
  expires_at < Time.now
end

#finished!Object



40
41
42
43
44
# File 'app/models/async_response/job.rb', line 40

def finished!
  increment_percentage(100)
  self.status = :finished
  save!
end

#increment_percentage(increment) ⇒ Object



46
47
48
49
50
# File 'app/models/async_response/job.rb', line 46

def increment_percentage(increment)
  total = (percentage_completion || 0) + increment
  total = [[total, 100].min, 0].max
  self.percentage_completion = total
end

#increment_percentage!(increment) ⇒ Object



52
53
54
55
# File 'app/models/async_response/job.rb', line 52

def increment_percentage!(increment)
  increment_percentage(increment)
  save!
end

#paramsObject



27
28
29
# File 'app/models/async_response/job.rb', line 27

def params
  JSON.parse(params_json).deep_symbolize_keys rescue nil
end

#params=(hash) ⇒ Object



23
24
25
# File 'app/models/async_response/job.rb', line 23

def params=(hash)
  self.params_json = hash.to_json
end