From 893c8938fc81166f386ff203f2129497ceddbc32 Mon Sep 17 00:00:00 2001 From: Pilou Date: Mon, 28 Mar 2022 14:54:59 +0200 Subject: [PATCH] New cron task: delete old system notices (#19219) Add a new cron task which deletes the old system notices. --- custom/conf/app.example.ini | 13 +++++++++++++ .../doc/advanced/config-cheat-sheet.en-us.md | 7 +++++++ models/admin/notice.go | 11 +++++++++++ options/locale/locale_en-US.ini | 1 + services/cron/tasks_extended.go | 16 ++++++++++++++++ 5 files changed, 48 insertions(+) diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini index 1b59e90e4..8c6643b9b 100644 --- a/custom/conf/app.example.ini +++ b/custom/conf/app.example.ini @@ -2018,6 +2018,19 @@ PATH = ;SCHEDULE = @every 168h ;HTTP_ENDPOINT = https://dl.gitea.io/gitea/version.json +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Delete all old system notices from database +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;[cron.delete_old_system_notices] +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;ENABLED = false +;RUN_AT_START = false +;NO_SUCCESS_NOTICE = false +;SCHEDULE = @every 168h +;OLDER_THAN = 8760h + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Git Operation timeout in seconds diff --git a/docs/content/doc/advanced/config-cheat-sheet.en-us.md b/docs/content/doc/advanced/config-cheat-sheet.en-us.md index d1d69a231..d9018f982 100644 --- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md +++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md @@ -921,6 +921,13 @@ Default templates for project boards: - `SCHEDULE`: **@every 168h**: Cron syntax for scheduling a work, e.g. `@every 168h`. - `HTTP_ENDPOINT`: **https://dl.gitea.io/gitea/version.json**: the endpoint that Gitea will check for newer versions +#### Cron - Delete all old system notices from database ('cron.delete_old_system_notices') +- `ENABLED`: **false**: Enable service. +- `RUN_AT_START`: **false**: Run tasks at start up time (if ENABLED). +- `NO_SUCCESS_NOTICE`: **false**: Set to true to switch off success notices. +- `SCHEDULE`: **@every 168h**: Cron syntax to set how often to check. +- `OLDER_THAN`: **@every 8760h**: any system notice older than this expression will be deleted from database. + ## Git (`git`) - `PATH`: **""**: The path of Git executable. If empty, Gitea searches through the PATH environment. diff --git a/models/admin/notice.go b/models/admin/notice.go index daf095f68..77277e4b2 100644 --- a/models/admin/notice.go +++ b/models/admin/notice.go @@ -7,6 +7,7 @@ package admin import ( "context" "fmt" + "time" "code.gitea.io/gitea/models/db" "code.gitea.io/gitea/modules/log" @@ -133,3 +134,13 @@ func DeleteNoticesByIDs(ids []int64) error { Delete(new(Notice)) return err } + +// DeleteOldSystemNotices deletes all old system notices from database. +func DeleteOldSystemNotices(olderThan time.Duration) (err error) { + if olderThan <= 0 { + return nil + } + + _, err = db.GetEngine(db.DefaultContext).Where("created_unix < ?", time.Now().Add(-olderThan).Unix()).Delete(&Notice{}) + return +} diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini index b371c047e..5008cd2ba 100644 --- a/options/locale/locale_en-US.ini +++ b/options/locale/locale_en-US.ini @@ -2463,6 +2463,7 @@ dashboard.gc_times = GC Times dashboard.delete_old_actions = Delete all old actions from database dashboard.delete_old_actions.started = Delete all old actions from database started. dashboard.update_checker = Update checker +dashboard.delete_old_system_notices = Delete all old system notices from database users.user_manage_panel = User Account Management users.new_account = Create User Account diff --git a/services/cron/tasks_extended.go b/services/cron/tasks_extended.go index ded819a71..2d1bf5323 100644 --- a/services/cron/tasks_extended.go +++ b/services/cron/tasks_extended.go @@ -9,6 +9,7 @@ import ( "time" "code.gitea.io/gitea/models" + "code.gitea.io/gitea/models/admin" asymkey_model "code.gitea.io/gitea/models/asymkey" user_model "code.gitea.io/gitea/models/user" "code.gitea.io/gitea/modules/setting" @@ -154,6 +155,20 @@ func registerUpdateGiteaChecker() { }) } +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) + return admin.DeleteOldSystemNotices(olderThanConfig.OlderThan) + }) +} + func initExtendedTasks() { registerDeleteInactiveUsers() registerDeleteRepositoryArchives() @@ -166,4 +181,5 @@ func initExtendedTasks() { registerRemoveRandomAvatars() registerDeleteOldActions() registerUpdateGiteaChecker() + registerDeleteOldSystemNotices() }