48 lines
1.5 KiB
Go
48 lines
1.5 KiB
Go
// Package schedulecapture records a schedule's firing as a capture on the
|
|
// reserved schedule source, where armed bindings pick it up like any other
|
|
// event.
|
|
package schedulecapture
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"fmt"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/source"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
)
|
|
|
|
// Firer captures schedule firings. Retention and MaxEvents are the capture
|
|
// table's prune-on-write bounds, as config.CaptureConfig sets for webhooks.
|
|
type Firer struct {
|
|
Captures storecontract.CaptureStore
|
|
Retention time.Duration
|
|
MaxEvents int
|
|
}
|
|
|
|
// Fire records one firing and returns its capture id. The tasks it starts are
|
|
// the binding dispatches recorded against that id. A retried firing of the
|
|
// same slot shares its delivery, so no binding starts a second task for it.
|
|
func (f Firer) Fire(ctx context.Context, fired source.ScheduleFired) (string, error) {
|
|
if f.Captures == nil {
|
|
return "", errors.New("schedulecapture: no capture store")
|
|
}
|
|
body, err := fired.Body()
|
|
if err != nil {
|
|
return "", fmt.Errorf("schedulecapture: encode firing: %w", err)
|
|
}
|
|
id, err := f.Captures.InsertCapture(ctx, storecontract.CapturedEvent{
|
|
ReceivedAt: fired.FiredAt.UTC(),
|
|
Source: source.SchedulePath,
|
|
ContentType: "application/json",
|
|
Body: string(body),
|
|
Authenticated: true,
|
|
Delivery: fired.Delivery(),
|
|
}, f.Retention, f.MaxEvents)
|
|
if err != nil {
|
|
return "", fmt.Errorf("schedulecapture: capture firing of %q: %w", fired.ScheduleID, err)
|
|
}
|
|
return id, nil
|
|
}
|