forked from rubyonjets/jets
-
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.
Merge pull request rubyonjets#700 from rubyonjets/event-helpers
update s3 sns and sqs event helpers to return all events
- Loading branch information
Showing
6 changed files
with
86 additions
and
16 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
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,13 +1,43 @@ | ||
module Jets::Job::Helpers | ||
module S3EventHelper | ||
def s3_events | ||
messages = event["Records"].map do |record| | ||
record["Sns"]["Message"] | ||
end | ||
message.map do |message| | ||
h = JSON.load(message) | ||
ActiveSupport::HashWithIndifferentAccess.new(h) | ||
end | ||
end | ||
|
||
def s3_events? | ||
event["Records"]&.any? { |r| r.dig("Sns", "Message") } | ||
end | ||
|
||
def s3_objects | ||
records = s3_event["Records"] | ||
records.map do |record| | ||
record["s3"]["object"] | ||
end | ||
end | ||
|
||
def s3_objects? | ||
s3_event["Records"]&.any? { |r| r.dig("s3", "object") } | ||
end | ||
|
||
# Deprecated methods below | ||
def s3_event | ||
message = event["Records"][0]["Sns"]["Message"] | ||
h = JSON.load(message) | ||
ActiveSupport::HashWithIndifferentAccess.new(h) | ||
puts "WARN: s3_event is deprecated".color(:yellow) | ||
puts "It can possibly drop events when come in extremely fast." | ||
puts "Use s3_events instead" | ||
s3_events.first | ||
end | ||
|
||
def s3_object | ||
s3_event["Records"][0]["s3"]["object"] | ||
puts "WARN: s3_object is deprecated".color(:yellow) | ||
puts "It can possibly drop events when come in extremely fast." | ||
puts "Use s3_objects instead" | ||
s3_objects.first | ||
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 |
---|---|---|
@@ -1,8 +1,24 @@ | ||
module Jets::Job::Helpers | ||
module SnsEventHelper | ||
def sns_event_payload | ||
message = event&.dig("Records", 0, "Sns", "Message") | ||
@sns_event_payload ||= ActiveSupport::HashWithIndifferentAccess.new(JSON.load(message)) | ||
module SnsEventHelper | ||
def sns_event_payloads | ||
records = event["Records"] | ||
return [] unless records | ||
records.map do |record| | ||
message = record["Sns"]["Message"] | ||
ActiveSupport::HashWithIndifferentAccess.new(JSON.load(message)) | ||
end | ||
end | ||
|
||
def sns_event_payloads? | ||
event["Records"]&.any? { |r| r.dig("Sns", "Message") } | ||
end | ||
|
||
# Deprecated methods below | ||
def sns_event_payload | ||
puts "WARN: sns_event_payload is deprecated".color(:yellow) | ||
puts "It can possibly drop events when come in extremely fast." | ||
puts "Use sns_event_payloads instead" | ||
sns_event_payloads.first | ||
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 |
---|---|---|
@@ -1,8 +1,24 @@ | ||
module Jets::Job::Helpers | ||
module SqsEventHelper | ||
def sqs_event_payload | ||
message = event&.dig("Records", 0, "body") | ||
@sqs_event_payload ||= ActiveSupport::HashWithIndifferentAccess.new(JSON.parse(message)) | ||
end | ||
module SqsEventHelper | ||
def sqs_event_payloads | ||
records = event["Records"] | ||
return [] unless records | ||
records.map do |record| | ||
message = record["body"] | ||
ActiveSupport::HashWithIndifferentAccess.new(JSON.load(message)) | ||
end | ||
end | ||
end | ||
|
||
def sqs_event_payloads? | ||
event["Records"]&.any? { |r| r.dig("body") } | ||
end | ||
|
||
# Deprecated methods below | ||
def sqs_event_payload | ||
puts "WARN: sqs_event_payload is deprecated".color(:yellow) | ||
puts "It can possibly drop events when come in extremely fast." | ||
puts "Use sqs_event_payloads instead" | ||
sqs_event_payloads.first | ||
end | ||
end | ||
end |