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>
This commit is contained in:
a1012112796 2021-05-01 20:17:02 +08:00 committed by GitHub
parent ca0460beb7
commit ae6d7860be
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 42 additions and 0 deletions

View File

@ -1145,6 +1145,14 @@ RUN_AT_START = false
NO_SUCCESS_NOTICE = false NO_SUCCESS_NOTICE = false
SCHEDULE = @every 72h 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] [git]
; The path of git executable. If empty, Gitea searches through the PATH environment. ; The path of git executable. If empty, Gitea searches through the PATH environment.
PATH = PATH =

View 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. - `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`. - `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`) ## Git (`git`)
- `PATH`: **""**: The path of git executable. If empty, Gitea searches through the PATH environment. - `PATH`: **""**: The path of git executable. If empty, Gitea searches through the PATH environment.

View File

@ -395,3 +395,13 @@ func activityQueryCondition(opts GetFeedsOptions) (builder.Cond, error) {
return cond, nil 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
}

View 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() { func initExtendedTasks() {
registerDeleteInactiveUsers() registerDeleteInactiveUsers()
registerDeleteRepositoryArchives() registerDeleteRepositoryArchives()
@ -127,4 +141,5 @@ func initExtendedTasks() {
registerReinitMissingRepositories() registerReinitMissingRepositories()
registerDeleteMissingRepositories() registerDeleteMissingRepositories()
registerRemoveRandomAvatars() registerRemoveRandomAvatars()
registerDeleteOldActions()
} }

View File

@ -2179,6 +2179,8 @@ dashboard.total_gc_time = Total GC Pause
dashboard.total_gc_pause = Total GC Pause dashboard.total_gc_pause = Total GC Pause
dashboard.last_gc_pause = Last GC Pause dashboard.last_gc_pause = Last GC Pause
dashboard.gc_times = GC Times 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.user_manage_panel = User Account Management
users.new_account = Create User Account users.new_account = Create User Account