]> source.dussan.org Git - gitea.git/commitdiff
add cron job to delete old actions from database (#15688)
authora1012112796 <1012112796@qq.com>
Sat, 1 May 2021 12:17:02 +0000 (20:17 +0800)
committerGitHub <noreply@github.com>
Sat, 1 May 2021 12:17:02 +0000 (14:17 +0200)
that's a way to save database storage space.

Signed-off-by: a1012112796 <1012112796@qq.com>
custom/conf/app.example.ini
docs/content/doc/advanced/config-cheat-sheet.en-us.md
models/action.go
modules/cron/tasks_extended.go
options/locale/locale_en-US.ini

index 35f1bfaeabf2a5b16553f98a30f7a325c5b62dc1..e8b02bdae3df2c441b271a3327d17de66a8b62d6 100644 (file)
@@ -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 =
index 95c20ca41057bd0ae319d6478ccf7fb70325de5e..c620614cab63dac19b909f01985d8285c0fb11f3 100644 (file)
@@ -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.
index 2a84133bf8901d1ecd336d45c83d9e7a59e1d12b..f6170005c7a6bdf903d1312afd205a715568a5b1 100644 (file)
@@ -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
+}
index f0742eb471f36b1efd4ab95a51d41e2ceaad55fb..4a37e5d242ee329890cc39d5a0531dd2b2042728 100644 (file)
@@ -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()
 }
index 1a8d253749cf5a98ce7edf0baee927d89636b8b3..25b0a1b0bd33fcc43be3f829b7f7a282f5f3d6e8 100644 (file)
@@ -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