Skip to content

Commit

Permalink
add cron job to delete old actions from database (#15688)
Browse files Browse the repository at this point in the history
that's a way to save database storage space.

Signed-off-by: a1012112796 <1012112796@qq.com>
  • Loading branch information
a1012112796 authored May 1, 2021
1 parent ca0460b commit ae6d786
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 0 deletions.
8 changes: 8 additions & 0 deletions custom/conf/app.example.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
7 changes: 7 additions & 0 deletions docs/content/doc/advanced/config-cheat-sheet.en-us.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions models/action.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
15 changes: 15 additions & 0 deletions modules/cron/tasks_extended.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -127,4 +141,5 @@ func initExtendedTasks() {
registerReinitMissingRepositories()
registerDeleteMissingRepositories()
registerRemoveRandomAvatars()
registerDeleteOldActions()
}
2 changes: 2 additions & 0 deletions options/locale/locale_en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ae6d786

Please sign in to comment.