Skip to content
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

Merged
merged 5 commits into from
Jun 2, 2017

Conversation

makimoto
Copy link
Contributor

@makimoto makimoto commented Jun 2, 2017

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?

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.
@makimoto makimoto requested a review from k0kubun June 2, 2017 04:05
@k0kubun
Copy link
Collaborator

k0kubun commented Jun 2, 2017

enqueued via SNS subscriptions

Ah~, will take a look later

@makimoto
Copy link
Contributor Author

makimoto commented Jun 2, 2017

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
So I update .travis to change target rubies. c7f4ef5

@@ -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'])
Copy link

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?

Copy link
Contributor Author

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.

rescue JSON::ParserError => e
ExceptionHandler.handle_exception(e)
InvalidMessage.new(raw_message, {})
end

Copy link

@taiki45 taiki45 left a 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.

@makimoto
Copy link
Contributor Author

makimoto commented Jun 2, 2017

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.

@k0kubun
Copy link
Collaborator

k0kubun commented Jun 2, 2017

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)
Copy link
Collaborator

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?

Copy link
Collaborator

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"...

Copy link
Collaborator

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?

Copy link
Contributor Author

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...

Copy link
Collaborator

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.

Copy link
Collaborator

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.

Copy link
Contributor Author

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.

@@ -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'])
Copy link
Collaborator

@k0kubun k0kubun Jun 2, 2017

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

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah~

@body = if message_body['Message'].is_a?(String)
JSON.parse(message_body['Message'])
else
message_body['Message']
Copy link
Collaborator

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.

Copy link
Contributor Author

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

Copy link
Collaborator

@k0kubun k0kubun left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

@makimoto makimoto merged commit 013d1ce into cookpad:master Jun 2, 2017
@makimoto makimoto deleted the json-formatted-message branch June 2, 2017 12:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants