helper.go 883 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. package scheduler
  2. import (
  3. "encoding/json"
  4. "fmt"
  5. "os"
  6. )
  7. // Load a list of jobs from file
  8. func loadJobsFromFile(cronfile string) ([]*Job, error) {
  9. //Try to read the cronfile
  10. filecontent, err := os.ReadFile(cronfile)
  11. if err != nil {
  12. return []*Job{}, err
  13. }
  14. //Phrase the cronfile
  15. prevousJobs := []*Job{}
  16. err = json.Unmarshal(filecontent, &prevousJobs)
  17. if err != nil {
  18. return []*Job{}, err
  19. }
  20. return prevousJobs, nil
  21. }
  22. // save the changes in job list to file
  23. func (a *Scheduler) saveJobsToCronFile() error {
  24. js, err := json.Marshal(a.jobs)
  25. if err != nil {
  26. return err
  27. }
  28. return os.WriteFile(a.options.CronFile, js, 0775)
  29. }
  30. func (a *Scheduler) cronlog(message string) {
  31. a.options.Logger.PrintAndLog("Scheduler", fmt.Sprint(message), nil)
  32. }
  33. func (a *Scheduler) cronlogError(message string, err error) {
  34. a.options.Logger.PrintAndLog("Scheduler", message, err)
  35. }