cron.go 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. log "gopkg.in/clog.v1"
  8. "github.com/gogits/cron"
  9. "github.com/gogits/gogs/models"
  10. "github.com/gogits/gogs/pkg/setting"
  11. )
  12. var c = cron.New()
  13. func NewContext() {
  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(2, "Cron.(update mirrors): %v", err)
  22. }
  23. if setting.Cron.UpdateMirror.RunAtStart {
  24. entry.Prev = time.Now()
  25. entry.ExecTimes++
  26. go models.MirrorUpdate()
  27. }
  28. }
  29. if setting.Cron.RepoHealthCheck.Enabled {
  30. entry, err = c.AddFunc("Repository health check", setting.Cron.RepoHealthCheck.Schedule, models.GitFsck)
  31. if err != nil {
  32. log.Fatal(2, "Cron.(repository health check): %v", err)
  33. }
  34. if setting.Cron.RepoHealthCheck.RunAtStart {
  35. entry.Prev = time.Now()
  36. entry.ExecTimes++
  37. go models.GitFsck()
  38. }
  39. }
  40. if setting.Cron.CheckRepoStats.Enabled {
  41. entry, err = c.AddFunc("Check repository statistics", setting.Cron.CheckRepoStats.Schedule, models.CheckRepoStats)
  42. if err != nil {
  43. log.Fatal(2, "Cron.(check repository statistics): %v", err)
  44. }
  45. if setting.Cron.CheckRepoStats.RunAtStart {
  46. entry.Prev = time.Now()
  47. entry.ExecTimes++
  48. go models.CheckRepoStats()
  49. }
  50. }
  51. if setting.Cron.RepoArchiveCleanup.Enabled {
  52. entry, err = c.AddFunc("Repository archive cleanup", setting.Cron.RepoArchiveCleanup.Schedule, models.DeleteOldRepositoryArchives)
  53. if err != nil {
  54. log.Fatal(2, "Cron.(repository archive cleanup): %v", err)
  55. }
  56. if setting.Cron.RepoArchiveCleanup.RunAtStart {
  57. entry.Prev = time.Now()
  58. entry.ExecTimes++
  59. go models.DeleteOldRepositoryArchives()
  60. }
  61. }
  62. c.Start()
  63. }
  64. // ListTasks returns all running cron tasks.
  65. func ListTasks() []*cron.Entry {
  66. return c.Entries()
  67. }