You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

97 lines
2.8 KiB

  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/cron"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/setting"
  11. )
  12. var c = cron.New()
  13. // NewContext begins cron tasks
  14. func NewContext() {
  15. var (
  16. entry *cron.Entry
  17. err error
  18. )
  19. if setting.Cron.UpdateMirror.Enabled {
  20. entry, err = c.AddFunc("Update mirrors", setting.Cron.UpdateMirror.Schedule, models.MirrorUpdate)
  21. if err != nil {
  22. log.Fatal(4, "Cron[Update mirrors]: %v", err)
  23. }
  24. if setting.Cron.UpdateMirror.RunAtStart {
  25. entry.Prev = time.Now()
  26. entry.ExecTimes++
  27. go models.MirrorUpdate()
  28. }
  29. }
  30. if setting.Cron.RepoHealthCheck.Enabled {
  31. entry, err = c.AddFunc("Repository health check", setting.Cron.RepoHealthCheck.Schedule, models.GitFsck)
  32. if err != nil {
  33. log.Fatal(4, "Cron[Repository health check]: %v", err)
  34. }
  35. if setting.Cron.RepoHealthCheck.RunAtStart {
  36. entry.Prev = time.Now()
  37. entry.ExecTimes++
  38. go models.GitFsck()
  39. }
  40. }
  41. if setting.Cron.CheckRepoStats.Enabled {
  42. entry, err = c.AddFunc("Check repository statistics", setting.Cron.CheckRepoStats.Schedule, models.CheckRepoStats)
  43. if err != nil {
  44. log.Fatal(4, "Cron[Check repository statistics]: %v", err)
  45. }
  46. if setting.Cron.CheckRepoStats.RunAtStart {
  47. entry.Prev = time.Now()
  48. entry.ExecTimes++
  49. go models.CheckRepoStats()
  50. }
  51. }
  52. if setting.Cron.ArchiveCleanup.Enabled {
  53. entry, err = c.AddFunc("Clean up old repository archives", setting.Cron.ArchiveCleanup.Schedule, models.DeleteOldRepositoryArchives)
  54. if err != nil {
  55. log.Fatal(4, "Cron[Clean up old repository archives]: %v", err)
  56. }
  57. if setting.Cron.ArchiveCleanup.RunAtStart {
  58. entry.Prev = time.Now()
  59. entry.ExecTimes++
  60. go models.DeleteOldRepositoryArchives()
  61. }
  62. }
  63. if setting.Cron.SyncExternalUsers.Enabled {
  64. entry, err = c.AddFunc("Synchronize external users", setting.Cron.SyncExternalUsers.Schedule, models.SyncExternalUsers)
  65. if err != nil {
  66. log.Fatal(4, "Cron[Synchronize external users]: %v", err)
  67. }
  68. if setting.Cron.SyncExternalUsers.RunAtStart {
  69. entry.Prev = time.Now()
  70. entry.ExecTimes++
  71. go models.SyncExternalUsers()
  72. }
  73. }
  74. if setting.Cron.DeletedBranchesCleanup.Enabled {
  75. entry, err = c.AddFunc("Remove old deleted branches", setting.Cron.DeletedBranchesCleanup.Schedule, models.RemoveOldDeletedBranches)
  76. if err != nil {
  77. log.Fatal(4, "Cron[Remove old deleted branches]: %v", err)
  78. }
  79. if setting.Cron.DeletedBranchesCleanup.RunAtStart {
  80. entry.Prev = time.Now()
  81. entry.ExecTimes++
  82. go models.RemoveOldDeletedBranches()
  83. }
  84. }
  85. c.Start()
  86. }
  87. // ListTasks returns all running cron tasks.
  88. func ListTasks() []*cron.Entry {
  89. return c.Entries()
  90. }