1
0
mirror of https://github.com/nxshock/gron.git synced 2024-11-27 03:41:00 +05:00

Add reload jobs option

This commit is contained in:
nxshock 2022-03-26 20:20:00 +05:00
parent de98867c9c
commit 6c38e60b47
3 changed files with 50 additions and 11 deletions

View File

@ -12,6 +12,7 @@ import (
func httpServer(listenAddress string) { func httpServer(listenAddress string) {
http.HandleFunc("/", handler) http.HandleFunc("/", handler)
http.HandleFunc("/reloadJobs", handleReloadJobs)
http.HandleFunc("/shutdown", handleShutdown) http.HandleFunc("/shutdown", handleShutdown)
http.HandleFunc("/start", handleForceStart) http.HandleFunc("/start", handleForceStart)
log.WithField("job", "http_server").Fatal(http.ListenAndServe(listenAddress, nil)) log.WithField("job", "http_server").Fatal(http.ListenAndServe(listenAddress, nil))
@ -23,7 +24,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
return return
} }
currentRunningJobsMutex.RLock() globalMutex.RLock()
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
jobEntries := c.Entries() jobEntries := c.Entries()
var jobs []*Job var jobs []*Job
@ -31,7 +32,7 @@ func handler(w http.ResponseWriter, r *http.Request) {
jobs = append(jobs, v.Job.(*Job)) jobs = append(jobs, v.Job.(*Job))
} }
indexTemplate.ExecuteTemplate(buf, "index", jobs) indexTemplate.ExecuteTemplate(buf, "index", jobs)
currentRunningJobsMutex.RUnlock() globalMutex.RUnlock()
buf.WriteTo(w) buf.WriteTo(w)
} }
@ -69,3 +70,24 @@ func handleShutdown(w http.ResponseWriter, r *http.Request) {
os.Exit(0) 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)
}

View File

@ -150,6 +150,7 @@
padding: 0.5em; padding: 0.5em;
text-decoration: none; text-decoration: none;
display: block; display: block;
white-space: nowrap;
} }
.dropdown:hover .dropdown-content { .dropdown:hover .dropdown-content {
@ -163,7 +164,11 @@
<div class="dropdown"> <div class="dropdown">
<button class="dropbtn">☰ Menu</button> <button class="dropbtn">☰ Menu</button>
<div class="dropdown-content"> <div class="dropdown-content">
<a href="/shutdown">Shutdown</a> <a href="/reloadJobs">&#x27f3; Reload jobs</a>
<a>
<hr>
</a>
<a href="/shutdown">&#x23fc; Shutdown</a>
</div> </div>
</div> </div>
<h1>Job list</h1> <h1>Job list</h1>

28
main.go
View File

@ -33,11 +33,10 @@ func init() {
c = cron.New() c = cron.New()
} }
func main() { func initJobs() error {
log := log.WithField("job", "core") 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 { err := filepath.Walk("jobs.d", func(path string, info os.FileInfo, err error) error {
if err != nil { if err != nil {
return err return err
@ -61,14 +60,27 @@ func main() {
return nil return nil
}) })
if err != nil { if err != nil {
log.Fatalln(err) return err
} }
if len(c.Entries()) == 0 { 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() c.Start()
@ -76,10 +88,10 @@ func main() {
signal.Notify(intChan, syscall.SIGTERM) signal.Notify(intChan, syscall.SIGTERM)
<-intChan <-intChan
log.Info("got stop signal") log.Info("Got stop signal.")
err = logFile.Close() err = logFile.Close()
if err != nil { if err != nil {
log.Fatal(err.Error()) log.Fatal(err)
} }
} }