2022-03-26 05:44:04 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-04-13 05:28:28 +00:00
|
|
|
"encoding/json"
|
2022-03-26 05:44:04 +00:00
|
|
|
"fmt"
|
2022-04-13 05:28:28 +00:00
|
|
|
"net/http"
|
2022-03-26 05:44:04 +00:00
|
|
|
"os"
|
|
|
|
"os/signal"
|
2022-04-13 05:28:28 +00:00
|
|
|
"sync"
|
2022-03-26 05:44:04 +00:00
|
|
|
"syscall"
|
|
|
|
|
2022-04-13 20:44:48 +00:00
|
|
|
"github.com/prometheus/client_golang/prometheus/promhttp"
|
2022-03-26 05:44:04 +00:00
|
|
|
"github.com/robfig/cron/v3"
|
|
|
|
)
|
|
|
|
|
2024-01-06 23:10:29 +00:00
|
|
|
var (
|
|
|
|
jobResultsLock = sync.Mutex{}
|
|
|
|
jobResults = map[string]JobResult{}
|
|
|
|
)
|
2022-04-13 05:28:28 +00:00
|
|
|
|
|
|
|
type JobResult struct {
|
|
|
|
JobName string
|
|
|
|
JobType string
|
|
|
|
Success bool
|
|
|
|
LastError error
|
|
|
|
Message string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r JobResult) Format() string {
|
|
|
|
return fmt.Sprintf("%s %s ok? %v\n\n%+v", r.JobName, r.JobType, r.Success, r.LastError)
|
|
|
|
}
|
|
|
|
|
|
|
|
func JobComplete(result JobResult) {
|
|
|
|
fmt.Printf("Completed job %+v\n", result)
|
|
|
|
|
|
|
|
jobResultsLock.Lock()
|
|
|
|
jobResults[result.JobName] = result
|
|
|
|
jobResultsLock.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
func writeJobResult(writer http.ResponseWriter, jobName string) {
|
|
|
|
if jobResult, ok := jobResults[jobName]; ok {
|
|
|
|
if !jobResult.Success {
|
|
|
|
writer.WriteHeader(http.StatusServiceUnavailable)
|
|
|
|
}
|
|
|
|
|
|
|
|
jobResult.Message = jobResult.LastError.Error()
|
|
|
|
if err := json.NewEncoder(writer).Encode(jobResult); err != nil {
|
|
|
|
_, _ = writer.Write([]byte(fmt.Sprintf("failed writing json for %s", jobResult.Format())))
|
|
|
|
}
|
|
|
|
|
|
|
|
writer.Header().Set("Content-Type", "application/json")
|
|
|
|
} else {
|
|
|
|
writer.WriteHeader(http.StatusNotFound)
|
|
|
|
_, _ = writer.Write([]byte("{\"Message\": \"Unknown job\"}"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func healthHandleFunc(writer http.ResponseWriter, request *http.Request) {
|
|
|
|
query := request.URL.Query()
|
|
|
|
if jobName, ok := query["job"]; ok {
|
|
|
|
writeJobResult(writer, jobName[0])
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
_, _ = writer.Write([]byte("ok"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func RunHTTPHandlers(addr string) error {
|
|
|
|
http.HandleFunc("/health", healthHandleFunc)
|
2023-05-09 21:16:39 +00:00
|
|
|
http.Handle("/metrics", promhttp.HandlerFor(
|
|
|
|
Metrics.Registry,
|
|
|
|
promhttp.HandlerOpts{Registry: Metrics.Registry}, //nolint:exhaustruct
|
|
|
|
))
|
2022-04-13 05:28:28 +00:00
|
|
|
|
2023-05-09 21:16:08 +00:00
|
|
|
return fmt.Errorf("error on http server: %w", http.ListenAndServe(addr, nil)) //#nosec: g114
|
2022-04-13 05:28:28 +00:00
|
|
|
}
|
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
func ScheduleAndRunJobs(jobs []Job) error {
|
|
|
|
signalChan := make(chan os.Signal, 1)
|
|
|
|
|
2022-04-13 05:28:28 +00:00
|
|
|
signal.Notify(
|
|
|
|
signalChan,
|
2022-03-26 05:44:04 +00:00
|
|
|
syscall.SIGINT,
|
|
|
|
syscall.SIGTERM,
|
|
|
|
syscall.SIGQUIT,
|
|
|
|
)
|
|
|
|
|
2022-04-13 05:28:28 +00:00
|
|
|
scheduler := cron.New()
|
2022-03-26 05:44:04 +00:00
|
|
|
|
|
|
|
for _, job := range jobs {
|
|
|
|
fmt.Println("Scheduling", job.Name)
|
|
|
|
|
2022-04-13 05:28:28 +00:00
|
|
|
if _, err := scheduler.AddJob(job.Schedule, job); err != nil {
|
|
|
|
return fmt.Errorf("error scheduling job %s: %w", job.Name, err)
|
2022-03-26 05:44:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-13 05:28:28 +00:00
|
|
|
scheduler.Start()
|
2022-03-26 05:44:04 +00:00
|
|
|
|
|
|
|
switch <-signalChan {
|
|
|
|
case syscall.SIGINT:
|
|
|
|
fmt.Println("Stopping now...")
|
|
|
|
|
2022-04-13 05:28:28 +00:00
|
|
|
defer scheduler.Stop()
|
2022-03-26 05:44:04 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
case syscall.SIGTERM:
|
|
|
|
fallthrough
|
|
|
|
case syscall.SIGQUIT:
|
|
|
|
// Wait for all jobs to complete
|
|
|
|
fmt.Println("Stopping after running jobs complete...")
|
|
|
|
|
|
|
|
defer func() {
|
2022-04-13 05:28:28 +00:00
|
|
|
ctx := scheduler.Stop()
|
2022-03-26 05:44:04 +00:00
|
|
|
<-ctx.Done()
|
|
|
|
|
2024-01-22 16:50:49 +00:00
|
|
|
fmt.Println("All jobs successfully stopped")
|
|
|
|
}()
|
2024-01-22 16:49:46 +00:00
|
|
|
|
2022-03-26 05:44:04 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|