Class: Jiggler::Worker

Inherits:
Object
  • Object
show all
Includes:
Support::Helper
Defined in:
lib/jiggler/worker.rb

Defined Under Namespace

Classes: CurrentJob

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Support::Helper

#log_error, #log_error_short, #logger, #safe_async, #scan_all, #tid

Constructor Details

#initialize(config, collection, acknowledger, fetcher, &callback) ⇒ Worker

Returns a new instance of Worker.



11
12
13
14
15
16
17
18
19
# File 'lib/jiggler/worker.rb', line 11

def initialize(config, collection, acknowledger, fetcher, &callback)
  @done = false
  @current_job = nil
  @callback = callback
  @config = config
  @collection = collection
  @acknowledger = acknowledger
  @fetcher = fetcher
end

Instance Attribute Details

#acknowledgerObject (readonly)

Returns the value of attribute acknowledger.



9
10
11
# File 'lib/jiggler/worker.rb', line 9

def acknowledger
  @acknowledger
end

#collectionObject (readonly)

Returns the value of attribute collection.



9
10
11
# File 'lib/jiggler/worker.rb', line 9

def collection
  @collection
end

#configObject (readonly)

Returns the value of attribute config.



9
10
11
# File 'lib/jiggler/worker.rb', line 9

def config
  @config
end

#current_jobObject (readonly)

Returns the value of attribute current_job.



9
10
11
# File 'lib/jiggler/worker.rb', line 9

def current_job
  @current_job
end

#fetcherObject (readonly)

Returns the value of attribute fetcher.



9
10
11
# File 'lib/jiggler/worker.rb', line 9

def fetcher
  @fetcher
end

Instance Method Details

#runObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/jiggler/worker.rb', line 21

def run
  reason = nil
  @runner = safe_async('Worker') do
    @tid = tid
    loop do
      if @done
        @runner = nil
        break @callback.call(self)
      end

      process_job

      # pass control to other fibers
      Async::Task.current.yield
    rescue Async::Stop
      @runner = nil
      break @callback.call(self)
    rescue => err
      collection.incr_failures
      @runner = nil
      break @callback.call(self, err)     
    end
  end
end

#terminateObject



46
47
48
# File 'lib/jiggler/worker.rb', line 46

def terminate
  @runner&.stop
end

#waitObject



50
51
52
# File 'lib/jiggler/worker.rb', line 50

def wait
  @runner&.wait
end