Class: BackgroundWorker::Base

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Base

Returns a new instance of Base.



9
10
11
12
13
14
15
16
# File 'lib/background_worker/base.rb', line 9

def initialize(options = {})
  @uid = options[:uid]

  # Store state persistently, to enable status checkups & progress reporting
  @state = BackgroundWorker::PersistentState.new(@uid, options.except(:uid))
  log("Created #{self.class}")
  log("Options are: #{options.inspect}")
end

Class Attribute Details

.queueObject (readonly)

Returns the value of attribute queue.



60
61
62
# File 'lib/background_worker/base.rb', line 60

def queue
  @queue
end

Instance Attribute Details

#stateObject

Returns the value of attribute state.



7
8
9
# File 'lib/background_worker/base.rb', line 7

def state
  @state
end

#uidObject

Returns the value of attribute uid.



7
8
9
# File 'lib/background_worker/base.rb', line 7

def uid
  @uid
end

Class Method Details

.get_state_of(worker_id) ⇒ Object



61
62
63
# File 'lib/background_worker/base.rb', line 61

def get_state_of(worker_id)
  BackgroundWorker::PersistentState.get_state_of(worker_id)
end

.perform_later(options = {}) ⇒ Object

Public method to do in background…



66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/background_worker/base.rb', line 66

def perform_later(options = {})
  opts = options.symbolize_keys

  opts[:uid] ||= BackgroundWorker::Uid.new(to_s).generate

  # Store into shared-cache before kicking job off
  BackgroundWorker::PersistentState.new(opts[:uid], opts.except(:uid))

  # Enqueue to the background queue
  BackgroundWorker.enqueue(self, opts)

  opts[:uid]
end

.perform_now(options = {}) ⇒ Object

This method is called by the job runner

It will just call your preferred method in the worker.



83
84
85
86
87
88
89
90
91
# File 'lib/background_worker/base.rb', line 83

def perform_now(options = {})
  BackgroundWorker.verify_active_connections! if BackgroundWorker.config.backgrounded

  worker = new(options)
  execution = WorkerExecution.new(worker, options)
  execution.call
ensure
  BackgroundWorker.release_connections! if BackgroundWorker.config.backgrounded
end

.queue_as(queue = nil) ⇒ Object



93
94
95
# File 'lib/background_worker/base.rb', line 93

def queue_as(queue = nil)
  @queue = queue&.to_sym || :default
end

Instance Method Details

#log(message, options = {}) ⇒ Object



54
55
56
57
# File 'lib/background_worker/base.rb', line 54

def log(message, options = {})
  severity = options.fetch(:severity, :info)
  logger.send(severity, "uid=#{uid} #{message}")
end

#loggerObject



50
51
52
# File 'lib/background_worker/base.rb', line 50

def logger
  BackgroundWorker.logger
end

#performObject

Raises:

  • (AbstractError)


18
19
20
# File 'lib/background_worker/base.rb', line 18

def perform
  raise AbstractError, 'Must be implemented in Job Class'
end

#report_failed(message = 'Failed', detailed_message = nil) ⇒ Object



45
46
47
48
# File 'lib/background_worker/base.rb', line 45

def report_failed(message = 'Failed', detailed_message = nil)
  state.detailed_message = detailed_message
  state.set_completed(message, :failed)
end

#report_minor_progress(message) ⇒ Object

Report a minor progress – may get called a lot, so don’t save it so often



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/background_worker/base.rb', line 29

def report_minor_progress(message)
  state.message = message

  # Only report minor events once per second
  @last_report ||= Time.now - 2
  time_elapsed =  Time.now - @last_report
  return unless time_elapsed > 1

  @last_report = Time.now
  state.save
end

#report_progress(message) ⇒ Object

Report progress…



23
24
25
26
# File 'lib/background_worker/base.rb', line 23

def report_progress(message)
  state.message = message
  state.save
end

#report_successful(message = 'Finished successfully') ⇒ Object



41
42
43
# File 'lib/background_worker/base.rb', line 41

def report_successful(message = 'Finished successfully')
  state.set_completed(message, :successful)
end