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

Add event that is being fired upon the webhook's dispatch #150

Merged
merged 3 commits into from
Nov 27, 2023

Conversation

thannaske
Copy link
Contributor

The idea for this PR came in regards of discussion #91, which introduced the idea of a database persistence layer. While this would be currently out of scope for this package, I figured that you could easily achieve everything the creator of the idea intended to do by utilising the package's events.

The only thing that was missing to allow persisting the webhook in whichever way the developer wants, was an event that is being fired exactly once when a new webhook is being dispatched (not to confuse with the dispatching of the job which could occur multiple times when retrying). For this reason, this PR adds the WebhookCallDispatchedEvent which fires when the dispatch() method for the WebhookCall object is being called.

One could then simply listen to the WebhookCallDispatchedEvent, persist it to the database and e.g. increase attempt counts or update the web hook's status based on fired WebhookCallFailedEvents and WebhookCallSucceededEvents.

I was unable to reuse the abstract WebhookCallEvent as it contains a lot of attributes that are specific to an actual attempt to send the webhook to its target URL.

Thank you for your work and of course no hard feelings if this PR is going to be declined.

@@ -229,6 +230,16 @@ public function dispatch(): PendingDispatch
{
$this->prepareForDispatch();

event(new WebhookCallDispatchedEvent(
Copy link
Member

Choose a reason for hiding this comment

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

This event should be named DispatchingWebhookCallEvent as it is fired before the actual dispatch.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Very good point, thank you. I amended the event's name and updated the README.md accordingly.

@freekmurze freekmurze merged commit 0347b3c into spatie:main Nov 27, 2023
8 checks passed
@freekmurze
Copy link
Member

Thanks!

@siarheipashkevich
Copy link

@thannaske which database structure of table did you use for storing handling outgoing webhooks?

@thannaske
Copy link
Contributor Author

@siarheipashkevich Please use GitHub's discussions for questions regarding the usage of the package, a (closed and over half a year old) Pull Request is not the right place for it.

You can however just put all available properties of the DispatchingWebhookCallEvent into a table. The exact table structure depends on your individual needs.

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.

3 participants