Sha256: 8720a81040445c43d202cd31ce712a200ef573454f5a7e948da79b9140874244

Contents?: true

Size: 1.07 KB

Versions: 1

Compression:

Stored size: 1.07 KB

Contents

require "delegate"
require "securerandom"

module Rector
  class Job
    class WorkerCollection < SimpleDelegator
      def initialize(job)
        @job = job

        # Wraps an array
        super(Array.new)
      end

      def create
        Rector::Worker.new(@job.allocate_worker_id).tap do |worker|
          self << worker
        end
      end
    end

    attr_reader :id, :workers

    # TODO: Obviously there's a small chance of jobs overlapping here
    # Can do something more reliable for ID generation?
    def initialize(id = SecureRandom.hex(10))
      @id      = id
      @workers = WorkerCollection.new(self)
      @backend = Rector.backend_for(id)
    end

    def allocate_worker_id
      # TODO: Obviously there's a small chance of jobs overlapping here
      # Can do something more reliable for ID generation?
      "#{id}:#{SecureRandom.hex(8)}"
    end

    def join
      while @backend.workers_working?
        sleep 5
      end
    end

    def data
      @data ||= @backend.read_job_data_to_hash
    end

    def cleanup
      @backend.cleanup
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rector-0.0.4 lib/rector/job.rb