Class: BackgroundWorker::PersistentState

Inherits:
Object
  • Object
show all
Defined in:
lib/background_worker/persistent_state.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(job_id, data) ⇒ PersistentState

Returns a new instance of PersistentState.



10
11
12
13
14
15
16
17
18
# File 'lib/background_worker/persistent_state.rb', line 10

def initialize(job_id, data)
  @message = 'Waiting for task to queue...'
  @status = :processing
  @completed = false

  @job_id = job_id
  @data = data
  save
end

Instance Attribute Details

#completedObject

Returns the value of attribute completed.



8
9
10
# File 'lib/background_worker/persistent_state.rb', line 8

def completed
  @completed
end

#dataObject

Returns the value of attribute data.



8
9
10
# File 'lib/background_worker/persistent_state.rb', line 8

def data
  @data
end

#detailed_messageObject

Returns the value of attribute detailed_message.



8
9
10
# File 'lib/background_worker/persistent_state.rb', line 8

def detailed_message
  @detailed_message
end

#messageObject

Returns the value of attribute message.



8
9
10
# File 'lib/background_worker/persistent_state.rb', line 8

def message
  @message
end

#statusObject

Returns the value of attribute status.



8
9
10
# File 'lib/background_worker/persistent_state.rb', line 8

def status
  @status
end

Class Method Details

.get_state_of(job_id) ⇒ Object

Get a report out the queue (was .get_report, then .progress)



35
36
37
# File 'lib/background_worker/persistent_state.rb', line 35

def self.get_state_of(job_id)
  Rails.cache.read(job_id)
end

Instance Method Details

#saveObject

Save persistently (well for an hour at least)



29
30
31
# File 'lib/background_worker/persistent_state.rb', line 29

def save
  Rails.cache.write(@job_id, generate_persistent_hash, expires_in: 1.hour)
end

#set_completed(message, status) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/background_worker/persistent_state.rb', line 20

def set_completed(message, status)
  self.status = status
  self.message = message

  self.completed = true
  save
end