2019-10-14 15:39:18 +01:00
|
|
|
module Pipeline::Rpc::Worker
|
|
|
|
|
|
2019-11-12 12:31:00 +00:00
|
|
|
class DaemonRestartException < StandardError
|
|
|
|
|
end
|
|
|
|
|
|
2019-10-14 15:39:18 +01:00
|
|
|
class Daemon
|
|
|
|
|
|
|
|
|
|
attr_reader :identity, :context, :incoming, :outgoing, :environment
|
|
|
|
|
|
|
|
|
|
def initialize(identity, channel_address, env_base)
|
2019-10-15 16:48:16 +01:00
|
|
|
puts identity
|
|
|
|
|
puts channel_address
|
|
|
|
|
puts env_base
|
2019-10-14 15:39:18 +01:00
|
|
|
@identity = identity
|
|
|
|
|
channel_address = URI(channel_address)
|
|
|
|
|
@control_queue = "#{channel_address.scheme}://#{channel_address.host}:#{channel_address.port}"
|
2019-10-15 16:44:22 +01:00
|
|
|
@channel = channel_address.path[1..-1]
|
2019-11-11 22:19:30 +00:00
|
|
|
|
|
|
|
|
@topic = "*"
|
|
|
|
|
if channel_address.query
|
|
|
|
|
query = CGI::parse(channel_address.query)
|
|
|
|
|
@topics = query["topic"] if query["topic"]
|
|
|
|
|
end
|
2019-11-12 12:31:00 +00:00
|
|
|
@topics = ["*"] if @topics.nil? || @topics.empty?
|
2019-11-11 22:19:30 +00:00
|
|
|
|
2019-10-14 15:39:18 +01:00
|
|
|
@context = ZMQ::Context.new(1)
|
|
|
|
|
@incoming = context.socket(ZMQ::PULL)
|
|
|
|
|
@notifications = context.socket(ZMQ::SUB)
|
|
|
|
|
@notifications.setsockopt(ZMQ::SUBSCRIBE, "")
|
|
|
|
|
@environment = Pipeline::Runtime::RuntimeEnvironment.new(env_base)
|
|
|
|
|
end
|
|
|
|
|
|
2019-11-12 12:31:00 +00:00
|
|
|
def bootstrap_and_listen
|
|
|
|
|
bootstrap
|
|
|
|
|
configure
|
|
|
|
|
connect
|
|
|
|
|
poll_messages
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def bootstrap
|
2019-10-14 15:39:18 +01:00
|
|
|
@setup = context.socket(ZMQ::REQ)
|
|
|
|
|
@setup.setsockopt(ZMQ::LINGER, 0)
|
|
|
|
|
puts @control_queue
|
|
|
|
|
@setup.connect(@control_queue)
|
|
|
|
|
request = {
|
|
|
|
|
action: "configure_worker",
|
2019-11-11 22:19:30 +00:00
|
|
|
channel: @channel,
|
|
|
|
|
topics: @topics
|
2019-10-14 15:39:18 +01:00
|
|
|
}
|
|
|
|
|
@setup.send_string(request.to_json)
|
|
|
|
|
msg = ""
|
|
|
|
|
@setup.recv_string(msg)
|
2019-11-12 12:31:00 +00:00
|
|
|
@bootstrap = JSON.parse(msg)
|
|
|
|
|
puts "Bootstrap with #{JSON.pretty_generate(@bootstrap)}"
|
2019-10-14 15:39:18 +01:00
|
|
|
@setup.close
|
2019-11-12 12:31:00 +00:00
|
|
|
end
|
2019-10-14 15:39:18 +01:00
|
|
|
|
2019-11-12 12:31:00 +00:00
|
|
|
def configure
|
2019-10-14 15:39:18 +01:00
|
|
|
environment.prepare
|
2019-11-12 12:31:00 +00:00
|
|
|
action = Pipeline::Rpc::Worker::ConfigureAction.new(@channel, @bootstrap, @topics)
|
2019-10-14 15:39:18 +01:00
|
|
|
action.environment = environment
|
|
|
|
|
action.invoke
|
2019-11-12 12:31:00 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def listen
|
|
|
|
|
connect
|
|
|
|
|
poll_messages
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
private
|
2019-10-14 15:39:18 +01:00
|
|
|
|
2019-11-12 12:31:00 +00:00
|
|
|
def connect
|
|
|
|
|
channel_defn = @bootstrap["channel"]
|
|
|
|
|
response_address = channel_defn["response_address"]
|
|
|
|
|
workqueue_addresses =channel_defn["workqueue_addresses"]
|
|
|
|
|
notification_address = channel_defn["notification_address"]
|
2019-10-14 15:39:18 +01:00
|
|
|
@outgoing = context.socket(ZMQ::PUB)
|
|
|
|
|
@outgoing.connect(response_address)
|
2019-11-11 22:19:30 +00:00
|
|
|
workqueue_addresses.each do |workqueue_address|
|
|
|
|
|
incoming.connect(workqueue_address)
|
|
|
|
|
end
|
2019-10-14 15:39:18 +01:00
|
|
|
@notifications.connect(notification_address)
|
|
|
|
|
|
|
|
|
|
@incoming_wrapper = Pipeline::Rpc::Worker::WorkSocketWrapper.new(incoming)
|
2019-11-12 12:31:00 +00:00
|
|
|
@noificationincoming_wrapper = Pipeline::Rpc::Worker::NotificationSocketWrapper.new(@notifications, @channel, @topics)
|
2019-10-14 15:39:18 +01:00
|
|
|
|
|
|
|
|
@poller = Pipeline::Rpc::ChannelPoller.new
|
|
|
|
|
@poller.register(@incoming_wrapper)
|
|
|
|
|
@poller.register(@noificationincoming_wrapper)
|
2019-11-12 12:31:00 +00:00
|
|
|
end
|
2019-10-14 15:39:18 +01:00
|
|
|
|
2019-11-12 12:31:00 +00:00
|
|
|
def poll_messages
|
2019-10-14 15:39:18 +01:00
|
|
|
loop do
|
|
|
|
|
msg = []
|
|
|
|
|
@poller.listen_for_messages do |action_task|
|
|
|
|
|
unless action_task.nil?
|
|
|
|
|
action_task.environment = environment
|
|
|
|
|
result = action_task.invoke
|
2019-11-11 20:27:07 +00:00
|
|
|
if result && result[:return_address]
|
2019-11-11 22:19:30 +00:00
|
|
|
puts "RESULT #{result}"
|
2019-10-14 15:39:18 +01:00
|
|
|
outgoing.send_string(result.to_json)
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
end
|
|
|
|
|
end
|