Class: BackgroundServices::Worker
- Inherits:
-
Object
- Object
- BackgroundServices::Worker
- Defined in:
- lib/background_services/worker.rb
Instance Attribute Summary collapse
-
#implementation ⇒ Object
Returns the value of attribute implementation.
-
#service ⇒ Object
readonly
Returns the value of attribute service.
-
#team ⇒ Object
readonly
Returns the value of attribute team.
Instance Method Summary collapse
-
#initialize(service, team) ⇒ Worker
constructor
A new instance of Worker.
- #join ⇒ Object
- #start ⇒ Object
- #stop ⇒ Object
- #working? ⇒ Boolean
Constructor Details
#initialize(service, team) ⇒ Worker
Returns a new instance of Worker.
6 7 8 9 10 |
# File 'lib/background_services/worker.rb', line 6 def initialize(service, team) @team = team @service = service @implementation = nil end |
Instance Attribute Details
#implementation ⇒ Object
Returns the value of attribute implementation.
4 5 6 |
# File 'lib/background_services/worker.rb', line 4 def implementation @implementation end |
#service ⇒ Object (readonly)
Returns the value of attribute service.
3 4 5 |
# File 'lib/background_services/worker.rb', line 3 def service @service end |
#team ⇒ Object (readonly)
Returns the value of attribute team.
3 4 5 |
# File 'lib/background_services/worker.rb', line 3 def team @team end |
Instance Method Details
#join ⇒ Object
17 18 19 |
# File 'lib/background_services/worker.rb', line 17 def join @thread.join end |
#start ⇒ Object
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/background_services/worker.rb', line 20 def start @working = true @thread = Thread.new(self) do |this| begin while this.working? job = this.team.receive_job unless job.nil? App.logger.info "processing job with id: #{job.id}" this.service.process_job this, job this.team.with_connection { job.delete } else sleep 0.5 if this.working? end end rescue => e puts e.inspect puts e.backtrace end end end |
#stop ⇒ Object
14 15 16 |
# File 'lib/background_services/worker.rb', line 14 def stop @working = false end |
#working? ⇒ Boolean
11 12 13 |
# File 'lib/background_services/worker.rb', line 11 def working? @working == true end |