-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support JSON-formatted string as messages #25
Conversation
Using BarbequeClient for enqueuing a message, it might be Array or Hash because it is posted as JSON format with `Content-Type: application/json` header. However in some cases, e.g. posted as query parameter style or enqueued via SNS subscriptions, the message comes as JSON-formatted string. So this patch make a message which is String parse as JSON.
Ah~, will take a look later |
Travis CI's building with Ruby 2.3.1 has failed by a rubygems bug. https://travis-ci.org/cookpad/barbeque/jobs/238602336 https://bugs.ruby-lang.org/issues/12326 |
@@ -13,7 +13,11 @@ def assign_body(message_body) | |||
super | |||
@application = message_body['Application'] | |||
@job = message_body['Job'] | |||
@body = message_body['Message'] | |||
@body = if message_body['Message'].is_a?(String) | |||
JSON.parse(message_body['Message']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q. Are there any cases a non-JSON string comes here? In that case, what will be happened?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Basically no. If non-JSON string came, the exception handling (shows the follow) world occur.
barbeque/lib/barbeque/message.rb
Lines 17 to 20 in 8ea3873
rescue JSON::ParserError => e | |
ExceptionHandler.handle_exception(e) | |
InvalidMessage.new(raw_message, {}) | |
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks reasonable to me, as far as I know.
It's good if you show us an example HTTP requests which SNS made without credentials.
In Barbeque SNS subscription feature, SQS queues catch SNS notifications directly (#20), so HTTP requests from SNS are not made explicitly. |
I firstly designed Barbeque to be compatible with SNS. But at some moment I might forget it and changed the API's interface and internal data structure regardless of that. So this change made sense... |
@@ -13,7 +13,11 @@ def assign_body(message_body) | |||
super | |||
@application = message_body['Application'] | |||
@job = message_body['Job'] | |||
@body = message_body['Message'] | |||
@body = if message_body['Message'].is_a?(String) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why are both Barbeque::Message::JobExecution
and Barbeque::Message::Notification
modified? Isn't changing only Notification sufficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, "posted as query parameter style"...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think garage_client will send with Content-Body: application/json
. Is there really such a use case?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Honestly there're no use case for us. So I should get rid of this fix for JobExecution message...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Then, let's remove it. Unnecessary code just increases maintenance cost. Future maintainers will be confused about its background and not be able to remove it easily for consistency once such a behavior is introduced.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Even if we change to accept Content-Type: application/x-www-form-urlencoded
in API, the enqueued data format can (and should) be unified, again, for maintenance cost.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Make sense. Removed in fc54849.
lib/barbeque/message/notification.rb
Outdated
@@ -21,7 +21,11 @@ def set_params_from_subscription(subscription) | |||
def assign_body(message_body) | |||
super | |||
@topic_arn = message_body['TopicArn'] | |||
@body = message_body['Message'] | |||
@body = if message_body['Message'].is_a?(String) | |||
JSON.parse(message_body['Message']) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In your company, this is prohibited. ™️
https://github.com/cookpad/styleguide/blob/092ff807549c16b7ca6d63e11e5514d540f974ba/ruby.en.md#control-structures
[MUST] If you write a control structure just after an assignment operator such as =, obey the following form denoted as "good".
# bad
result = if condition
body_code
end
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah~
lib/barbeque/message/notification.rb
Outdated
@body = if message_body['Message'].is_a?(String) | ||
JSON.parse(message_body['Message']) | ||
else | ||
message_body['Message'] |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this line necessary? If "enqueued via SNS subscriptions, the message comes as JSON-formatted string" is true, the line will be never used. Though I don't understand how it works now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Exactly... Fixed it in 5a95022
3339d04
to
5a95022
Compare
There're no use case for now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
Using BarbequeClient for enqueuing a message, it might be Array or Hash
because it is posted as JSON format with
Content-Type: application/json
header.However in some cases, e.g. posted as query parameter style or enqueued
via SNS subscriptions, the message comes as JSON-formatted string. So
this patch make a message which is String parse as JSON.
@cookpad/dev-infra @k0kubun How do you think about it?