Files
archie/internal/infrastructure/postgres/task_alerts.go
T

92 lines
3.2 KiB
Go

package postgres
import (
"context"
"errors"
"fmt"
"github.com/jackc/pgx/v5"
"github.com/samcharles93/archie-core/internal/domain/alert"
"github.com/samcharles93/archie-core/internal/domain/org"
"github.com/samcharles93/archie-core/internal/domain/workflow"
"github.com/samcharles93/archie-core/internal/infrastructure/postgres/postgresdb"
"github.com/samcharles93/archie-core/internal/taskstate"
)
// systemResolver is recorded as the resolver of an alert the store clears
// itself because the task moved on.
const systemResolver = "system"
// recordTransition writes a transition's audit row and the alerts that
// transition raises or clears, in the same transaction. Every task status
// move goes through it, so the alerts follow the state without a poller.
func recordTransition(ctx context.Context, q *postgresdb.Queries, p postgresdb.InsertTransitionParams) error {
if err := q.InsertTransition(ctx, p); err != nil {
return err
}
return syncTaskAlerts(ctx, q, p)
}
// needsHuman reports whether a task in status, parked with class, is waiting
// on an operator's decision.
func needsHuman(status, class string) bool {
return status == workflow.StatusWaitingHuman || (status == workflow.StatusParked && class == taskstate.ParkNeedsHuman)
}
// failed reports whether a task in status, parked with class, will not
// succeed without an operator starting it over.
func failed(status, class string) bool {
return status == workflow.StatusDead || (status == workflow.StatusParked && class == taskstate.ParkTerminal)
}
func syncTaskAlerts(ctx context.Context, q *postgresdb.Queries, p postgresdb.InsertTransitionParams) error {
leaving := p.FromStatus == workflow.StatusWaitingHuman || p.FromStatus == workflow.StatusParked
if !leaving && p.ToStatus != workflow.StatusWaitingHuman && p.ToStatus != workflow.StatusParked && p.ToStatus != workflow.StatusDead {
return nil
}
info, err := q.TaskAlertInfo(ctx, p.TaskID)
if errors.Is(err, pgx.ErrNoRows) {
return nil
}
if err != nil {
return err
}
subject := fmt.Sprintf("task:%d", p.TaskID)
link := fmt.Sprintf("/tasks/%d", p.TaskID)
name := fmt.Sprintf("Task #%d", p.TaskID)
described := name
if info.Title != "" {
described = fmt.Sprintf("%s %q", name, info.Title)
}
key := func(kind string) alert.Key {
return alert.Key{Org: org.OrgID(info.OrgID), Kind: kind, Subject: subject}
}
if needsHuman(p.ToStatus, info.ParkClass) {
body := described + " is waiting for your approval or review."
if p.ToStatus == workflow.StatusParked {
body = described + " is parked and needs a decision."
}
_, err := raiseAlert(ctx, q, alert.Input{
Key: key(alert.KindTaskNeedsHuman), Severity: alert.SeverityAction, Condition: true,
Title: name + " needs attention", Body: body, Link: link,
})
if err != nil {
return err
}
} else if leaving {
if err := resolveAlert(ctx, q, key(alert.KindTaskNeedsHuman), systemResolver); err != nil {
return err
}
}
if failed(p.ToStatus, info.ParkClass) {
_, err := raiseAlert(ctx, q, alert.Input{
Key: key(alert.KindTaskFailed), Severity: alert.SeverityWarning,
Title: name + " failed", Body: described + " failed and will not run again until you retry it.", Link: link,
})
return err
}
return nil
}