Class: Lev::Status

Inherits:
Object show all
Defined in:
lib/lev/status.rb

Constant Summary collapse

STATE_QUEUED =
'queued'
STATE_WORKING =
'working'
STATE_COMPLETED =
'completed'
STATE_FAILED =
'failed'
STATE_KILLED =
'killed'
STATES =
[
  STATE_QUEUED,
  STATE_WORKING,
  STATE_COMPLETED,
  STATE_FAILED,
  STATE_KILLED
].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uuid = nil) ⇒ Status

Returns a new instance of Status.



21
22
23
24
# File 'lib/lev/status.rb', line 21

def initialize(uuid = nil)
  @uuid = uuid || SecureRandom.uuid
  save
end

Instance Attribute Details

#uuidObject (readonly)

Returns the value of attribute uuid.



19
20
21
# File 'lib/lev/status.rb', line 19

def uuid
  @uuid
end

Class Method Details

.find(uuid) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/lev/status.rb', line 26

def self.find(uuid)
  if status = store.fetch(status_key(uuid))
    JSON.parse(status)
  else
    nil
  end
end

.status_key(uuid) ⇒ Object



90
91
92
# File 'lib/lev/status.rb', line 90

def self.status_key(uuid)
  "#{Lev.configuration.status_store_namespace}:#{uuid}"
end

.storeObject



72
73
74
75
76
# File 'lib/lev/status.rb', line 72

def self.store
  # Nice to get the store from lev config each time so it isn't serialized
  # when activejobs are sent off to places like redis
  Lev.configuration.status_store
end

Instance Method Details

#add_error(error, options = { }) ⇒ Object



58
59
60
61
62
63
# File 'lib/lev/status.rb', line 58

def add_error(error, options = { })
  options = { is_fatal: false }.merge(options)
  push('errors', { is_fatal: options[:is_fatal],
                   code: error.code,
                   message: error.message })
end

#get(key) ⇒ Object



65
66
67
# File 'lib/lev/status.rb', line 65

def get(key)
  self.class.find(uuid)[key]
end

#save(hash = {}) ⇒ Object



49
50
51
52
53
54
55
56
# File 'lib/lev/status.rb', line 49

def save(hash = {})
  if has_reserved_keys?(hash)
    raise IllegalArgument,
          "Caller cannot specify any reserved keys (#{RESERVED_KEYS})"
  else
    set(hash)
  end
end

#set_progress(at, out_of = nil) ⇒ Object



34
35
36
37
38
39
40
41
# File 'lib/lev/status.rb', line 34

def set_progress(at, out_of = nil)
  progress = compute_fractional_progress(at, out_of)

  data_to_set = { progress: progress }
  data_to_set[:state] = STATE_COMPLETED if 1.0 == progress

  set(data_to_set)
end