add webui cron CRUD endpoints (create/read/update/delete)

This commit is contained in:
DBT
2026-02-25 15:49:34 +00:00
parent 44cb2c61c2
commit 1118c3e44b
3 changed files with 222 additions and 23 deletions

View File

@@ -128,6 +128,17 @@ type CronService struct {
runner *lifecycle.LoopRunner
}
type UpdateJobInput struct {
Name *string
Enabled *bool
Schedule *CronSchedule
Message *string
Deliver *bool
Channel *string
To *string
DeleteAfterRun *bool
}
func NewCronService(storePath string, onJob JobHandler) *CronService {
cs := &CronService{
storePath: storePath,
@@ -651,6 +662,73 @@ func (cs *CronService) EnableJob(jobID string, enabled bool) *CronJob {
return nil
}
func (cs *CronService) GetJob(jobID string) *CronJob {
cs.mu.RLock()
defer cs.mu.RUnlock()
for i := range cs.store.Jobs {
if cs.store.Jobs[i].ID == jobID {
job := cs.store.Jobs[i]
return &job
}
}
return nil
}
func (cs *CronService) UpdateJob(jobID string, in UpdateJobInput) (*CronJob, error) {
cs.mu.Lock()
defer cs.mu.Unlock()
idx := cs.findJobIndexByIDLocked(jobID)
if idx < 0 {
return nil, fmt.Errorf("job not found: %s", jobID)
}
job := &cs.store.Jobs[idx]
if in.Name != nil {
job.Name = *in.Name
}
if in.Schedule != nil {
job.Schedule = *in.Schedule
if job.Enabled {
now := time.Now().UnixMilli()
job.State.NextRunAtMS = cs.computeNextRun(&job.Schedule, now)
}
}
if in.Message != nil {
job.Payload.Message = *in.Message
}
if in.Deliver != nil {
job.Payload.Deliver = *in.Deliver
}
if in.Channel != nil {
job.Payload.Channel = *in.Channel
}
if in.To != nil {
job.Payload.To = *in.To
}
if in.DeleteAfterRun != nil {
job.DeleteAfterRun = *in.DeleteAfterRun
}
if in.Enabled != nil {
job.Enabled = *in.Enabled
if job.Enabled {
now := time.Now().UnixMilli()
job.State.NextRunAtMS = cs.computeNextRun(&job.Schedule, now)
} else {
job.State.NextRunAtMS = nil
}
}
job.UpdatedAtMS = time.Now().UnixMilli()
if err := cs.saveStore(); err != nil {
cs.lastSaveError = err.Error()
return nil, err
}
cs.lastSaveError = ""
ret := *job
return &ret, nil
}
func (cs *CronService) ListJobs(includeDisabled bool) []CronJob {
cs.mu.RLock()
defer cs.mu.RUnlock()

View File

@@ -22,7 +22,7 @@ type RegistryServer struct {
configPath string
onChat func(ctx context.Context, sessionKey, content string) (string, error)
onConfigAfter func()
onCron func(action, id string) (interface{}, error)
onCron func(action string, args map[string]interface{}) (interface{}, error)
webUIDir string
}
@@ -42,7 +42,7 @@ func (s *RegistryServer) SetChatHandler(fn func(ctx context.Context, sessionKey,
s.onChat = fn
}
func (s *RegistryServer) SetConfigAfterHook(fn func()) { s.onConfigAfter = fn }
func (s *RegistryServer) SetCronHandler(fn func(action, id string) (interface{}, error)) {
func (s *RegistryServer) SetCronHandler(fn func(action string, args map[string]interface{}) (interface{}, error)) {
s.onCron = fn
}
func (s *RegistryServer) SetWebUIDir(dir string) { s.webUIDir = strings.TrimSpace(dir) }
@@ -343,33 +343,53 @@ func (s *RegistryServer) handleWebUICron(w http.ResponseWriter, r *http.Request)
http.Error(w, "cron handler not configured", http.StatusInternalServerError)
return
}
if r.Method == http.MethodGet {
res, err := s.onCron("list", "")
switch r.Method {
case http.MethodGet:
id := strings.TrimSpace(r.URL.Query().Get("id"))
action := "list"
if id != "" {
action = "get"
}
res, err := s.onCron(action, map[string]interface{}{"id": id})
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_ = json.NewEncoder(w).Encode(map[string]interface{}{"ok": true, "jobs": res})
return
}
if r.Method == http.MethodPost {
var body struct {
Action string `json:"action"`
ID string `json:"id"`
if action == "list" {
_ = json.NewEncoder(w).Encode(map[string]interface{}{"ok": true, "jobs": res})
} else {
_ = json.NewEncoder(w).Encode(map[string]interface{}{"ok": true, "job": res})
}
if err := json.NewDecoder(r.Body).Decode(&body); err != nil {
http.Error(w, "invalid json", http.StatusBadRequest)
return
case http.MethodPost, http.MethodPut, http.MethodDelete:
args := map[string]interface{}{}
if r.Body != nil {
_ = json.NewDecoder(r.Body).Decode(&args)
}
res, err := s.onCron(strings.ToLower(strings.TrimSpace(body.Action)), strings.TrimSpace(body.ID))
if id := strings.TrimSpace(r.URL.Query().Get("id")); id != "" {
args["id"] = id
}
action := ""
switch r.Method {
case http.MethodPost:
action = "create"
if a, ok := args["action"].(string); ok && strings.TrimSpace(a) != "" {
action = strings.ToLower(strings.TrimSpace(a))
}
case http.MethodPut:
action = "update"
case http.MethodDelete:
action = "delete"
}
res, err := s.onCron(action, args)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
_ = json.NewEncoder(w).Encode(map[string]interface{}{"ok": true, "result": res})
return
default:
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
}
func (s *RegistryServer) checkAuth(r *http.Request) bool {