本站源代码
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.

138 lines
4.4KB

  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2019 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package cron
  6. import (
  7. "time"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/log"
  10. "code.gitea.io/gitea/modules/migrations"
  11. "code.gitea.io/gitea/modules/setting"
  12. "code.gitea.io/gitea/modules/sync"
  13. mirror_service "code.gitea.io/gitea/services/mirror"
  14. "github.com/gogs/cron"
  15. )
  16. const (
  17. mirrorUpdate = "mirror_update"
  18. gitFsck = "git_fsck"
  19. checkRepos = "check_repos"
  20. archiveCleanup = "archive_cleanup"
  21. syncExternalUsers = "sync_external_users"
  22. deletedBranchesCleanup = "deleted_branches_cleanup"
  23. updateMigrationPosterID = "update_migration_post_id"
  24. )
  25. var c = cron.New()
  26. // Prevent duplicate running tasks.
  27. var taskStatusTable = sync.NewStatusTable()
  28. // Func defines a cron function body
  29. type Func func()
  30. // WithUnique wrap a cron func with an unique running check
  31. func WithUnique(name string, body Func) Func {
  32. return func() {
  33. if !taskStatusTable.StartIfNotRunning(name) {
  34. return
  35. }
  36. defer taskStatusTable.Stop(name)
  37. body()
  38. }
  39. }
  40. // NewContext begins cron tasks
  41. func NewContext() {
  42. var (
  43. entry *cron.Entry
  44. err error
  45. )
  46. if setting.Cron.UpdateMirror.Enabled {
  47. entry, err = c.AddFunc("Update mirrors", setting.Cron.UpdateMirror.Schedule, WithUnique(mirrorUpdate, mirror_service.Update))
  48. if err != nil {
  49. log.Fatal("Cron[Update mirrors]: %v", err)
  50. }
  51. if setting.Cron.UpdateMirror.RunAtStart {
  52. entry.Prev = time.Now()
  53. entry.ExecTimes++
  54. go WithUnique(mirrorUpdate, mirror_service.Update)()
  55. }
  56. }
  57. if setting.Cron.RepoHealthCheck.Enabled {
  58. entry, err = c.AddFunc("Repository health check", setting.Cron.RepoHealthCheck.Schedule, WithUnique(gitFsck, models.GitFsck))
  59. if err != nil {
  60. log.Fatal("Cron[Repository health check]: %v", err)
  61. }
  62. if setting.Cron.RepoHealthCheck.RunAtStart {
  63. entry.Prev = time.Now()
  64. entry.ExecTimes++
  65. go WithUnique(gitFsck, models.GitFsck)()
  66. }
  67. }
  68. if setting.Cron.CheckRepoStats.Enabled {
  69. entry, err = c.AddFunc("Check repository statistics", setting.Cron.CheckRepoStats.Schedule, WithUnique(checkRepos, models.CheckRepoStats))
  70. if err != nil {
  71. log.Fatal("Cron[Check repository statistics]: %v", err)
  72. }
  73. if setting.Cron.CheckRepoStats.RunAtStart {
  74. entry.Prev = time.Now()
  75. entry.ExecTimes++
  76. go WithUnique(checkRepos, models.CheckRepoStats)()
  77. }
  78. }
  79. if setting.Cron.ArchiveCleanup.Enabled {
  80. entry, err = c.AddFunc("Clean up old repository archives", setting.Cron.ArchiveCleanup.Schedule, WithUnique(archiveCleanup, models.DeleteOldRepositoryArchives))
  81. if err != nil {
  82. log.Fatal("Cron[Clean up old repository archives]: %v", err)
  83. }
  84. if setting.Cron.ArchiveCleanup.RunAtStart {
  85. entry.Prev = time.Now()
  86. entry.ExecTimes++
  87. go WithUnique(archiveCleanup, models.DeleteOldRepositoryArchives)()
  88. }
  89. }
  90. if setting.Cron.SyncExternalUsers.Enabled {
  91. entry, err = c.AddFunc("Synchronize external users", setting.Cron.SyncExternalUsers.Schedule, WithUnique(syncExternalUsers, models.SyncExternalUsers))
  92. if err != nil {
  93. log.Fatal("Cron[Synchronize external users]: %v", err)
  94. }
  95. if setting.Cron.SyncExternalUsers.RunAtStart {
  96. entry.Prev = time.Now()
  97. entry.ExecTimes++
  98. go WithUnique(syncExternalUsers, models.SyncExternalUsers)()
  99. }
  100. }
  101. if setting.Cron.DeletedBranchesCleanup.Enabled {
  102. entry, err = c.AddFunc("Remove old deleted branches", setting.Cron.DeletedBranchesCleanup.Schedule, WithUnique(deletedBranchesCleanup, models.RemoveOldDeletedBranches))
  103. if err != nil {
  104. log.Fatal("Cron[Remove old deleted branches]: %v", err)
  105. }
  106. if setting.Cron.DeletedBranchesCleanup.RunAtStart {
  107. entry.Prev = time.Now()
  108. entry.ExecTimes++
  109. go WithUnique(deletedBranchesCleanup, models.RemoveOldDeletedBranches)()
  110. }
  111. }
  112. entry, err = c.AddFunc("Update migrated repositories' issues and comments' posterid", setting.Cron.UpdateMigrationPosterID.Schedule, WithUnique(updateMigrationPosterID, migrations.UpdateMigrationPosterID))
  113. if err != nil {
  114. log.Fatal("Cron[Update migrated repositories]: %v", err)
  115. }
  116. entry.Prev = time.Now()
  117. entry.ExecTimes++
  118. go WithUnique(updateMigrationPosterID, migrations.UpdateMigrationPosterID)()
  119. c.Start()
  120. }
  121. // ListTasks returns all running cron tasks.
  122. func ListTasks() []*cron.Entry {
  123. return c.Entries()
  124. }
上海开阖软件有限公司 沪ICP备12045867号-1