From ae6d7860be6a5f5032f86369af3adcedf0ad0725 Mon Sep 17 00:00:00 2001
From: a1012112796 <1012112796@qq.com>
Date: Sat, 1 May 2021 20:17:02 +0800
Subject: [PATCH] add cron job to delete old actions from database (#15688)

that's a way to save database storage space.

Signed-off-by: a1012112796 <1012112796@qq.com>
---
 custom/conf/app.example.ini                       |  8 ++++++++
 .../doc/advanced/config-cheat-sheet.en-us.md      |  7 +++++++
 models/action.go                                  | 10 ++++++++++
 modules/cron/tasks_extended.go                    | 15 +++++++++++++++
 options/locale/locale_en-US.ini                   |  2 ++
 5 files changed, 42 insertions(+)

diff --git a/custom/conf/app.example.ini b/custom/conf/app.example.ini
index 35f1bfaeab..e8b02bdae3 100644
--- a/custom/conf/app.example.ini
+++ b/custom/conf/app.example.ini
@@ -1145,6 +1145,14 @@ RUN_AT_START = false
 NO_SUCCESS_NOTICE = false
 SCHEDULE = @every 72h
 
+; Delete all old actions from database
+[cron.delete_old_actions]
+ENABLED = false
+RUN_AT_START = false
+NO_SUCCESS_NOTICE = false
+SCHEDULE = @every 168h
+OLDER_THAN = 8760h
+
 [git]
 ; The path of git executable. If empty, Gitea searches through the PATH environment.
 PATH =
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 95c20ca410..c620614cab 100644
--- a/docs/content/doc/advanced/config-cheat-sheet.en-us.md
+++ b/docs/content/doc/advanced/config-cheat-sheet.en-us.md
@@ -786,6 +786,13 @@ NB: You must have `DISABLE_ROUTER_LOG` set to `false` for this option to take ef
 - `NO_SUCCESS_NOTICE`: **false**: Set to true to switch off success notices.
 - `SCHEDULE`: **@every 72h**: Cron syntax for scheduling repository archive cleanup, e.g. `@every 1h`.
 
+#### Cron -  Delete all old actions from database ('cron.delete_old_actions')
+- `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 128h**: Cron syntax for scheduling a work, e.g. `@every 128h`.
+- `OLDER_THAN`: **@every 8760h**: any action older than this expression will be deleted from database, suggest using `8760h` (1 year) because that's the max length of heatmap.
+
 ## Git (`git`)
 
 - `PATH`: **""**: The path of git executable. If empty, Gitea searches through the PATH environment.
diff --git a/models/action.go b/models/action.go
index 2a84133bf8..f6170005c7 100644
--- a/models/action.go
+++ b/models/action.go
@@ -395,3 +395,13 @@ func activityQueryCondition(opts GetFeedsOptions) (builder.Cond, error) {
 
 	return cond, nil
 }
+
+// DeleteOldActions deletes all old actions from database.
+func DeleteOldActions(olderThan time.Duration) (err error) {
+	if olderThan <= 0 {
+		return nil
+	}
+
+	_, err = x.Where("created_unix < ?", time.Now().Add(-olderThan).Unix()).Delete(&Action{})
+	return
+}
diff --git a/modules/cron/tasks_extended.go b/modules/cron/tasks_extended.go
index f0742eb471..4a37e5d242 100644
--- a/modules/cron/tasks_extended.go
+++ b/modules/cron/tasks_extended.go
@@ -117,6 +117,20 @@ func registerRemoveRandomAvatars() {
 	})
 }
 
+func registerDeleteOldActions() {
+	RegisterTaskFatal("delete_old_actions", &OlderThanConfig{
+		BaseConfig: BaseConfig{
+			Enabled:    false,
+			RunAtStart: false,
+			Schedule:   "@every 168h",
+		},
+		OlderThan: 365 * 24 * time.Hour,
+	}, func(ctx context.Context, _ *models.User, config Config) error {
+		olderThanConfig := config.(*OlderThanConfig)
+		return models.DeleteOldActions(olderThanConfig.OlderThan)
+	})
+}
+
 func initExtendedTasks() {
 	registerDeleteInactiveUsers()
 	registerDeleteRepositoryArchives()
@@ -127,4 +141,5 @@ func initExtendedTasks() {
 	registerReinitMissingRepositories()
 	registerDeleteMissingRepositories()
 	registerRemoveRandomAvatars()
+	registerDeleteOldActions()
 }
diff --git a/options/locale/locale_en-US.ini b/options/locale/locale_en-US.ini
index 1a8d253749..25b0a1b0bd 100644
--- a/options/locale/locale_en-US.ini
+++ b/options/locale/locale_en-US.ini
@@ -2179,6 +2179,8 @@ dashboard.total_gc_time = Total GC Pause
 dashboard.total_gc_pause = Total GC Pause
 dashboard.last_gc_pause = Last GC Pause
 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.
 
 users.user_manage_panel = User Account Management
 users.new_account = Create User Account