feat(ui): schedules table, runs page and Schedule trigger
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.
This commit is contained in:
1 parent
12dd3084e5
commit
d4e563a19c
52 files changed
+1425
-131
No files matched your search
@@ -152,6 +152,9 @@ func wireOrgSurface(srv *webui.Server, store storecontract.TaskStore) {
|
||||
if orgs, ok := store.(org.API); ok {
|
||||
srv.Orgs = orgs
|
||||
}
|
||||
if schedules, ok := store.(webui.ScheduleStore); ok {
|
||||
srv.Schedules = schedules
|
||||
}
|
||||
}
|
||||
|
||||
// wireCatalog attaches the Gateway-owned surfaces the dashboard reads over the
|
||||
|
||||
@@ -11,6 +11,9 @@ import (
|
||||
"github.com/samcharles93/archie-core/internal/webui"
|
||||
)
|
||||
|
||||
// The State Store client serves the dashboard's schedules (wireOrgSurface).
|
||||
var _ webui.ScheduleStore = (*staterpc.Client)(nil)
|
||||
|
||||
func wireServiceLogs(opts Options, srv *webui.Server, tasks *staterpc.Client, chat *gatewayrpc.Client, feed *logging.Feed) (func(), error) {
|
||||
srv.LogFeed = feed
|
||||
srv.LogSources = map[string]webui.LogSource{
|
||||
|
||||
@@ -0,0 +1,239 @@
|
||||
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
|
||||
}
|
||||
@@ -307,9 +307,10 @@ func routeOverride(action access.Action, path string) (access.Action, access.Res
|
||||
return actionManage(action), access.KindIdentity, segmentValue(path, 2), true
|
||||
case matchPrefix(path, "/api/orgs"):
|
||||
return orgRouteAction(action, path), access.KindOrg, segmentValue(path, 2), true
|
||||
case matchPrefix(path, "/api/extensions"):
|
||||
// Installing and enabling an extension runs a process on the host, so it
|
||||
// is the same administration as editing a control-plane resource.
|
||||
case matchPrefix(path, "/api/extensions"), matchPrefix(path, "/api/schedules"):
|
||||
// Installing and enabling an extension runs a process on the host, and
|
||||
// a schedule runs unattended as the instance, so both are the same
|
||||
// administration as editing a control-plane resource.
|
||||
return actionManage(action), access.KindPolicy, segmentValue(path, 2), true
|
||||
case matchPrefix(path, "/api/control-plane/resources"):
|
||||
return controlPlaneResourceRoute(action, path)
|
||||
|
||||
@@ -199,6 +199,10 @@ func TestAdministrationRoutes(t *testing.T) {
|
||||
{http.MethodGet, "/api/access/policies", access.ActionRead},
|
||||
{http.MethodPut, "/api/access/policies", access.ActionManagePolicies},
|
||||
{http.MethodDelete, "/api/access/policies", access.ActionManagePolicies},
|
||||
{http.MethodGet, "/api/schedules", access.ActionRead},
|
||||
{http.MethodPut, "/api/schedules/nightly", access.ActionManageIdentities},
|
||||
{http.MethodPost, "/api/schedules/nightly/runs", access.ActionManageIdentities},
|
||||
{http.MethodDelete, "/api/schedules/nightly", access.ActionDelete},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.method+" "+tt.path, func(t *testing.T) {
|
||||
|
||||
@@ -206,6 +206,10 @@ type Server struct {
|
||||
// routes 503 and lists bindings without their last outcome.
|
||||
Dispatches storecontract.DispatchLedger
|
||||
|
||||
// Schedules reads and edits the instance's schedules and their runs. Nil
|
||||
// answers the /api/schedules routes 503.
|
||||
Schedules ScheduleStore
|
||||
|
||||
// Sources persists capture sources and their signing setting.
|
||||
// Optional: nil makes every
|
||||
// /api/sources route answer 503 and marks no binding unsigned.
|
||||
@@ -281,6 +285,11 @@ func (s *Server) registerCoreRoutes(mux *http.ServeMux) {
|
||||
mux.HandleFunc("GET /api/skills", instanceOnly(s.handleSkills))
|
||||
mux.HandleFunc("GET /api/curators", instanceOnly(s.handleCurators))
|
||||
mux.HandleFunc("GET /api/captures", s.handleCaptures)
|
||||
mux.HandleFunc("GET /api/schedules", instanceOnly(s.handleSchedules))
|
||||
mux.HandleFunc("PUT /api/schedules/{id}", instanceOnly(s.handleSchedulePut))
|
||||
mux.HandleFunc("DELETE /api/schedules/{id}", instanceOnly(s.handleScheduleDelete))
|
||||
mux.HandleFunc("GET /api/schedules/{id}/runs", instanceOnly(s.handleScheduleRuns))
|
||||
mux.HandleFunc("POST /api/schedules/{id}/runs", instanceOnly(s.handleScheduleRunNow))
|
||||
mux.HandleFunc("GET /api/channels", instanceOnly(s.handleChannels))
|
||||
mux.HandleFunc("POST /api/channels/{id}/reload", instanceOnly(s.handleChannelReload))
|
||||
mux.HandleFunc("GET /api/version", s.handleVersion)
|
||||
|
||||
Reference in new issue
Block a user