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 3.2KB

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