74 lines
1.5 KiB
Ruby
74 lines
1.5 KiB
Ruby
module Mutant
|
|
module Parallel
|
|
# Parallel execution worker
|
|
class Worker
|
|
include Adamantium::Flat, Anima.new(:actor, :processor, :parent)
|
|
|
|
# Run worker
|
|
#
|
|
# @param [Hash<Symbol, Object] attributes
|
|
#
|
|
# @return [self]
|
|
#
|
|
# @api private
|
|
#
|
|
def self.run(attributes)
|
|
new(attributes).run
|
|
self
|
|
end
|
|
|
|
private_class_method :new
|
|
|
|
# Worker loop
|
|
#
|
|
# @return [self]
|
|
#
|
|
# @api private
|
|
#
|
|
# rubocop:disable Lint/Loop
|
|
#
|
|
def run
|
|
begin
|
|
parent.call(Actor::Message.new(:ready, actor.sender))
|
|
end until handle(actor.receiver.call)
|
|
end
|
|
|
|
private
|
|
|
|
# Handle job
|
|
#
|
|
# @param [Message] message
|
|
#
|
|
# @return [Boolean]
|
|
#
|
|
# @api private
|
|
#
|
|
def handle(message)
|
|
type, payload = message.type, 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]
|
|
#
|
|
# @api private
|
|
#
|
|
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 # Mutant
|