2020-05-17 05:01:38 +05:30
|
|
|
// Copyright 2020 The Gitea Authors. All rights reserved.
|
2022-11-27 23:50:29 +05:30
|
|
|
// SPDX-License-Identifier: MIT
|
2020-05-17 05:01:38 +05:30
|
|
|
|
|
|
|
package cron
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
|
|
|
|
2022-08-25 08:01:57 +05:30
|
|
|
activities_model "code.gitea.io/gitea/models/activities"
|
2021-12-10 13:44:24 +05:30
|
|
|
asymkey_model "code.gitea.io/gitea/models/asymkey"
|
2022-05-20 19:38:52 +05:30
|
|
|
"code.gitea.io/gitea/models/db"
|
2022-10-17 04:59:26 +05:30
|
|
|
"code.gitea.io/gitea/models/system"
|
2021-11-24 15:19:20 +05:30
|
|
|
user_model "code.gitea.io/gitea/models/user"
|
2022-10-23 20:14:45 +05:30
|
|
|
"code.gitea.io/gitea/modules/git"
|
2020-05-17 05:01:38 +05:30
|
|
|
"code.gitea.io/gitea/modules/setting"
|
2021-10-21 21:40:49 +05:30
|
|
|
"code.gitea.io/gitea/modules/updatechecker"
|
2021-11-16 19:00:11 +05:30
|
|
|
repo_service "code.gitea.io/gitea/services/repository"
|
2021-12-06 12:49:28 +05:30
|
|
|
archiver_service "code.gitea.io/gitea/services/repository/archiver"
|
2021-11-18 23:12:27 +05:30
|
|
|
user_service "code.gitea.io/gitea/services/user"
|
2020-05-17 05:01:38 +05:30
|
|
|
)
|
|
|
|
|
|
|
|
func registerDeleteInactiveUsers() {
|
|
|
|
RegisterTaskFatal("delete_inactive_accounts", &OlderThanConfig{
|
|
|
|
BaseConfig: BaseConfig{
|
|
|
|
Enabled: false,
|
|
|
|
RunAtStart: false,
|
|
|
|
Schedule: "@annually",
|
|
|
|
},
|
2022-05-13 18:19:22 +05:30
|
|
|
OlderThan: time.Minute * time.Duration(setting.Service.ActiveCodeLives),
|
2021-11-24 15:19:20 +05:30
|
|
|
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
2020-05-17 05:01:38 +05:30
|
|
|
olderThanConfig := config.(*OlderThanConfig)
|
2021-11-18 23:12:27 +05:30
|
|
|
return user_service.DeleteInactiveUsers(ctx, olderThanConfig.OlderThan)
|
2020-05-17 05:01:38 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerDeleteRepositoryArchives() {
|
|
|
|
RegisterTaskFatal("delete_repo_archives", &BaseConfig{
|
|
|
|
Enabled: false,
|
|
|
|
RunAtStart: false,
|
|
|
|
Schedule: "@annually",
|
2021-11-24 15:19:20 +05:30
|
|
|
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
|
2021-12-06 12:49:28 +05:30
|
|
|
return archiver_service.DeleteRepositoryArchives(ctx)
|
2020-05-17 05:01:38 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerGarbageCollectRepositories() {
|
|
|
|
type RepoHealthCheckConfig struct {
|
|
|
|
BaseConfig
|
|
|
|
Timeout time.Duration
|
|
|
|
Args []string `delim:" "`
|
|
|
|
}
|
|
|
|
RegisterTaskFatal("git_gc_repos", &RepoHealthCheckConfig{
|
|
|
|
BaseConfig: BaseConfig{
|
|
|
|
Enabled: false,
|
|
|
|
RunAtStart: false,
|
|
|
|
Schedule: "@every 72h",
|
|
|
|
},
|
|
|
|
Timeout: time.Duration(setting.Git.Timeout.GC) * time.Second,
|
|
|
|
Args: setting.Git.GCArgs,
|
2021-11-24 15:19:20 +05:30
|
|
|
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
2020-05-17 05:01:38 +05:30
|
|
|
rhcConfig := config.(*RepoHealthCheckConfig)
|
2022-10-23 20:14:45 +05:30
|
|
|
// the git args are set by config, they can be safe to be trusted
|
|
|
|
args := make([]git.CmdArg, 0, len(rhcConfig.Args))
|
|
|
|
for _, arg := range rhcConfig.Args {
|
|
|
|
args = append(args, git.CmdArg(arg))
|
|
|
|
}
|
|
|
|
return repo_service.GitGcRepos(ctx, rhcConfig.Timeout, args...)
|
2020-05-17 05:01:38 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerRewriteAllPublicKeys() {
|
|
|
|
RegisterTaskFatal("resync_all_sshkeys", &BaseConfig{
|
|
|
|
Enabled: false,
|
|
|
|
RunAtStart: false,
|
|
|
|
Schedule: "@every 72h",
|
2021-11-24 15:19:20 +05:30
|
|
|
}, func(_ context.Context, _ *user_model.User, _ Config) error {
|
2021-12-10 13:44:24 +05:30
|
|
|
return asymkey_model.RewriteAllPublicKeys()
|
2020-05-17 05:01:38 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-10-11 06:08:09 +05:30
|
|
|
func registerRewriteAllPrincipalKeys() {
|
|
|
|
RegisterTaskFatal("resync_all_sshprincipals", &BaseConfig{
|
|
|
|
Enabled: false,
|
|
|
|
RunAtStart: false,
|
|
|
|
Schedule: "@every 72h",
|
2021-11-24 15:19:20 +05:30
|
|
|
}, func(_ context.Context, _ *user_model.User, _ Config) error {
|
2022-05-20 19:38:52 +05:30
|
|
|
return asymkey_model.RewriteAllPrincipalKeys(db.DefaultContext)
|
2020-10-11 06:08:09 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-17 05:01:38 +05:30
|
|
|
func registerRepositoryUpdateHook() {
|
|
|
|
RegisterTaskFatal("resync_all_hooks", &BaseConfig{
|
|
|
|
Enabled: false,
|
|
|
|
RunAtStart: false,
|
|
|
|
Schedule: "@every 72h",
|
2021-11-24 15:19:20 +05:30
|
|
|
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
|
2021-11-17 20:47:31 +05:30
|
|
|
return repo_service.SyncRepositoryHooks(ctx)
|
2020-05-17 05:01:38 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerReinitMissingRepositories() {
|
|
|
|
RegisterTaskFatal("reinit_missing_repos", &BaseConfig{
|
|
|
|
Enabled: false,
|
|
|
|
RunAtStart: false,
|
|
|
|
Schedule: "@every 72h",
|
2021-11-24 15:19:20 +05:30
|
|
|
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
|
2021-11-24 13:26:24 +05:30
|
|
|
return repo_service.ReinitMissingRepositories(ctx)
|
2020-05-17 05:01:38 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerDeleteMissingRepositories() {
|
|
|
|
RegisterTaskFatal("delete_missing_repos", &BaseConfig{
|
|
|
|
Enabled: false,
|
|
|
|
RunAtStart: false,
|
|
|
|
Schedule: "@every 72h",
|
2021-11-24 15:19:20 +05:30
|
|
|
}, func(ctx context.Context, user *user_model.User, _ Config) error {
|
2021-11-24 13:26:24 +05:30
|
|
|
return repo_service.DeleteMissingRepositories(ctx, user)
|
2020-05-17 05:01:38 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func registerRemoveRandomAvatars() {
|
|
|
|
RegisterTaskFatal("delete_generated_repository_avatars", &BaseConfig{
|
|
|
|
Enabled: false,
|
|
|
|
RunAtStart: false,
|
|
|
|
Schedule: "@every 72h",
|
2021-11-24 15:19:20 +05:30
|
|
|
}, func(ctx context.Context, _ *user_model.User, _ Config) error {
|
2021-12-12 21:18:20 +05:30
|
|
|
return repo_service.RemoveRandomAvatars(ctx)
|
2020-05-17 05:01:38 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-05-01 17:47:02 +05:30
|
|
|
func registerDeleteOldActions() {
|
|
|
|
RegisterTaskFatal("delete_old_actions", &OlderThanConfig{
|
|
|
|
BaseConfig: BaseConfig{
|
|
|
|
Enabled: false,
|
|
|
|
RunAtStart: false,
|
|
|
|
Schedule: "@every 168h",
|
|
|
|
},
|
|
|
|
OlderThan: 365 * 24 * time.Hour,
|
2021-11-24 15:19:20 +05:30
|
|
|
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
2021-05-01 17:47:02 +05:30
|
|
|
olderThanConfig := config.(*OlderThanConfig)
|
2022-08-25 08:01:57 +05:30
|
|
|
return activities_model.DeleteOldActions(olderThanConfig.OlderThan)
|
2021-05-01 17:47:02 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-10-16 11:44:34 +05:30
|
|
|
func registerUpdateGiteaChecker() {
|
|
|
|
type UpdateCheckerConfig struct {
|
|
|
|
BaseConfig
|
|
|
|
HTTPEndpoint string
|
|
|
|
}
|
|
|
|
RegisterTaskFatal("update_checker", &UpdateCheckerConfig{
|
|
|
|
BaseConfig: BaseConfig{
|
2022-12-19 00:26:08 +05:30
|
|
|
Enabled: false,
|
2021-10-16 11:44:34 +05:30
|
|
|
RunAtStart: false,
|
|
|
|
Schedule: "@every 168h",
|
|
|
|
},
|
|
|
|
HTTPEndpoint: "https://dl.gitea.io/gitea/version.json",
|
2021-11-24 15:19:20 +05:30
|
|
|
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
2021-10-16 11:44:34 +05:30
|
|
|
updateCheckerConfig := config.(*UpdateCheckerConfig)
|
2021-10-21 21:40:49 +05:30
|
|
|
return updatechecker.GiteaUpdateChecker(updateCheckerConfig.HTTPEndpoint)
|
2021-10-16 11:44:34 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-03-28 18:24:59 +05:30
|
|
|
func registerDeleteOldSystemNotices() {
|
|
|
|
RegisterTaskFatal("delete_old_system_notices", &OlderThanConfig{
|
|
|
|
BaseConfig: BaseConfig{
|
|
|
|
Enabled: false,
|
|
|
|
RunAtStart: false,
|
|
|
|
Schedule: "@every 168h",
|
|
|
|
},
|
|
|
|
OlderThan: 365 * 24 * time.Hour,
|
|
|
|
}, func(ctx context.Context, _ *user_model.User, config Config) error {
|
|
|
|
olderThanConfig := config.(*OlderThanConfig)
|
2022-10-17 04:59:26 +05:30
|
|
|
return system.DeleteOldSystemNotices(olderThanConfig.OlderThan)
|
2022-03-28 18:24:59 +05:30
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2020-05-17 05:01:38 +05:30
|
|
|
func initExtendedTasks() {
|
|
|
|
registerDeleteInactiveUsers()
|
|
|
|
registerDeleteRepositoryArchives()
|
|
|
|
registerGarbageCollectRepositories()
|
|
|
|
registerRewriteAllPublicKeys()
|
2020-10-11 06:08:09 +05:30
|
|
|
registerRewriteAllPrincipalKeys()
|
2020-05-17 05:01:38 +05:30
|
|
|
registerRepositoryUpdateHook()
|
|
|
|
registerReinitMissingRepositories()
|
|
|
|
registerDeleteMissingRepositories()
|
|
|
|
registerRemoveRandomAvatars()
|
2021-05-01 17:47:02 +05:30
|
|
|
registerDeleteOldActions()
|
2021-10-16 11:44:34 +05:30
|
|
|
registerUpdateGiteaChecker()
|
2022-03-28 18:24:59 +05:30
|
|
|
registerDeleteOldSystemNotices()
|
2020-05-17 05:01:38 +05:30
|
|
|
}
|