cron.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package cron
  5. import (
  6. "time"
  7. "github.com/gogits/gogs/models"
  8. "github.com/gogits/gogs/modules/cron"
  9. "github.com/gogits/gogs/modules/log"
  10. "github.com/gogits/gogs/modules/setting"
  11. )
  12. var c = cron.New()
  13. func NewCronContext() {
  14. var (
  15. entry *cron.Entry
  16. err error
  17. )
  18. if setting.Cron.UpdateMirror.Enabled {
  19. entry, err = c.AddFunc("Update mirrors", setting.Cron.UpdateMirror.Schedule, models.MirrorUpdate)
  20. if err != nil {
  21. log.Fatal(4, "Cron[Update mirrors]: %v", err)
  22. }
  23. if setting.Cron.UpdateMirror.RunAtStart {
  24. entry.Prev = time.Now()
  25. go models.MirrorUpdate()
  26. }
  27. }
  28. if setting.Cron.RepoHealthCheck.Enabled {
  29. entry, err = c.AddFunc("Repository health check", setting.Cron.RepoHealthCheck.Schedule, models.GitFsck)
  30. if err != nil {
  31. log.Fatal(4, "Cron[Repository health check]: %v", err)
  32. }
  33. if setting.Cron.RepoHealthCheck.RunAtStart {
  34. entry.Prev = time.Now()
  35. go models.GitFsck()
  36. }
  37. }
  38. if setting.Cron.CheckRepoStats.Enabled {
  39. entry, err = c.AddFunc("Check repository statistics", setting.Cron.CheckRepoStats.Schedule, models.CheckRepoStats)
  40. if err != nil {
  41. log.Fatal(4, "Cron[Check repository statistics]: %v", err)
  42. }
  43. if setting.Cron.CheckRepoStats.RunAtStart {
  44. entry.Prev = time.Now()
  45. go models.CheckRepoStats()
  46. }
  47. }
  48. c.Start()
  49. }
  50. // ListTasks returns all running cron tasks.
  51. func ListTasks() []*cron.Entry {
  52. return c.Entries()
  53. }