Sha256: b79004c9092afd9f83ffca0919efc78697231c7441b2965d0176003d7901d5a2

Contents?: true

Size: 1.07 KB

Versions: 3

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 :workers

    def initialize
      @workers = WorkerCollection.new(self)
      @backend = Rector.backend_for(id)
    end

    def id
      # TODO: Obviously there's a small chance of jobs overlapping here
      # Can do something more reliable for ID generation?
      @id ||= SecureRandom.hex(10)
    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

3 entries across 3 versions & 1 rubygems

Version Path
rector-0.0.3 lib/rector/job.rb
rector-0.0.2 lib/rector/job.rb
rector-0.0.1 lib/rector/job.rb