From 0ccdee8bd11f5032def8cfa48e7ead382b877133 Mon Sep 17 00:00:00 2001 From: bel Date: Sun, 15 Mar 2020 17:05:29 +0000 Subject: [PATCH] Add titles to config file and sort list by title --- scheduler/job.go | 2 +- scheduler/scheduler.go | 1 + server/list.go | 4 +++- testdata/5_jobs.cron | 10 +++++----- testdata/hostname_per_5m.cron | 2 +- testdata/hostname_per_second.cron | 2 +- 6 files changed, 12 insertions(+), 9 deletions(-) diff --git a/scheduler/job.go b/scheduler/job.go index 00e62f1..6359c1b 100755 --- a/scheduler/job.go +++ b/scheduler/job.go @@ -46,7 +46,7 @@ func newBashJob(schedule, sh string, title ...string) (*Job, error) { Raw: sh, Runner: Bash, } - if len(title) == 0 { + if len(title) == 0 || len(title[0]) == 0 { j.Title = j.Name } else { j.Title = title[0] diff --git a/scheduler/scheduler.go b/scheduler/scheduler.go index 3647523..883dd01 100755 --- a/scheduler/scheduler.go +++ b/scheduler/scheduler.go @@ -93,6 +93,7 @@ func splitScheduleCommandTitle(b []byte) (string, string, string) { if i := strings.LastIndex(commandTitle, "#"); i >= 0 { title = commandTitle[i+1:] } + title = strings.TrimSpace(title) return schedule, commandTitle, title } diff --git a/server/list.go b/server/list.go index ff270a2..e5b01ca 100755 --- a/server/list.go +++ b/server/list.go @@ -16,7 +16,6 @@ func (s *Server) list(w http.ResponseWriter, r *http.Request) { http.Error(w, err.Error(), http.StatusInternalServerError) return } - sort.Strings(jobs) out := make([]map[string]interface{}, len(jobs)) for i, job := range jobs { out[i] = make(map[string]interface{}) @@ -46,5 +45,8 @@ func (s *Server) list(w http.ResponseWriter, r *http.Request) { "status": j.LastStatus, } } + sort.Slice(out, func(i, j int) bool { + return out[i]["title"].(string) < out[j]["title"].(string) + }) json.NewEncoder(w).Encode(out) } diff --git a/testdata/5_jobs.cron b/testdata/5_jobs.cron index fa0dfb3..5e59d52 100755 --- a/testdata/5_jobs.cron +++ b/testdata/5_jobs.cron @@ -1,5 +1,5 @@ -0 */5 * * * * echo first job -0 */15 * * * * echo second job -0 */25 * * * * echo third job -0 */35 * * * * echo fourth job -0 */45 * * * * true +0 */5 * * * * echo first job #first job; every 5m +0 */15 * * * * echo second job # second job; every 15m +0 */25 * * * * echo third job #### third job; every 25m +0 */35 * * * * echo fourth job #4th job; ever 35m +0 */45 * * * * true diff --git a/testdata/hostname_per_5m.cron b/testdata/hostname_per_5m.cron index a9adfa2..1d2b5a3 100755 --- a/testdata/hostname_per_5m.cron +++ b/testdata/hostname_per_5m.cron @@ -1 +1 @@ -0 */5 * * * * hostname +0 */5 * * * * hostname #hostname every 5m diff --git a/testdata/hostname_per_second.cron b/testdata/hostname_per_second.cron index e1c619a..52f1206 100755 --- a/testdata/hostname_per_second.cron +++ b/testdata/hostname_per_second.cron @@ -1 +1 @@ -* * * * * * hostname +* * * * * * hostname #hostname per sec