77 lines
2.1 KiB
Go
77 lines
2.1 KiB
Go
package runs
|
|
|
|
import (
|
|
"context"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/modules/runs/internal/store"
|
|
)
|
|
|
|
// RunnerHealth is the state of the workers that execute runs.
|
|
type RunnerHealth struct {
|
|
Workers int `json:"workers"`
|
|
// Error is the last failure to claim work, until a claim succeeds again.
|
|
Error string `json:"error,omitzero"`
|
|
ErrorAt *time.Time `json:"error_at,omitzero"`
|
|
}
|
|
|
|
// RunCounts is how many runs are in each state that still needs work or a
|
|
// person.
|
|
type RunCounts struct {
|
|
Queued int `json:"queued"`
|
|
Running int `json:"running"`
|
|
// Waiting runs wait for a timer or a signal.
|
|
Waiting int `json:"waiting"`
|
|
// WaitingApproval runs ended waiting for an approval.
|
|
WaitingApproval int `json:"waiting_approval"`
|
|
// Parked runs stopped for a person, who retries them from a step.
|
|
Parked int `json:"parked"`
|
|
}
|
|
|
|
type claimFailure struct {
|
|
err string
|
|
at time.Time
|
|
}
|
|
|
|
// noteClaim records the outcome of a worker's attempt to claim a run.
|
|
func (e *Engine) noteClaim(err error) {
|
|
if err == nil {
|
|
e.claimFailure.Store(nil)
|
|
return
|
|
}
|
|
e.claimFailure.Store(&claimFailure{err: err.Error(), at: time.Now()})
|
|
}
|
|
|
|
// Runner reports the workers and the last failure to claim work.
|
|
func (e *Engine) Runner() RunnerHealth {
|
|
h := RunnerHealth{Workers: e.opts.Workers}
|
|
if f := e.claimFailure.Load(); f != nil {
|
|
h.Error, h.ErrorAt = f.err, &f.at
|
|
}
|
|
return h
|
|
}
|
|
|
|
// Counts returns how many of orgID's runs are in each state that still needs
|
|
// work or a person.
|
|
func (e *Engine) Counts(ctx context.Context, orgID string) (RunCounts, error) {
|
|
n, err := e.db.Counts(ctx, orgID)
|
|
if err != nil {
|
|
return RunCounts{}, err
|
|
}
|
|
return RunCounts{
|
|
Queued: n[store.StateQueued], Running: n[store.StateRunning], Waiting: n[store.StateWaiting],
|
|
WaitingApproval: n[store.StateWaitingApproval], Parked: n[store.StateParked],
|
|
}, nil
|
|
}
|
|
|
|
// LastFailed returns orgID's most recently failed run, or nil when none has
|
|
// failed.
|
|
func (e *Engine) LastFailed(ctx context.Context, orgID string) (*RunView, error) {
|
|
s, ok, err := e.db.LastFailed(ctx, orgID)
|
|
if err != nil || !ok {
|
|
return nil, err
|
|
}
|
|
v := view(s, nil)
|
|
return &v, nil
|
|
}
|