Class: BackgroundWorker::Base

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

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
17
18
# File 'lib/background_worker/base.rb', line 9

def initialize(options = {})
  Time.zone = Setting.time_zone

  @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.pretty_inspect}")
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



58
59
60
# File 'lib/background_worker/base.rb', line 58

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

.perform(method_name, options = {}) ⇒ Object

This method is called by the job runner

It will just call your preferred method in the worker.



81
82
83
84
85
86
87
# File 'lib/background_worker/base.rb', line 81

def perform(method_name, options = {})
  BackgroundWorker.verify_active_connections!

  worker = new(options)
  execution = WorkerExecution.new(worker, method_name, options)
  execution.call
end

.perform_in_background(method_name, options = {}) ⇒ Object

Public method to do in background…



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

def perform_in_background(method_name, options = {})
  opts = options.symbolize_keys

  method_name = method_name.to_sym
  opts[:uid] ||= BackgroundWorker::Uid.new(to_s, method_name).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, method_name, opts)

  opts[:uid]
end

Instance Method Details

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



52
53
54
55
# File 'lib/background_worker/base.rb', line 52

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

#loggerObject



48
49
50
# File 'lib/background_worker/base.rb', line 48

def logger
  BackgroundWorker.logger
end

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



43
44
45
46
# File 'lib/background_worker/base.rb', line 43

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



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

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…



21
22
23
24
# File 'lib/background_worker/base.rb', line 21

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

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



39
40
41
# File 'lib/background_worker/base.rb', line 39

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