Sha256: 45e1c7eb96193591d131682b2495b44a5a22ddcad1aa26fa510358648449b79b

Contents?: true

Size: 847 Bytes

Versions: 1

Compression:

Stored size: 847 Bytes

Contents

require "tennis/actor"
require "tennis/exceptions"

module Tennis
  class Worker
    include Actor

    attr_accessor :worker_id

    def initialize(pool)
      @pool = pool
    end

    def work(task)
      # Send the current working thread to the pool.
      register_working_thread

      ack = true
      begin
        task.execute
      rescue Shutdown
        ack = false
        raise
      rescue Exception => exception
        # TODO: add an error handler on the job's class
        raise
      ensure
        task.ack if ack
      end

      # Tell the pool that we've successfully done the job.
      notifies_work_done(task)
    end

    private

    def register_working_thread
      @pool.async.register_thread(worker_id, Thread.current)
    end

    def notifies_work_done(task)
      @pool.async.work_done(task)
    end

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
tennis-jobs-0.4.0 lib/tennis/worker.rb