Skip to content

AfterCommitAsyncDispatcher does not pass event to custom scheduler #1391

@grncdr

Description

@grncdr

Every layer from Broker->Scheduler passes a triple of (subscriber, event, event_record), but the very last step hides the event object and only passes the serializ(ed/able) record to custom schedulers:

def call(subscriber, _, record)
run { @scheduler.call(subscriber, record) }
end

This is a problem for me because I'd like to run a predicate on the event in my custom scheduler before enqueueing a job. My specific use case involves a fairly frequent event, where only a tiny percentage are actually relevant to a given subscriber.

How I'm working around this

Monkey patch AfterCommitAsyncDispatcher in an initializer:

class RailsEventStore::AfterCommitAsyncDispatcher
  def call(subscriber, event, record)
    run { @scheduler.call(subscriber, event, record) } 
  end
end

Suggested patch

The existing API contract is obviously for 2 arguments only. I know arity checking is usually frowned upon, but I feel like it's an acceptable trade-off here:

class AfterCommitAsyncDispatcher
  def initialize(scheduler:)
    @scheduler = scheduler
    @scheduler_arity = scheduler.method(:call).arity
  end

  def call(subscriber, event, record)
    run { @scheduler_arity === 3 ? @scheduler.call(subscriber, event, record) : @scheduler.call(subscriber, record) } 
  end
end

What do you think? Should this be changed? Would the change be breaking or something that could happen in the 2.x series?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions