Class: Tasker::Events::Subscribers::BaseSubscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/tasker/events/subscribers/base_subscriber.rb

Overview

BaseSubscriber provides a clean foundation for creating custom event subscribers

This class extracts common patterns from TelemetrySubscriber and provides:

  • Declarative subscription registration via class methods

  • Automatic method routing from event names to handler methods

  • Defensive payload handling with safe accessors

  • Easy integration with the Tasker event system

  • Metrics collection helper methods for common patterns

Usage:

class OrderNotificationSubscriber < Tasker::Events::Subscribers::BaseSubscriber
  subscribe_to :task_completed, :step_failed

  def handle_task_completed(event)
    OrderMailer.completion_email(event[:task_id]).deliver_later
  end

  def handle_step_failed(event)
    AlertService.notify("Step failed: #{event[:step_name]}")
  end
end

# Register the subscriber
OrderNotificationSubscriber.subscribe(Tasker::Events::Publisher.instance)

Direct Known Subclasses

MetricsSubscriber, TelemetrySubscriber

Defined Under Namespace

Classes: ErrorCategorizer, MetricTagsExtractor

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name: nil, events: nil, config: {}) ⇒ BaseSubscriber

Returns a new instance of BaseSubscriber.



34
35
36
37
38
39
40
41
42
43
# File 'lib/tasker/events/subscribers/base_subscriber.rb', line 34

def initialize(name: nil, events: nil, config: {})
  @subscription_name = name
  @subscription_config = config

  # If events are provided via constructor (from YAML), add them to subscribed events
  return if events.blank?

  current_events = self.class.subscribed_events || []
  self.class.subscribed_events = (current_events + Array(events)).uniq
end

Class Method Details

.filter_events(&filter_proc) ⇒ void

This method returns an undefined value.

Set a filter for events (optional)

Example:

filter_events { |event_name| event_name.include?('order') }

Parameters:

  • filter_proc (Proc)

    A proc that returns true for events to process



67
68
69
# File 'lib/tasker/events/subscribers/base_subscriber.rb', line 67

def filter_events(&filter_proc)
  self.event_filter = filter_proc
end

.subscribe(publisher) ⇒ BaseSubscriber

Subscribe this subscriber to a publisher

Parameters:

Returns:



75
76
77
78
79
# File 'lib/tasker/events/subscribers/base_subscriber.rb', line 75

def subscribe(publisher)
  subscriber = new
  subscriber.subscribe_to_publisher(publisher)
  subscriber
end

.subscribe_to(*events) ⇒ void

This method returns an undefined value.

Declarative method to register events this subscriber cares about

Example:

subscribe_to :task_completed, :step_failed
subscribe_to 'order.created', 'payment.processed'

Parameters:

  • events (Array<Symbol, String>)

    Event names to subscribe to



54
55
56
57
58
# File 'lib/tasker/events/subscribers/base_subscriber.rb', line 54

def subscribe_to(*events)
  # Accumulate events instead of replacing them
  current_events = subscribed_events || []
  self.subscribed_events = (current_events + events.map(&:to_s)).uniq
end

Instance Method Details

#subscribe_to_publisher(publisher) ⇒ void

This method returns an undefined value.

Subscribe to all events defined by the class

Parameters:



86
87
88
89
90
91
92
93
94
95
# File 'lib/tasker/events/subscribers/base_subscriber.rb', line 86

def subscribe_to_publisher(publisher)
  event_subscriptions.each do |event_constant, handler_method|
    # Apply filtering if defined
    next unless should_handle_event?(event_constant)

    # Subscribe to the event with automatic method routing
    # This will fail fast if the event doesn't exist, which is the correct behavior
    publisher.subscribe(event_constant, &method(handler_method))
  end
end