Sha256: de69866fb43b76d77a5f9e3c300b7188608d04e709a45cfe4b89862c1a96b5eb

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

module Mutest
  module Parallel
    # Parallel execution worker
    class Worker
      include Adamantium::Flat, Anima.new(
        :mailbox,
        :parent,
        :processor
      )

      # Run worker
      #
      # @param [Hash<Symbol, Object] attributes
      #
      # @return [self]
      def self.run(attributes)
        new(attributes).run
        self
      end

      private_class_method :new

      # Worker loop
      #
      # @return [self]
      #
      # rubocop:disable Lint/Loop
      def run
        begin
          parent.call(Actor::Message.new(:ready, mailbox.sender))
        end until handle(mailbox.receiver.call)
      end

      private

      # Handle job
      #
      # @param [Message] message
      #
      # @return [Boolean]
      def handle(message) # rubocop:disable Metrics/MethodLength
        type    = message.type
        payload = message.payload

        case message.type
        when :job
          handle_job(payload)
          nil
        when :stop
          true
        else
          fail Actor::ProtocolError, "Unknown command: #{type.inspect}"
        end
      end

      # Handle mutation
      #
      # @param [Job] job
      #
      # @return [undefined]
      def handle_job(job)
        result = processor.call(job.payload)

        parent.call(
          Actor::Message.new(
            :result,
            JobResult.new(
              job:     job,
              payload: result
            )
          )
        )
      end
    end # Worker
  end # Parallel
end # Mutest

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
mutest-0.0.6 lib/mutest/parallel/worker.rb
mutest-0.0.5 lib/mutest/parallel/worker.rb
mutest-0.0.4 lib/mutest/parallel/worker.rb
mutest-0.0.3 lib/mutest/parallel/worker.rb
mutest-0.0.2 lib/mutest/parallel/worker.rb