Skip to content

Commit

Permalink
added batch method
Browse files Browse the repository at this point in the history
  • Loading branch information
dmccarty committed Sep 16, 2020
1 parent e5fb86b commit 36a5a09
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions lib/pubsub_tie.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,10 @@ def configure_events
def publish(topic, data, resource: nil)
Publisher.publish(topic, data, resource)
end

def batch(topic, data, resource: nil)
Publisher.batch(topic, data, resource)
end
end

require 'pubsub_tie/railtie' if defined? Rails::Railtie
15 changes: 15 additions & 0 deletions lib/pubsub_tie/publisher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@ def publish(topic_sym, data, resource)
end
end

def batch(topic_sym, messages, resource)
topic = @pubsub.
topic(Events.name topic_sym)
messages.each do |data|
topic.publish_async(message(validate_data(topic_sym, data), resource),
publish_time: Time.now.utc) do |result|
unless result.succeeded?
Rails.logger.error(
"Failed to publish #{data} to #{topic_sym} on #{data} due to #{result.error}")
end
end
end
topic.async_publisher.stop.wait!
end

private
def message(data, resource)
# TODO: embed resource in message
Expand Down
2 changes: 1 addition & 1 deletion lib/pubsub_tie/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module PubSubTie
VERSION = "0.0.1"
VERSION = "0.0.2"
end

0 comments on commit 36a5a09

Please sign in to comment.