mirror of
https://github.com/usememos/memos.git
synced 2025-06-05 22:09:59 +02:00
chore: add asynchronous webhook dispatch
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"io"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
@@ -63,3 +64,17 @@ func Post(requestPayload *v1pb.WebhookRequestPayload) error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
// PostAsync posts the message to webhook endpoint asynchronously.
|
||||
// It spawns a new goroutine to handle the request and does not wait for the response.
|
||||
func PostAsync(requestPayload *v1pb.WebhookRequestPayload) {
|
||||
go func() {
|
||||
if err := Post(requestPayload); err != nil {
|
||||
// Since we're in a goroutine, we can only log the error
|
||||
slog.Warn("Failed to dispatch webhook asynchronously",
|
||||
slog.String("url", requestPayload.Url),
|
||||
slog.String("activityType", requestPayload.ActivityType),
|
||||
slog.Any("err", err))
|
||||
}
|
||||
}()
|
||||
}
|
||||
|
@@ -689,9 +689,9 @@ func (s *APIV1Service) dispatchMemoRelatedWebhook(ctx context.Context, memo *v1p
|
||||
}
|
||||
payload.ActivityType = activityType
|
||||
payload.Url = hook.URL
|
||||
if err := webhook.Post(payload); err != nil {
|
||||
return errors.Wrap(err, "failed to post webhook")
|
||||
}
|
||||
|
||||
// Use asynchronous webhook dispatch
|
||||
webhook.PostAsync(payload)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
Reference in New Issue
Block a user