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



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

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
15
# 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.errored?
  # Only care about expiry if the job has finished running
  return nil if record.complete? && record.expired?

  record
end

Instance Method Details

#active?Boolean

Returns:

  • (Boolean)


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

def active?
  started? || running?
end

#complete?Boolean

Returns:

  • (Boolean)


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

def complete?
  shown? || errored?
end

#expire!Object



44
45
46
47
# File 'app/models/async_response/job.rb', line 44

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

#expired?Boolean

Returns:

  • (Boolean)


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

def expired?
  expires_at < Time.now
end

#finished!Object



49
50
51
52
53
# File 'app/models/async_response/job.rb', line 49

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

#increment_percentage(increment) ⇒ Object



55
56
57
58
59
# File 'app/models/async_response/job.rb', line 55

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

#increment_percentage!(increment) ⇒ Object



61
62
63
64
# File 'app/models/async_response/job.rb', line 61

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

#paramsObject



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

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

#params=(hash) ⇒ Object



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

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