summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorPilou <pierre-louis@libregerbil.fr>2022-03-28 14:54:59 +0200
committerGitHub <noreply@github.com>2022-03-28 13:54:59 +0100
commit893c8938fc81166f386ff203f2129497ceddbc32 (patch)
treea4b0d79b4c4871932d344fa9c29a0aaa77139951
parent6526733a58632086d51ce7211b3a4dc75dbbef90 (diff)
downloadgitea-893c8938fc81166f386ff203f2129497ceddbc32.tar.gz
gitea-893c8938fc81166f386ff203f2129497ceddbc32.zip
New cron task: delete old system notices (#19219)
Add a new cron task which deletes the old system notices.
-rw-r--r--custom/conf/app.example.ini13
-rw-r--r--docs/content/doc/advanced/config-cheat-sheet.en-us.md7
-rw-r--r--models/admin/notice.go11
-rw-r--r--options/locale/locale_en-US.ini1
-rw-r--r--services/cron/tasks_extended.go16
5 files changed, 48 insertions, 0 deletions
diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini
index 1b59e90e4e..8c6643b9bc 100644
--- a/custom/conf/app.example.ini
+++ b/custom/conf/app.example.ini
@@ -2020,6 +2020,19 @@ PATH =
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
+;; 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
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;[git.timeout]
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 d1d69a2310..d9018f982a 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 daf095f680..77277e4b2d 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 b371c047e3..5008cd2bab 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 ded819a71e..2d1bf53234 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()
}