Sha256: 2cf6a8afc4c450754c10a79f00f4a699d6f1f2510bdf7bfbc187ea9f4974ca43
Contents?: true
Size: 1.41 KB
Versions: 1
Compression:
Stored size: 1.41 KB
Contents
# frozen_string_literal: true module Tobox class Worker attr_reader :label def initialize(label, configuration) @label = label @wait_for_events_delay = configuration[:wait_for_events_delay] @handlers = configuration.handlers || {} @fetcher = configuration.fetcher_class.new(label, configuration) @finished = false if (message_to_arguments = configuration.arguments_handler) define_singleton_method(:message_to_arguments, &message_to_arguments) end Array(configuration.lifecycle_events[:start_worker]).each do |hd| hd.call(self) end end def finished? @finished end def finish! @finished = true end def work do_work until @finished end private def do_work return if @finished sum_fetched_events = @fetcher.fetch_events do |events| cs = @handlers.each_with_object({}) do |(callback, event_types), bucket| events.each do |event| (bucket[callback] ||= []) << message_to_arguments(event) if event_types.include?(event[:type].to_sym) end end cs.each do |callback, evs| callback.call(*evs) end end return if @finished wait_for_work if sum_fetched_events.zero? end def wait_for_work sleep(@wait_for_events_delay) end def message_to_arguments(event) event end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
tobox-0.7.0 | lib/tobox/worker.rb |