Class: Lev::BackgroundJob

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

Constant Summary collapse

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attrs = {}) ⇒ BackgroundJob

Returns a new instance of BackgroundJob.



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/lev/background_job.rb', line 23

def initialize(attrs = {})
  @id = attrs[:id] || attrs['id'] || SecureRandom.uuid
  @status = attrs[:status] || attrs['status'] || STATE_UNKNOWN
  @progress = attrs[:progress] || attrs['progress'] || set_progress(0)
  @errors = attrs[:errors] || attrs['errors'] || []

  set({ id: id,
        status: status,
        progress: progress,
        errors: errors })
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



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

def method_missing(method_name, *args)
  instance_variable_get("@#{method_name}") || super
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



5
6
7
# File 'lib/lev/background_job.rb', line 5

def errors
  @errors
end

#idObject (readonly)

Returns the value of attribute id.



5
6
7
# File 'lib/lev/background_job.rb', line 5

def id
  @id
end

#progressObject (readonly)

Returns the value of attribute progress.



5
6
7
# File 'lib/lev/background_job.rb', line 5

def progress
  @progress
end

#statusObject (readonly)

Returns the value of attribute status.



5
6
7
# File 'lib/lev/background_job.rb', line 5

def status
  @status
end

Class Method Details

.allObject



47
48
49
# File 'lib/lev/background_job.rb', line 47

def self.all
  job_ids.map { |id| find(id) }
end

.find(id) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/lev/background_job.rb', line 35

def self.find(id)
  attrs = { id: id }

  if job = store.fetch(job_key(id))
    attrs.merge!(JSON.parse(job))
  else
    attrs.merge!(status: STATE_UNKNOWN)
  end

  new(attrs)
end

Instance Method Details

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



68
69
70
71
72
73
74
# File 'lib/lev/background_job.rb', line 68

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

#as_json(options = {}) ⇒ Object

Rails compatibility returns a Hash of all key-value pairs that have been #set()



78
79
80
# File 'lib/lev/background_job.rb', line 78

def as_json(options = {})
  stored
end

#respond_to?(method_name) ⇒ Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
100
# File 'lib/lev/background_job.rb', line 94

def respond_to?(method_name)
  if method_name.match /\?$/
    super
  else
    instance_variable_get("@#{method_name}").present? || super
  end
end

#save(incoming_hash) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/lev/background_job.rb', line 82

def save(incoming_hash)
  if reserved = incoming_hash.select { |k, _| RESERVED_KEYS.include?(k) }.first
    raise IllegalArgument, "Cannot set reserved key: #{reserved[0]}"
  else
    set(incoming_hash)
  end
end

#set_progress(at, out_of = nil) ⇒ Object



51
52
53
54
55
56
57
58
59
60
# File 'lib/lev/background_job.rb', line 51

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

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

  set(data_to_set)

  progress
end