This repository has been archived by the owner on May 11, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
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
236 additions
and
3 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 |
---|---|---|
|
@@ -9,3 +9,5 @@ | |
|
||
# rspec failure tracking | ||
.rspec_status | ||
|
||
/config.yaml |
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
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,4 +1,10 @@ | ||
#!/usr/bin/env ruby | ||
# frozen_string_literal: true | ||
|
||
require "nats_streamer" | ||
require_relative "../lib/nats_streamer" | ||
|
||
CONFIG = YAML.load(File.read(ARGV[0]), symbolize_names: true) | ||
|
||
Sync do | ||
NatsStreamer::Application.new(url: "nats://127.0.0.1:4222", config: NatsStreamer::Config.new(CONFIG)).run | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# frozen_string_literal: true | ||
|
||
class NatsStreamer::Application | ||
extend Dry::Initializer | ||
|
||
include NatsStreamer::Logger | ||
include Memery | ||
|
||
option :url | ||
option :config | ||
|
||
def run | ||
config.streams.each do |name, subjects| | ||
NatsStreamer::Stream.new(jsm:, name:, subjects:).run | ||
end | ||
|
||
Async::Notification.new.wait # wait forever | ||
end | ||
|
||
private | ||
|
||
memoize def client = NATS.connect(url) | ||
|
||
def jsm = client.jsm | ||
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,22 @@ | ||
# frozen_string_literal: true | ||
|
||
class NatsStreamer::Config < Dry::Struct | ||
Types = Dry.Types | ||
|
||
class Subscriber < Dry::Struct | ||
attribute :name, Types::String # Must be uniq | ||
attribute :url, Types::String | ||
attribute :params, Types::Hash.map(Types::Coercible::String, Types::String).default({}.freeze) | ||
end | ||
|
||
attribute :streams, Types::Hash.map( | ||
Types::Coercible::String, | ||
Types::Hash.map( | ||
Types::Coercible::String, | ||
Types::Hash.map( | ||
Types::Coercible::String, | ||
Types::Array.of(Subscriber) | ||
) | ||
) | ||
) | ||
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,20 @@ | ||
# frozen_string_literal: true | ||
|
||
class NatsStreamer::Deliverer | ||
extend Dry::Initializer | ||
|
||
include NatsStreamer::Logger | ||
include Memery | ||
|
||
# TODO: how to propely unsubscribe? | ||
option :subscriber | ||
|
||
def deliver(**) = connection.post(".", **) | ||
|
||
memoize def connection | ||
Faraday.new(subscriber.url) do |f| | ||
f.request :json | ||
f.response :raise_error | ||
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,43 @@ | ||
# frozen_string_literal: true | ||
|
||
class NatsStreamer::Listener | ||
extend Dry::Initializer | ||
|
||
include NatsStreamer::Logger | ||
include Memery | ||
|
||
# TODO: how to propely unsubscribe? | ||
option :jsm | ||
option :subject | ||
option :subscriber | ||
|
||
def run | ||
pull do |msg| | ||
Async { handle_message(msg) } | ||
end | ||
end | ||
|
||
private | ||
|
||
memoize def params_renderer = NatsStreamer::ParamsRenderer.new(subject:, subscriber:) | ||
memoize def deliverer(subscriber) = NatsStreamer::Deliverer.new(subscriber:) | ||
|
||
def pull(&) | ||
psub = @jsm.pull_subscribe(subject, "nats-streamer-subscriber-#{subscriber.name}") | ||
|
||
loop do | ||
psub.fetch(1).each(&) | ||
rescue NATS::IO::Timeout | ||
debug { "Pulling timeout, retrying..." } | ||
end | ||
end | ||
|
||
def handle_message(msg) | ||
event = JSON.parse(msg.data, symbolize_names: true) | ||
debug { "subject=#{subject}, event=#{event}" } | ||
|
||
deliverer(subscriber).deliver(**params_renderer.render(event)) | ||
|
||
msg.ack | ||
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 @@ | ||
# frozen_string_literal: true | ||
|
||
module NatsStreamer::Logger | ||
[:debug, :info, :warn, :error, :fatal].each do |name| | ||
define_method(name) do |msg = nil, &block| | ||
Console.logger.public_send(name, self, msg, &block) | ||
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,18 @@ | ||
# frozen_string_literal: true | ||
|
||
class NatsStreamer::ParamsRenderer | ||
extend Dry::Initializer | ||
|
||
include NatsStreamer::Logger | ||
include Memery | ||
|
||
option :subject | ||
option :subscriber | ||
|
||
def render(event) | ||
subscriber.params.transform_values do |v| | ||
binding.local_variable_set(:event, event) | ||
ERB.new(v).result(binding) | ||
end.merge(event:, subject:) | ||
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,41 @@ | ||
# frozen_string_literal: true | ||
|
||
class NatsStreamer::Stream | ||
extend Dry::Initializer | ||
|
||
include NatsStreamer::Logger | ||
include Memery | ||
|
||
option :jsm | ||
option :name | ||
option :subjects | ||
|
||
def run | ||
create_stream! | ||
|
||
subjects.each do |subject, events| | ||
events.each do |event_name, subscribers| | ||
subscribers.each do |subscriber| | ||
subject = [subject, event_name].join(".") | ||
NatsStreamer::Listener.new(jsm:, subject:, subscriber:).run | ||
end | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
# ["{subject_name}.{event_name}"] | ||
memoize def subject_names | ||
subjects.flat_map do |subject, events| | ||
[subject].product(events.keys).map { _1.join(".") } | ||
end | ||
end | ||
|
||
def create_stream! | ||
jsm.add_stream(name:, subjects: subject_names) | ||
rescue NATS::JetStream::Error::BadRequest | ||
jsm.delete_stream(name) | ||
retry | ||
end | ||
end |