From 6c38e60b47752151304791780372901613dd85e2 Mon Sep 17 00:00:00 2001 From: nxshock Date: Sat, 26 Mar 2022 20:20:00 +0500 Subject: [PATCH] Add reload jobs option --- httpserver.go | 26 ++++++++++++++++++++++++-- index.htm | 7 ++++++- main.go | 28 ++++++++++++++++++++-------- 3 files changed, 50 insertions(+), 11 deletions(-) diff --git a/httpserver.go b/httpserver.go index 2aef4ec..5651d19 100644 --- a/httpserver.go +++ b/httpserver.go @@ -12,6 +12,7 @@ import ( func httpServer(listenAddress string) { http.HandleFunc("/", handler) + http.HandleFunc("/reloadJobs", handleReloadJobs) http.HandleFunc("/shutdown", handleShutdown) http.HandleFunc("/start", handleForceStart) log.WithField("job", "http_server").Fatal(http.ListenAndServe(listenAddress, nil)) @@ -23,7 +24,7 @@ func handler(w http.ResponseWriter, r *http.Request) { return } - currentRunningJobsMutex.RLock() + globalMutex.RLock() buf := new(bytes.Buffer) jobEntries := c.Entries() var jobs []*Job @@ -31,7 +32,7 @@ func handler(w http.ResponseWriter, r *http.Request) { jobs = append(jobs, v.Job.(*Job)) } indexTemplate.ExecuteTemplate(buf, "index", jobs) - currentRunningJobsMutex.RUnlock() + globalMutex.RUnlock() buf.WriteTo(w) } @@ -69,3 +70,24 @@ func handleShutdown(w http.ResponseWriter, r *http.Request) { os.Exit(0) }() } + +func handleReloadJobs(w http.ResponseWriter, r *http.Request) { + globalMutex.Lock() + defer globalMutex.Unlock() + + c.Stop() + + for _, entry := range c.Entries() { + c.Remove(entry.ID) + } + + err := initJobs() + if err != nil { + http.Error(w, fmt.Sprintf("reload jobs error: %v", err), http.StatusInternalServerError) + return + } + + c.Start() + + http.Redirect(w, r, "/", http.StatusTemporaryRedirect) +} diff --git a/index.htm b/index.htm index cb2cb83..4a3fbcf 100644 --- a/index.htm +++ b/index.htm @@ -150,6 +150,7 @@ padding: 0.5em; text-decoration: none; display: block; + white-space: nowrap; } .dropdown:hover .dropdown-content { @@ -163,7 +164,11 @@

Job list

diff --git a/main.go b/main.go index d620da6..575cdf3 100644 --- a/main.go +++ b/main.go @@ -33,11 +33,10 @@ func init() { c = cron.New() } -func main() { +func initJobs() error { log := log.WithField("job", "core") - log.Info("started") - + log.Infoln("Reading jobs...") err := filepath.Walk("jobs.d", func(path string, info os.FileInfo, err error) error { if err != nil { return err @@ -61,14 +60,27 @@ func main() { return nil }) if err != nil { - log.Fatalln(err) + return err } if len(c.Entries()) == 0 { - log.Fatal("no jobs loaded") + log.Warn("No jobs loaded.") + } else { + log.Infof("Loaded jobs count: %d", len(c.Entries())) } - log.Infof("loaded jobs count: %d", len(c.Entries())) + return nil +} + +func main() { + log := log.WithField("job", "core") + + log.Info("Started.") + + err := initJobs() + if err != nil { + log.Fatalln(err) + } c.Start() @@ -76,10 +88,10 @@ func main() { signal.Notify(intChan, syscall.SIGTERM) <-intChan - log.Info("got stop signal") + log.Info("Got stop signal.") err = logFile.Close() if err != nil { - log.Fatal(err.Error()) + log.Fatal(err) } }