Schedules list as a table with enable, run now, edit, duplicate, delete and view runs; built-ins only enable, retime and run. The runs page links each run's tasks through its capture's dispatches. The Start node gains a Schedule trigger that picks or creates a schedule and binds the schedule source to the workflow. Times show in a per-browser display zone set under Appearance; the cron time zone and run retention are edited on the Schedules page.
240 lines
7.6 KiB
Go
240 lines
7.6 KiB
Go
package webui
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"net/http"
|
|
"time"
|
|
|
|
"github.com/samcharles93/archie-core/internal/domain/org"
|
|
"github.com/samcharles93/archie-core/internal/domain/scheduling"
|
|
"github.com/samcharles93/archie-core/internal/domain/storecontract"
|
|
)
|
|
|
|
// ScheduleStore is the State Store's schedule contract as the dashboard uses
|
|
// it.
|
|
type ScheduleStore interface {
|
|
ListSchedules(ctx context.Context) ([]scheduling.Schedule, error)
|
|
GetSchedule(ctx context.Context, id string) (scheduling.Schedule, error)
|
|
PutSchedule(ctx context.Context, s scheduling.Schedule) (scheduling.Schedule, error)
|
|
DeleteSchedule(ctx context.Context, id string) error
|
|
RequestScheduleRun(ctx context.Context, id string) (scheduling.Run, error)
|
|
ListScheduleRuns(ctx context.Context, scheduleID string, limit int) ([]scheduling.Run, error)
|
|
}
|
|
|
|
// scheduleRunsShown is how many runs the runs page lists.
|
|
const scheduleRunsShown = 100
|
|
|
|
type scheduleOwnerView struct {
|
|
Kind scheduling.OwnerKind `json:"kind"`
|
|
ID string `json:"id,omitempty"`
|
|
// Name is the owning org's name, resolved here so the dashboard never
|
|
// stores or invents one.
|
|
Name string `json:"name,omitempty"`
|
|
}
|
|
|
|
type scheduleRunView struct {
|
|
ID int64 `json:"id"`
|
|
ScheduleID string `json:"schedule_id"`
|
|
ScheduledFor time.Time `json:"scheduled_for"`
|
|
Started *time.Time `json:"started,omitempty"`
|
|
Finished *time.Time `json:"finished,omitempty"`
|
|
Result scheduling.RunResult `json:"result"`
|
|
Error string `json:"error,omitempty"`
|
|
TaskIDs []int64 `json:"task_ids,omitempty"`
|
|
}
|
|
|
|
type scheduleView struct {
|
|
ID string `json:"id"`
|
|
Name string `json:"name"`
|
|
Owner scheduleOwnerView `json:"owner"`
|
|
Enabled bool `json:"enabled"`
|
|
BuiltIn bool `json:"built_in"`
|
|
Timing scheduling.Timing `json:"timing"`
|
|
NextRun *time.Time `json:"next_run,omitempty"`
|
|
LastRun *scheduleRunView `json:"last_run,omitempty"`
|
|
}
|
|
|
|
// scheduleInput is what the dashboard may set on a schedule. Owner and
|
|
// built-in are the store's to decide.
|
|
type scheduleInput struct {
|
|
Name string `json:"name"`
|
|
Enabled bool `json:"enabled"`
|
|
Timing scheduling.Timing `json:"timing"`
|
|
}
|
|
|
|
func optionalTime(t time.Time) *time.Time {
|
|
if t.IsZero() {
|
|
return nil
|
|
}
|
|
return &t
|
|
}
|
|
|
|
func runView(r scheduling.Run) scheduleRunView {
|
|
return scheduleRunView{
|
|
ID: r.ID, ScheduleID: r.ScheduleID, ScheduledFor: r.ScheduledFor,
|
|
Started: optionalTime(r.Started), Finished: optionalTime(r.Finished),
|
|
Result: r.Result, Error: r.Error,
|
|
}
|
|
}
|
|
|
|
func (s *Server) ownerView(ctx context.Context, owner scheduling.Owner, names map[org.OrgID]string) scheduleOwnerView {
|
|
view := scheduleOwnerView{Kind: owner.Kind, ID: owner.ID}
|
|
id := org.OrgID(owner.ID)
|
|
if owner.Kind == scheduling.OwnerInstance {
|
|
id = org.DefaultOrgID
|
|
}
|
|
name, ok := names[id]
|
|
if !ok && s.Orgs != nil {
|
|
if found, err := s.Orgs.GetOrg(ctx, id); err == nil {
|
|
name = found.Name
|
|
}
|
|
names[id] = name
|
|
}
|
|
view.Name = name
|
|
return view
|
|
}
|
|
|
|
func (s *Server) scheduleView(ctx context.Context, sched scheduling.Schedule, names map[org.OrgID]string) scheduleView {
|
|
view := scheduleView{
|
|
ID: sched.ID, Name: sched.Name, Owner: s.ownerView(ctx, sched.Owner, names),
|
|
Enabled: sched.Enabled, BuiltIn: sched.BuiltIn, Timing: sched.Timing,
|
|
NextRun: optionalTime(sched.NextRun),
|
|
}
|
|
if sched.LastRun != nil {
|
|
last := runView(*sched.LastRun)
|
|
view.LastRun = &last
|
|
}
|
|
return view
|
|
}
|
|
|
|
func (s *Server) scheduleError(ctx context.Context, w http.ResponseWriter, op string, err error) {
|
|
switch {
|
|
case errors.Is(err, scheduling.ErrNotFound):
|
|
http.Error(w, "schedule not found", http.StatusNotFound)
|
|
case errors.Is(err, scheduling.ErrBuiltIn):
|
|
http.Error(w, scheduling.ErrBuiltIn.Error(), http.StatusConflict)
|
|
case errors.Is(err, scheduling.ErrInvalidSpec):
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
default:
|
|
s.Log.ErrorContext(ctx, op, "err", err)
|
|
http.Error(w, op+" failed", http.StatusInternalServerError)
|
|
}
|
|
}
|
|
|
|
func (s *Server) schedulesReady(w http.ResponseWriter) bool {
|
|
if s.Schedules == nil {
|
|
http.Error(w, "schedules unavailable", http.StatusServiceUnavailable)
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func (s *Server) handleSchedules(w http.ResponseWriter, r *http.Request) {
|
|
if !s.schedulesReady(w) {
|
|
return
|
|
}
|
|
list, err := s.Schedules.ListSchedules(r.Context())
|
|
if err != nil {
|
|
s.scheduleError(r.Context(), w, "list schedules", err)
|
|
return
|
|
}
|
|
names := map[org.OrgID]string{}
|
|
views := make([]scheduleView, 0, len(list))
|
|
for _, sched := range list {
|
|
views = append(views, s.scheduleView(r.Context(), sched, names))
|
|
}
|
|
writeJSON(w, map[string]any{"schedules": views})
|
|
}
|
|
|
|
// handleSchedulePut creates the schedule or replaces what the dashboard may
|
|
// change on it. New schedules belong to the instance.
|
|
func (s *Server) handleSchedulePut(w http.ResponseWriter, r *http.Request) {
|
|
if !s.authorizeTaskMutation(w, r) || !s.schedulesReady(w) {
|
|
return
|
|
}
|
|
var input scheduleInput
|
|
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 64<<10)).Decode(&input); err != nil {
|
|
http.Error(w, "invalid schedule", http.StatusBadRequest)
|
|
return
|
|
}
|
|
id := r.PathValue("id")
|
|
sched, err := s.Schedules.GetSchedule(r.Context(), id)
|
|
switch {
|
|
case errors.Is(err, scheduling.ErrNotFound):
|
|
sched = scheduling.Schedule{ID: id, Owner: scheduling.Owner{Kind: scheduling.OwnerInstance}}
|
|
case err != nil:
|
|
s.scheduleError(r.Context(), w, "get schedule", err)
|
|
return
|
|
}
|
|
sched.Name, sched.Enabled, sched.Timing = input.Name, input.Enabled, input.Timing
|
|
saved, err := s.Schedules.PutSchedule(r.Context(), sched)
|
|
if err != nil {
|
|
s.scheduleError(r.Context(), w, "save schedule", err)
|
|
return
|
|
}
|
|
writeJSON(w, s.scheduleView(r.Context(), saved, map[org.OrgID]string{}))
|
|
}
|
|
|
|
func (s *Server) handleScheduleDelete(w http.ResponseWriter, r *http.Request) {
|
|
if !s.authorizeTaskMutation(w, r) || !s.schedulesReady(w) {
|
|
return
|
|
}
|
|
if err := s.Schedules.DeleteSchedule(r.Context(), r.PathValue("id")); err != nil {
|
|
s.scheduleError(r.Context(), w, "delete schedule", err)
|
|
return
|
|
}
|
|
w.WriteHeader(http.StatusNoContent)
|
|
}
|
|
|
|
func (s *Server) handleScheduleRunNow(w http.ResponseWriter, r *http.Request) {
|
|
if !s.authorizeTaskMutation(w, r) || !s.schedulesReady(w) {
|
|
return
|
|
}
|
|
run, err := s.Schedules.RequestScheduleRun(r.Context(), r.PathValue("id"))
|
|
if err != nil {
|
|
s.scheduleError(r.Context(), w, "run schedule", err)
|
|
return
|
|
}
|
|
writeJSON(w, runView(run))
|
|
}
|
|
|
|
// handleScheduleRuns lists a schedule's runs, each with the tasks its firing
|
|
// started: the binding dispatches recorded against the run's capture.
|
|
func (s *Server) handleScheduleRuns(w http.ResponseWriter, r *http.Request) {
|
|
if !s.schedulesReady(w) {
|
|
return
|
|
}
|
|
runs, err := s.Schedules.ListScheduleRuns(r.Context(), r.PathValue("id"), scheduleRunsShown)
|
|
if err != nil {
|
|
s.scheduleError(r.Context(), w, "list schedule runs", err)
|
|
return
|
|
}
|
|
views := make([]scheduleRunView, 0, len(runs))
|
|
for _, run := range runs {
|
|
view := runView(run)
|
|
view.TaskIDs = s.runTasks(r.Context(), run.CaptureID)
|
|
views = append(views, view)
|
|
}
|
|
writeJSON(w, map[string]any{"runs": views})
|
|
}
|
|
|
|
func (s *Server) runTasks(ctx context.Context, captureID string) []int64 {
|
|
if captureID == "" || s.Dispatches == nil {
|
|
return nil
|
|
}
|
|
dispatches, err := s.Dispatches.ListDispatches(ctx, storecontract.DispatchFilter{CaptureID: captureID, Limit: 50})
|
|
if err != nil {
|
|
s.Log.WarnContext(ctx, "schedule run tasks unavailable", "capture", captureID, "err", err)
|
|
return nil
|
|
}
|
|
var ids []int64
|
|
for _, d := range dispatches {
|
|
if d.TaskID != 0 {
|
|
ids = append(ids, d.TaskID)
|
|
}
|
|
}
|
|
return ids
|
|
}
|