Class: Bellbro::Service

Inherits:
Object
  • Object
show all
Includes:
Ringable, SidekiqUtils, Trackable
Defined in:
lib/bellbro/service.rb

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

Methods included from SidekiqUtils

#jobs, #jobs_in_flight_with, #jobs_with, #number_of_active_workers, #workers, #workers_for_queue, #workers_with

Constructor Details

#initializeService

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

#jidObject (readonly)

Returns the value of attribute jid.



9
10
11
# File 'lib/bellbro/service.rb', line 9

def jid
  @jid
end

#threadObject (readonly)

Returns the value of attribute thread.



9
10
11
# File 'lib/bellbro/service.rb', line 9

def thread
  @thread
end

#thread_errorObject (readonly)

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_classObject



23
24
25
# File 'lib/bellbro/service.rb', line 23

def self.get_worker_class
  @worker_class
end

.mutexObject



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_intervalObject



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_jobObject



77
78
79
80
# File 'lib/bellbro/service.rb', line 77

def each_job
  # Override
  []
end

#runObject



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

Returns:

  • (Boolean)


82
83
84
# File 'lib/bellbro/service.rb', line 82

def running?
  !@done
end

#sleepObject



73
74
75
# File 'lib/bellbro/service.rb', line 73

def sleep
  super(self.class.sleep_interval)
end

#startObject



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
    #rescue Exception => @thread_error
    #  Rails.logger.info "#{@thread_error.inspect}", type: :error
    #  Airbrake.notify(@thread_error)
    #  raise @thread_error
    end
  end
end

#start_jobsObject



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

#stopObject



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_classObject



86
87
88
# File 'lib/bellbro/service.rb', line 86

def worker_class
  @worker_class ||= self.class.get_worker_class
end