Store events to avoid duplication. Move event handling to async tasks.#544
Store events to avoid duplication. Move event handling to async tasks.#544
Conversation
7f4139f to
70e7514
Compare
| """ | ||
| def update_from_stripe(id_from_stripe) do | ||
| with customer <- Repo.get_by(StripePlatformCustomer, id_from_stripe: id_from_stripe), | ||
| with %StripePlatformCustomer{} = customer <- Repo.get_by(StripePlatformCustomer, id_from_stripe: id_from_stripe), |
There was a problem hiding this comment.
get_by might return a nil. We weren't restricting the type.
|
We should still consider doing the following here:
|
|
@begedin did not get a chance to implement my comment, but think all of that should still be implemented. |
70e7514 to
504f293
Compare
Working on this one
Done
We now store the event as "unhandled". Returning a 204, however, is not a problem. It's just for us, so we can easily test the response and see that the server differentiates. According to stripe documentation, any 2xx response will be considered as "event is done, do not send it again". That being said, we only return a 204 in the case of "ignored by environment", because we, for example, got a livemode event on staging or vice-versa. We do not return it in case of "unhandled", since in that case, we should just consume the event and (for now) not do anything about it. In this case, we return a 200, same as "errored" or "processed" From https://stripe.com/docs/webhooks#responding-to-a-webhook
|
|
I have now added async processing of webhooks. In order to achieve this, we use a Testing was giving me trouble with this one, but I managed to get around it by explicitly waiting for the child processes to exit, using a |
|
The process is now as follows:
|
|
Something we should consider adding but is probably out of scope for this PR:
|
Add StripeEvent table and model, with changesets and tests Hook events model into webhook processing Add event type Add "unhandled" status Add async processing of webhooks using supervisor Set event to processing when starting to handle it Change 204 to 400 for unhandled events
265ac10 to
c69b126
Compare
|
@begedin if they error out or are unhandled I think that means human intervention time, not machine. We haven't written code yet to effectively handle the edge case, but we can go in and process those events since we're keeping track. I think were in a good state to merge now. |
What's in this PR?
This PR adds a
StripeEventmodel and associated table.Then it hooks into the current webhook handling process.
As a webhook request is received, a
StripeEventrecord is stored, with fieldsOnce the request is processed, the created event is set to either
"processed"or"errored"depending on the processing result.Details
:id_from_stripe. The event record is only created when there is no such event stored already."unprocessed"or"errored". An already processed event is ignored.References
Fixes #512
Fixes #547