| 1 | // SPDX-License-Identifier: AGPL-3.0-or-later |
| 2 | |
| 3 | package webhook |
| 4 | |
| 5 | import ( |
| 6 | "context" |
| 7 | "encoding/json" |
| 8 | |
| 9 | "github.com/jackc/pgx/v5/pgxpool" |
| 10 | |
| 11 | "github.com/tenseleyFlow/shithub/internal/worker" |
| 12 | ) |
| 13 | |
| 14 | // Job kinds shipped by S33. Registered by cmd/shithubd/worker.go. |
| 15 | const ( |
| 16 | KindWebhookFanout worker.Kind = "webhook:fanout" |
| 17 | KindWebhookDeliver worker.Kind = "webhook:deliver" |
| 18 | KindWebhookPurgeOld worker.Kind = "webhook:purge_old" |
| 19 | ) |
| 20 | |
| 21 | // deliverPayload is the schema for a `webhook:deliver` job. The |
| 22 | // fanout creates one job per delivery row; redeliveries enqueue |
| 23 | // directly through this same shape. |
| 24 | type deliverPayload struct { |
| 25 | DeliveryID int64 `json:"delivery_id"` |
| 26 | } |
| 27 | |
| 28 | // MarshalDeliverPayload exposes the json shape so downstream packages |
| 29 | // (worker registration, tests) build the same payload without |
| 30 | // duplicating the struct. |
| 31 | func MarshalDeliverPayload(deliveryID int64) ([]byte, error) { |
| 32 | return json.Marshal(deliverPayload{DeliveryID: deliveryID}) |
| 33 | } |
| 34 | |
| 35 | // UnmarshalDeliverPayload extracts the id from a worker job's raw |
| 36 | // JSON. Used by the registered handler. |
| 37 | func UnmarshalDeliverPayload(raw json.RawMessage) (int64, error) { |
| 38 | var p deliverPayload |
| 39 | if err := json.Unmarshal(raw, &p); err != nil { |
| 40 | return 0, err |
| 41 | } |
| 42 | return p.DeliveryID, nil |
| 43 | } |
| 44 | |
| 45 | // workerEnqueueDeliver is the convenience the in-package callers |
| 46 | // (ping, redeliver) use so they don't have to repeat the marshal + |
| 47 | // enqueue boilerplate. |
| 48 | func workerEnqueueDeliver(ctx context.Context, pool *pgxpool.Pool, deliveryID int64) (int64, error) { |
| 49 | return worker.Enqueue(ctx, pool, KindWebhookDeliver, |
| 50 | deliverPayload{DeliveryID: deliveryID}, worker.EnqueueOptions{}) |
| 51 | } |
| 52 |