-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
200 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
/gemfiles/ | ||
/pkg/ | ||
/spec/reports/ | ||
/tmp/ | ||
tmp/ | ||
.pry_history | ||
.rspec_status | ||
Gemfile.lock | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ | |
|
||
module Downstream | ||
class Config | ||
attr_accessor :async_queue | ||
attr_writer :namespace | ||
|
||
def namespace | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,53 @@ | ||
require_relative "subscriber_job" | ||
|
||
module Downstream | ||
module Stateless | ||
class Subscriber | ||
attr_reader :callable | ||
include AfterCommitEverywhere | ||
|
||
attr_reader :callable, :async | ||
|
||
def initialize(callable) | ||
def initialize(callable, async: false) | ||
@callable = callable | ||
@async = async | ||
end | ||
|
||
def async? | ||
!!async | ||
end | ||
|
||
def call(name, event) | ||
if (callable.respond_to?(:arity) && callable.arity == 2) || callable.method(:call).arity == 2 | ||
callable.call(name, event) | ||
def call(_name, event) | ||
if async? | ||
if callable.is_a?(Proc) || callable.name.nil? | ||
raise ArgumentError, "Anonymous subscribers (blocks/procs/lambdas or anonymous modules) cannot be asynchronous" | ||
end | ||
|
||
raise ArgumentError, "Async subscriber must be a module/class, not instance" unless callable.is_a?(Module) | ||
|
||
after_commit do | ||
SubscriberJob.then do |job| | ||
if (queue_name = async_queue_name) | ||
job.set(queue: queue_name) | ||
else | ||
job | ||
end | ||
end.perform_later(event, callable.name) | ||
end | ||
else | ||
callable.call(event) | ||
end | ||
end | ||
|
||
private | ||
|
||
def async_queue_name | ||
return @async_queue_name if defined?(@async_queue_name) | ||
|
||
name = async[:queue] if async.is_a?(Hash) | ||
name ||= Downstream.config.async_queue | ||
|
||
@async_queue_name = name | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Downstream | ||
module Stateless | ||
class SubscriberJob < ActiveJob::Base | ||
def perform(event, callable) | ||
callable.constantize.call(event) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
test: | ||
adapter: sqlite3 | ||
database: tmp/test.sqlite3 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
ActiveRecord::Schema.define do | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
# frozen_string_literal: true | ||
|
||
require "spec_helper" | ||
|
||
module TestSubscriberJob | ||
module Subscriber | ||
class << self | ||
def events | ||
@events ||= [] | ||
end | ||
|
||
def call(event) | ||
events << event | ||
end | ||
end | ||
end | ||
end | ||
|
||
describe Downstream::Stateless::SubscriberJob do | ||
let(:event_class) { Downstream::TestEvent } | ||
let(:event) { event_class.new(user_id: 1) } | ||
let(:callable) { "TestSubscriberJob::Subscriber" } | ||
|
||
subject { described_class.perform_now(event, callable) } | ||
|
||
it "handles an event" do | ||
subject | ||
expect(TestSubscriberJob::Subscriber.events.size).to eq 1 | ||
expect(TestSubscriberJob::Subscriber.events.first).to eq event | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters