-
Notifications
You must be signed in to change notification settings - Fork 699
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
Fix registration of event_bridge
and pub_sub
webhooks
#1635
Conversation
ebd73da
to
4fc63ac
Compare
Hi @nelsonwittwer @paulomarg, I hope you're well. I noticed that currently, it's impossible to use the gem with event_bridge or pub_sub webhooks. I wrote a small fix that resolves this problem. I hope you will consider merging it quickly. Thank you for your time and attention to this matter. |
4fc63ac
to
9040bf4
Compare
@jeremywrowe @minichate Could anyone review and merge it, please? Due to this bug, it's impossible to use non-HTTP webhooks with |
This seems reasonable to me, @nelsonwittwer would you mind having a look? |
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.
Thanks for researching what would be needed to bring these subscriptions into this gem! It doesn't seem like that big of a shift to support this which is encouraging.
My one question is about the handler. Can you provide an example? From what I remember the naming of those files have to be exactly precise to work out of the box. I wouldn't think they would need to be different than http handlers but it sounds like they might be?
``` | ||
|
||
When registering for an EventBridge or PubSub Webhook you'll need to implement a handler that will fetch webhooks from the queue and process them yourself. |
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.
Do you have an example you could provide here?
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.
Hey @nelsonwittwer, I opened one of the issues being addressed here #1505. If it's ok I'm going to jump in here to try and speed this up, since its been causing my team some issues.
In our GCP implementation, all Shopify events (webhooks) are sent to the same subscriber endpoint (ex: /shopify/events
) which dispatches (handles) the jobs. The naming conventions for the jobs are the same for HTTP and Pub/Sub. Here is an example:
module Shopify::EventsController < ApplicationController
def dispatch
event_job_klass.perform_later(topic: event_topic, shop: event_shop, body: event_body)
head :created
end
private
def event_topic
params[:message][:attributes]['X-Shopify-Topic']
end
def event_shop
params[:message][:attributes]['X-Shopify-Shop-Domain']
end
def event_body
JSON.parse(Base64.decode64(params[:message][:data])).to_h
end
def event_job_klass
event_job_klass_name.constantize
end
def event_job_klass_name
"#{event_jobs_namespace}/#{event_topic.sub('/', '_')}_job".classify
end
def event_jobs_namespace
ShopifyApp.configuration.webhook_jobs_namespace
end
end
end
Thanks for the fix @kirillplatonov 🙏 Hopefully this makes it into the next release 🤞
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.
@nelsonwittwer Unfortunately, I can't share publicly the code we use for the EventBridge dispatcher. But the way it works is by pulling events from the SQS and delegating them to webhook jobs using the same convention as HTTP webhooks. The example above from @kcamcam for pub_sub
is similar.
It would be very helpful to create built-in event_bridge
and pub_sub
handlers for that gem. But that's a topic for another pull request. For now, it would be great if we could merge this tiny fix that will allow everyone to at least start using their own handlers with shopify_app
.
b4543b5
to
c02c8f9
Compare
@nelsonwittwer this PR is rather a And I hope this bugfix could be included in the next release. If I can do anything to make it easier for you to merge this PR please let me know. |
Hi @nelsonwittwer, we're also running into this bug on our side. Is there anything we can help with to expedite getting this merged? We use event bridge and have a similar sol'n to @kcamcam and @kirillplatonov. This PR would fix our issue, but in the meantime we're going to be creating empty classes that follow the |
@nelsonwittwer any chances it will be merged until the next release? It's really tiny fix that doesn't affect HTTP webhooks users. If you could provide some feedback on what need to be changed in the PR in order to be merged I will appreciate that 🙏 |
Until PR is merged the following monkey patch can be used: # config/initializers/shopify_app.rb
Rails.application.config.after_initialize do
ShopifyAPI::Context.setup(...)
# patch
class ShopifyApp::WebhooksManager
def self.add_registrations
return unless ShopifyApp.configuration.has_webhooks?
ShopifyApp::Logger.debug("Adding registrations to webhooks")
ShopifyApp.configuration.webhooks.each do |attributes|
webhook_path = path(attributes)
delivery_method = attributes[:delivery_method] || :http
ShopifyAPI::Webhooks::Registry.add_registration(
topic: attributes[:topic],
delivery_method: delivery_method,
path: webhook_path,
handler: (delivery_method == :http) ? webhook_job_klass(webhook_path) : nil,
fields: attributes[:fields]
)
end
end
end
# should be called after patch
ShopifyApp::WebhooksManager.add_registrations
end |
@nelsonwittwer Running into this too. Any chance we can get this merged in and not have to monkey patch? |
@nelsonwittwer Can you PLEASE merge this? It's been months for what is basically a 1-line fix. |
Thank you! 🙌 |
So sorry for the delay :( We are working on improving our priorities/processes for open source work. We know we can do better here and we appreciate your contributions and patience on this. |
What this PR does
Right now when you try to add
event_bridge
webhooks the gem generates an error:/Users/kirill/Code/shopify_app/lib/shopify_app/managers/webhooks_manager.rb:78:in `webhook_job_klass': ShopifyApp::MissingWebhookJobError
The error is caused by the webhook's
handle
attribute which is used only byhttp
webhooks. This PR makes this attribute empty forevent_bridge
andpub_sub
webhooks.Resolves #1505
Resolves #1297
Reviewer's guide to testing
Try adding a webhook with the delivery method
event_bridge
. It shouldn't generate an error anymore. Example:Checklist
Before submitting the PR, please consider if any of the following are needed:
CHANGELOG.md
if the changes would impact usersREADME.md
, if appropriate./docs
, if necessary