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.

tasks_basic.go 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. // Copyright 2020 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package cron
  4. import (
  5. "context"
  6. "time"
  7. "code.gitea.io/gitea/models"
  8. git_model "code.gitea.io/gitea/models/git"
  9. user_model "code.gitea.io/gitea/models/user"
  10. "code.gitea.io/gitea/models/webhook"
  11. "code.gitea.io/gitea/modules/git"
  12. "code.gitea.io/gitea/modules/setting"
  13. "code.gitea.io/gitea/services/actions"
  14. "code.gitea.io/gitea/services/auth"
  15. "code.gitea.io/gitea/services/migrations"
  16. mirror_service "code.gitea.io/gitea/services/mirror"
  17. packages_cleanup_service "code.gitea.io/gitea/services/packages/cleanup"
  18. repo_service "code.gitea.io/gitea/services/repository"
  19. archiver_service "code.gitea.io/gitea/services/repository/archiver"
  20. )
  21. func registerUpdateMirrorTask() {
  22. type UpdateMirrorTaskConfig struct {
  23. BaseConfig
  24. PullLimit int
  25. PushLimit int
  26. }
  27. RegisterTaskFatal("update_mirrors", &UpdateMirrorTaskConfig{
  28. BaseConfig: BaseConfig{
  29. Enabled: true,
  30. RunAtStart: false,
  31. Schedule: "@every 10m",
  32. },
  33. PullLimit: 50,
  34. PushLimit: 50,
  35. }, func(ctx context.Context, _ *user_model.User, cfg Config) error {
  36. umtc := cfg.(*UpdateMirrorTaskConfig)
  37. return mirror_service.Update(ctx, umtc.PullLimit, umtc.PushLimit)
  38. })
  39. }
  40. func registerRepoHealthCheck() {
  41. type RepoHealthCheckConfig struct {
  42. BaseConfig
  43. Timeout time.Duration
  44. Args []string `delim:" "`
  45. }
  46. RegisterTaskFatal("repo_health_check", &RepoHealthCheckConfig{
  47. BaseConfig: BaseConfig{
  48. Enabled: true,
  49. RunAtStart: false,
  50. Schedule: "@midnight",
  51. },
  52. Timeout: 60 * time.Second,
  53. Args: []string{},
  54. }, func(ctx context.Context, _ *user_model.User, config Config) error {
  55. rhcConfig := config.(*RepoHealthCheckConfig)
  56. // the git args are set by config, they can be safe to be trusted
  57. return repo_service.GitFsckRepos(ctx, rhcConfig.Timeout, git.ToTrustedCmdArgs(rhcConfig.Args))
  58. })
  59. }
  60. func registerCheckRepoStats() {
  61. RegisterTaskFatal("check_repo_stats", &BaseConfig{
  62. Enabled: true,
  63. RunAtStart: true,
  64. Schedule: "@midnight",
  65. }, func(ctx context.Context, _ *user_model.User, _ Config) error {
  66. return models.CheckRepoStats(ctx)
  67. })
  68. }
  69. func registerArchiveCleanup() {
  70. RegisterTaskFatal("archive_cleanup", &OlderThanConfig{
  71. BaseConfig: BaseConfig{
  72. Enabled: true,
  73. RunAtStart: true,
  74. Schedule: "@midnight",
  75. },
  76. OlderThan: 24 * time.Hour,
  77. }, func(ctx context.Context, _ *user_model.User, config Config) error {
  78. acConfig := config.(*OlderThanConfig)
  79. return archiver_service.DeleteOldRepositoryArchives(ctx, acConfig.OlderThan)
  80. })
  81. }
  82. func registerSyncExternalUsers() {
  83. RegisterTaskFatal("sync_external_users", &UpdateExistingConfig{
  84. BaseConfig: BaseConfig{
  85. Enabled: true,
  86. RunAtStart: false,
  87. Schedule: "@midnight",
  88. },
  89. UpdateExisting: true,
  90. }, func(ctx context.Context, _ *user_model.User, config Config) error {
  91. realConfig := config.(*UpdateExistingConfig)
  92. return auth.SyncExternalUsers(ctx, realConfig.UpdateExisting)
  93. })
  94. }
  95. func registerDeletedBranchesCleanup() {
  96. RegisterTaskFatal("deleted_branches_cleanup", &OlderThanConfig{
  97. BaseConfig: BaseConfig{
  98. Enabled: true,
  99. RunAtStart: true,
  100. Schedule: "@midnight",
  101. },
  102. OlderThan: 24 * time.Hour,
  103. }, func(ctx context.Context, _ *user_model.User, config Config) error {
  104. realConfig := config.(*OlderThanConfig)
  105. git_model.RemoveOldDeletedBranches(ctx, realConfig.OlderThan)
  106. return nil
  107. })
  108. }
  109. func registerUpdateMigrationPosterID() {
  110. RegisterTaskFatal("update_migration_poster_id", &BaseConfig{
  111. Enabled: true,
  112. RunAtStart: true,
  113. Schedule: "@midnight",
  114. }, func(ctx context.Context, _ *user_model.User, _ Config) error {
  115. return migrations.UpdateMigrationPosterID(ctx)
  116. })
  117. }
  118. func registerCleanupHookTaskTable() {
  119. RegisterTaskFatal("cleanup_hook_task_table", &CleanupHookTaskConfig{
  120. BaseConfig: BaseConfig{
  121. Enabled: true,
  122. RunAtStart: false,
  123. Schedule: "@midnight",
  124. },
  125. CleanupType: "OlderThan",
  126. OlderThan: 168 * time.Hour,
  127. NumberToKeep: 10,
  128. }, func(ctx context.Context, _ *user_model.User, config Config) error {
  129. realConfig := config.(*CleanupHookTaskConfig)
  130. return webhook.CleanupHookTaskTable(ctx, webhook.ToHookTaskCleanupType(realConfig.CleanupType), realConfig.OlderThan, realConfig.NumberToKeep)
  131. })
  132. }
  133. func registerCleanupPackages() {
  134. RegisterTaskFatal("cleanup_packages", &OlderThanConfig{
  135. BaseConfig: BaseConfig{
  136. Enabled: true,
  137. RunAtStart: true,
  138. Schedule: "@midnight",
  139. },
  140. OlderThan: 24 * time.Hour,
  141. }, func(ctx context.Context, _ *user_model.User, config Config) error {
  142. realConfig := config.(*OlderThanConfig)
  143. return packages_cleanup_service.CleanupTask(ctx, realConfig.OlderThan)
  144. })
  145. }
  146. func registerActionsCleanup() {
  147. RegisterTaskFatal("cleanup_actions", &OlderThanConfig{
  148. BaseConfig: BaseConfig{
  149. Enabled: true,
  150. RunAtStart: true,
  151. Schedule: "@midnight",
  152. },
  153. OlderThan: 24 * time.Hour,
  154. }, func(ctx context.Context, _ *user_model.User, config Config) error {
  155. realConfig := config.(*OlderThanConfig)
  156. return actions.Cleanup(ctx, realConfig.OlderThan)
  157. })
  158. }
  159. func initBasicTasks() {
  160. if setting.Mirror.Enabled {
  161. registerUpdateMirrorTask()
  162. }
  163. registerRepoHealthCheck()
  164. registerCheckRepoStats()
  165. registerArchiveCleanup()
  166. registerSyncExternalUsers()
  167. registerDeletedBranchesCleanup()
  168. if !setting.Repository.DisableMigrations {
  169. registerUpdateMigrationPosterID()
  170. }
  171. registerCleanupHookTaskTable()
  172. if setting.Packages.Enabled {
  173. registerCleanupPackages()
  174. }
  175. if setting.Actions.Enabled {
  176. registerActionsCleanup()
  177. }
  178. }