Class: BackgroundServices::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/background_services/worker.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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

#implementationObject

Returns the value of attribute implementation.



4
5
6
# File 'lib/background_services/worker.rb', line 4

def implementation
  @implementation
end

#serviceObject (readonly)

Returns the value of attribute service.



3
4
5
# File 'lib/background_services/worker.rb', line 3

def service
  @service
end

#teamObject (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

#joinObject



17
18
19
# File 'lib/background_services/worker.rb', line 17

def join
  @thread.join
end

#startObject



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

#stopObject



14
15
16
# File 'lib/background_services/worker.rb', line 14

def stop
  @working = false
end

#working?Boolean

Returns:

  • (Boolean)


11
12
13
# File 'lib/background_services/worker.rb', line 11

def working?
  @working == true
end