helper.go 918 B

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