Class: Bellbro::Service
Instance Attribute Summary collapse
Attributes included from Trackable
#record
Class Method Summary
collapse
Instance Method Summary
collapse
Methods included from Ringable
#error, included, logger, #ring
Methods included from Trackable
included, #record_add, #record_incr, #record_set, #record_update, #status_update, #stop_tracking, #track, #tracking?, #write_log
#jobs, #jobs_in_flight_with, #jobs_with, #number_of_active_workers, #workers, #workers_for_queue, #workers_with
Constructor Details
Returns a new instance of Service.
30
31
32
33
|
# File 'lib/bellbro/service.rb', line 30
def initialize
@done = false
@jid = Digest::MD5.hexdigest(Time.now.utc.to_s + Thread.current.object_id.to_s)
end
|
Instance Attribute Details
#jid ⇒ Object
Returns the value of attribute jid.
9
10
11
|
# File 'lib/bellbro/service.rb', line 9
def jid
@jid
end
|
#thread ⇒ Object
Returns the value of attribute thread.
9
10
11
|
# File 'lib/bellbro/service.rb', line 9
def thread
@thread
end
|
#thread_error ⇒ Object
Returns the value of attribute thread_error.
9
10
11
|
# File 'lib/bellbro/service.rb', line 9
def thread_error
@thread_error
end
|
Class Method Details
.get_worker_class ⇒ Object
23
24
25
|
# File 'lib/bellbro/service.rb', line 23
def self.get_worker_class
@worker_class
end
|
.mutex ⇒ Object
90
91
92
|
# File 'lib/bellbro/service.rb', line 90
def self.mutex
$mutex ||= Mutex.new
end
|
.poll_interval(arg) ⇒ Object
11
12
13
14
15
16
17
|
# File 'lib/bellbro/service.rb', line 11
def self.poll_interval(arg)
if defined?(Rails) && Rails.env.test?
@sleep_interval = 0.5
else
@sleep_interval = arg
end
end
|
.sleep_interval ⇒ Object
94
95
96
|
# File 'lib/bellbro/service.rb', line 94
def self.sleep_interval
@sleep_interval
end
|
.worker_class(arg) ⇒ Object
19
20
21
|
# File 'lib/bellbro/service.rb', line 19
def self.worker_class(arg)
@worker_class = arg
end
|
Instance Method Details
#each_job ⇒ Object
77
78
79
80
|
# File 'lib/bellbro/service.rb', line 77
def each_job
[]
end
|
#run ⇒ Object
54
55
56
57
58
59
60
61
62
63
|
# File 'lib/bellbro/service.rb', line 54
def run
Rails.logger.info "Starting #{self.class} service."
self.class.mutex.synchronize { track }
begin
self.class.mutex.synchronize { start_jobs }
self.class.mutex.synchronize { status_update }
sleep
end until @done
self.class.mutex.synchronize { stop_tracking }
end
|
#running? ⇒ Boolean
82
83
84
|
# File 'lib/bellbro/service.rb', line 82
def running?
!@done
end
|
#sleep ⇒ Object
73
74
75
|
# File 'lib/bellbro/service.rb', line 73
def sleep
super(self.class.sleep_interval)
end
|
#start ⇒ Object
35
36
37
38
39
40
41
42
43
44
45
|
# File 'lib/bellbro/service.rb', line 35
def start
@thread = Thread.new do
begin
run
end
end
end
|
#start_jobs ⇒ Object
65
66
67
68
69
70
71
|
# File 'lib/bellbro/service.rb', line 65
def start_jobs
each_job do |job|
jid = worker_class.perform_async(job)
Rails.logger.info "Starting job #{jid} #{worker_class.name} with #{job.inspect}."
record_incr(:jobs_started)
end
end
|
#stop ⇒ Object
47
48
49
50
51
52
|
# File 'lib/bellbro/service.rb', line 47
def stop
@done = true
Rails.logger.info "Stopping #{self.class} service..."
@thread.join
Rails.logger.info "#{self.class.to_s.capitalize} service stopped."
end
|
#worker_class ⇒ Object
86
87
88
|
# File 'lib/bellbro/service.rb', line 86
def worker_class
@worker_class ||= self.class.get_worker_class
end
|