Skip to content

Commit ae6d786

Browse files
authored
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>
1 parent ca0460b commit ae6d786

File tree

5 files changed

+42
-0
lines changed

5 files changed

+42
-0
lines changed

custom/conf/app.example.ini

+8
Original file line numberDiff line numberDiff line change
@@ -1145,6 +1145,14 @@ RUN_AT_START = false
11451145
NO_SUCCESS_NOTICE = false
11461146
SCHEDULE = @every 72h
11471147

1148+
; Delete all old actions from database
1149+
[cron.delete_old_actions]
1150+
ENABLED = false
1151+
RUN_AT_START = false
1152+
NO_SUCCESS_NOTICE = false
1153+
SCHEDULE = @every 168h
1154+
OLDER_THAN = 8760h
1155+
11481156
[git]
11491157
; The path of git executable. If empty, Gitea searches through the PATH environment.
11501158
PATH =

docs/content/doc/advanced/config-cheat-sheet.en-us.md

+7
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,13 @@ NB: You must have `DISABLE_ROUTER_LOG` set to `false` for this option to take ef
786786
- `NO_SUCCESS_NOTICE`: **false**: Set to true to switch off success notices.
787787
- `SCHEDULE`: **@every 72h**: Cron syntax for scheduling repository archive cleanup, e.g. `@every 1h`.
788788

789+
#### Cron - Delete all old actions from database ('cron.delete_old_actions')
790+
- `ENABLED`: **false**: Enable service.
791+
- `RUN_AT_START`: **false**: Run tasks at start up time (if ENABLED).
792+
- `NO_SUCCESS_NOTICE`: **false**: Set to true to switch off success notices.
793+
- `SCHEDULE`: **@every 128h**: Cron syntax for scheduling a work, e.g. `@every 128h`.
794+
- `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.
795+
789796
## Git (`git`)
790797

791798
- `PATH`: **""**: The path of git executable. If empty, Gitea searches through the PATH environment.

models/action.go

+10
Original file line numberDiff line numberDiff line change
@@ -395,3 +395,13 @@ func activityQueryCondition(opts GetFeedsOptions) (builder.Cond, error) {
395395

396396
return cond, nil
397397
}
398+
399+
// DeleteOldActions deletes all old actions from database.
400+
func DeleteOldActions(olderThan time.Duration) (err error) {
401+
if olderThan <= 0 {
402+
return nil
403+
}
404+
405+
_, err = x.Where("created_unix < ?", time.Now().Add(-olderThan).Unix()).Delete(&Action{})
406+
return
407+
}

modules/cron/tasks_extended.go

+15
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,20 @@ func registerRemoveRandomAvatars() {
117117
})
118118
}
119119

120+
func registerDeleteOldActions() {
121+
RegisterTaskFatal("delete_old_actions", &OlderThanConfig{
122+
BaseConfig: BaseConfig{
123+
Enabled: false,
124+
RunAtStart: false,
125+
Schedule: "@every 168h",
126+
},
127+
OlderThan: 365 * 24 * time.Hour,
128+
}, func(ctx context.Context, _ *models.User, config Config) error {
129+
olderThanConfig := config.(*OlderThanConfig)
130+
return models.DeleteOldActions(olderThanConfig.OlderThan)
131+
})
132+
}
133+
120134
func initExtendedTasks() {
121135
registerDeleteInactiveUsers()
122136
registerDeleteRepositoryArchives()
@@ -127,4 +141,5 @@ func initExtendedTasks() {
127141
registerReinitMissingRepositories()
128142
registerDeleteMissingRepositories()
129143
registerRemoveRandomAvatars()
144+
registerDeleteOldActions()
130145
}

options/locale/locale_en-US.ini

+2
Original file line numberDiff line numberDiff line change
@@ -2179,6 +2179,8 @@ dashboard.total_gc_time = Total GC Pause
21792179
dashboard.total_gc_pause = Total GC Pause
21802180
dashboard.last_gc_pause = Last GC Pause
21812181
dashboard.gc_times = GC Times
2182+
dashboard.delete_old_actions = Delete all old actions from database
2183+
dashboard.delete_old_actions.started = Delete all old actions from database started.
21822184

21832185
users.user_manage_panel = User Account Management
21842186
users.new_account = Create User Account

0 commit comments

Comments
 (0)