lib/reactor/event.rb in reactor-0.18.0 vs lib/reactor/event.rb in reactor-0.19.0

- old
+ new

@@ -1,9 +1,15 @@ class Reactor::Event - include Reactor::OptionallySubclassable include Sidekiq::Worker + sidekiq_options queue: ENV['REACTOR_QUEUE'] || Sidekiq.default_worker_options['queue'] + + CONSOLE_CONFIRMATION_MESSAGE = <<-eos + It looks like you are on a production console. Only fire an event if you intend to trigger + all of its subscribers. In order to proceed, you must pass `srsly: true` in the event data.' + eos + attr_accessor :__data__ def initialize(data = {}) self.__data__ = {}.with_indifferent_access data.each do |key, value| @@ -30,11 +36,10 @@ true end if need_to_fire data.merge!(fired_at: Time.current, name: name) - fire_database_driven_subscribers(data, name) fire_block_subscribers(data, name) end end def method_missing(method, *args) @@ -53,10 +58,13 @@ def perform(name, data) new.perform(name, data) end def publish(name, data = {}) + if defined?(Rails::Console) && ENV['RACK_ENV'] == 'production' && data[:srsly].blank? + raise ArgumentError.new(CONSOLE_CONFIRMATION_MESSAGE) + end message = new(data.merge(event: name, uuid: SecureRandom.uuid)) if message.at perform_at message.at, name, message.__data__ else @@ -111,16 +119,9 @@ __data__.has_key?("#{method}_type") end def initialize_polymorphic_association(method) __data__["#{method}_type"].constantize.find(__data__["#{method}_id"]) - end - - def fire_database_driven_subscribers(data, name) - #TODO: support more matching? - Reactor::Subscriber.where(event_name: [name, '*']).pluck(:id).each do |model_id| - Reactor::Workers::DatabaseSubscriberWorker.perform_async model_id, data - end end def fire_block_subscribers(data, name) ((Reactor::SUBSCRIBERS[name.to_s] || []) | (Reactor::SUBSCRIBERS['*'] || [])).each { |s| s.perform_where_needed(data) } end