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.

118 lines
3.2 KiB

  1. // Copyright 2020 The Gitea 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. "context"
  7. "time"
  8. "code.gitea.io/gitea/models"
  9. "code.gitea.io/gitea/modules/migrations"
  10. repository_service "code.gitea.io/gitea/modules/repository"
  11. mirror_service "code.gitea.io/gitea/services/mirror"
  12. )
  13. func registerUpdateMirrorTask() {
  14. RegisterTaskFatal("update_mirrors", &BaseConfig{
  15. Enabled: true,
  16. RunAtStart: false,
  17. Schedule: "@every 10m",
  18. }, func(ctx context.Context, _ *models.User, _ Config) error {
  19. return mirror_service.Update(ctx)
  20. })
  21. }
  22. func registerRepoHealthCheck() {
  23. type RepoHealthCheckConfig struct {
  24. BaseConfig
  25. Timeout time.Duration
  26. Args []string `delim:" "`
  27. }
  28. RegisterTaskFatal("repo_health_check", &RepoHealthCheckConfig{
  29. BaseConfig: BaseConfig{
  30. Enabled: true,
  31. RunAtStart: false,
  32. Schedule: "@every 24h",
  33. },
  34. Timeout: 60 * time.Second,
  35. Args: []string{},
  36. }, func(ctx context.Context, _ *models.User, config Config) error {
  37. rhcConfig := config.(*RepoHealthCheckConfig)
  38. return repository_service.GitFsck(ctx, rhcConfig.Timeout, rhcConfig.Args)
  39. })
  40. }
  41. func registerCheckRepoStats() {
  42. RegisterTaskFatal("check_repo_stats", &BaseConfig{
  43. Enabled: true,
  44. RunAtStart: true,
  45. Schedule: "@every 24h",
  46. }, func(ctx context.Context, _ *models.User, _ Config) error {
  47. return models.CheckRepoStats(ctx)
  48. })
  49. }
  50. func registerArchiveCleanup() {
  51. RegisterTaskFatal("archive_cleanup", &OlderThanConfig{
  52. BaseConfig: BaseConfig{
  53. Enabled: true,
  54. RunAtStart: true,
  55. Schedule: "@every 24h",
  56. },
  57. OlderThan: 24 * time.Hour,
  58. }, func(ctx context.Context, _ *models.User, config Config) error {
  59. acConfig := config.(*OlderThanConfig)
  60. return models.DeleteOldRepositoryArchives(ctx, acConfig.OlderThan)
  61. })
  62. }
  63. func registerSyncExternalUsers() {
  64. RegisterTaskFatal("sync_external_users", &UpdateExistingConfig{
  65. BaseConfig: BaseConfig{
  66. Enabled: true,
  67. RunAtStart: false,
  68. Schedule: "@every 24h",
  69. },
  70. UpdateExisting: true,
  71. }, func(ctx context.Context, _ *models.User, config Config) error {
  72. realConfig := config.(*UpdateExistingConfig)
  73. return models.SyncExternalUsers(ctx, realConfig.UpdateExisting)
  74. })
  75. }
  76. func registerDeletedBranchesCleanup() {
  77. RegisterTaskFatal("deleted_branches_cleanup", &OlderThanConfig{
  78. BaseConfig: BaseConfig{
  79. Enabled: true,
  80. RunAtStart: true,
  81. Schedule: "@every 24h",
  82. },
  83. OlderThan: 24 * time.Hour,
  84. }, func(ctx context.Context, _ *models.User, config Config) error {
  85. realConfig := config.(*OlderThanConfig)
  86. models.RemoveOldDeletedBranches(ctx, realConfig.OlderThan)
  87. return nil
  88. })
  89. }
  90. func registerUpdateMigrationPosterID() {
  91. RegisterTaskFatal("update_migration_poster_id", &BaseConfig{
  92. Enabled: true,
  93. RunAtStart: true,
  94. Schedule: "@every 24h",
  95. }, func(ctx context.Context, _ *models.User, _ Config) error {
  96. return migrations.UpdateMigrationPosterID(ctx)
  97. })
  98. }
  99. func initBasicTasks() {
  100. registerUpdateMirrorTask()
  101. registerRepoHealthCheck()
  102. registerCheckRepoStats()
  103. registerArchiveCleanup()
  104. registerSyncExternalUsers()
  105. registerDeletedBranchesCleanup()
  106. registerUpdateMigrationPosterID()
  107. }