gitea源码

tasks_basic.go 5.3KB

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