code.gitea.io/gitea@v1.21.7/services/cron/tasks_basic.go (about)

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