summaryrefslogtreecommitdiff
path: root/internal/cleaner
diff options
context:
space:
mode:
authorLibravatar tobi <31960611+tsmethurst@users.noreply.github.com>2023-10-30 18:35:11 +0100
committerLibravatar GitHub <noreply@github.com>2023-10-30 17:35:11 +0000
commit4dc0547dc0e80a4289f46cd8ee5b3aaf855f1f1e (patch)
tree465b66e88a1defdae6c29f86e9e1a3269dc474ff /internal/cleaner
parent[chore]: Bump github.com/google/uuid from 1.3.1 to 1.4.0 (#2315) (diff)
downloadgotosocial-4dc0547dc0e80a4289f46cd8ee5b3aaf855f1f1e.tar.xz
[feature] Customizable media cleaner schedule (#2304)
Diffstat (limited to 'internal/cleaner')
-rw-r--r--internal/cleaner/cleaner.go58
1 files changed, 47 insertions, 11 deletions
diff --git a/internal/cleaner/cleaner.go b/internal/cleaner/cleaner.go
index 31766bae6..1139a85bb 100644
--- a/internal/cleaner/cleaner.go
+++ b/internal/cleaner/cleaner.go
@@ -47,7 +47,6 @@ func New(state *state.State) *Cleaner {
c.state = state
c.emoji.Cleaner = c
c.media.Cleaner = c
- scheduleJobs(c)
return c
}
@@ -109,16 +108,46 @@ func (c *Cleaner) removeFiles(ctx context.Context, files ...string) (int, error)
return diff, nil
}
-func scheduleJobs(c *Cleaner) {
- const day = time.Hour * 24
+// ScheduleJobs schedules cleaning
+// jobs using configured parameters.
+//
+// Returns an error if `MediaCleanupFrom`
+// is not a valid format (hh:mm:ss).
+func (c *Cleaner) ScheduleJobs() error {
+ const hourMinute = "15:04"
+
+ var (
+ now = time.Now()
+ cleanupEvery = config.GetMediaCleanupEvery()
+ cleanupFromStr = config.GetMediaCleanupFrom()
+ )
+
+ // Parse cleanupFromStr as hh:mm.
+ // Resulting time will be on 1 Jan year zero.
+ cleanupFrom, err := time.Parse(hourMinute, cleanupFromStr)
+ if err != nil {
+ return gtserror.Newf(
+ "error parsing '%s' in time format 'hh:mm': %w",
+ cleanupFromStr, err,
+ )
+ }
- // Calculate closest midnight.
- now := time.Now()
- midnight := now.Round(day)
+ // Time travel from
+ // year zero, groovy.
+ firstCleanupAt := time.Date(
+ now.Year(),
+ now.Month(),
+ now.Day(),
+ cleanupFrom.Hour(),
+ cleanupFrom.Minute(),
+ 0,
+ 0,
+ now.Location(),
+ )
- if midnight.Before(now) {
- // since <= 11:59am rounds down.
- midnight = midnight.Add(day)
+ // Ensure first cleanup is in the future.
+ for firstCleanupAt.Before(now) {
+ firstCleanupAt = firstCleanupAt.Add(cleanupEvery)
}
// Get ctx associated with scheduler run state.
@@ -129,11 +158,18 @@ func scheduleJobs(c *Cleaner) {
// jobs restartable if we want to implement reloads in
// the future that make call to Workers.Stop() -> Workers.Start().
- // Schedule the cleaning tasks to execute every day at midnight.
+ log.Infof(nil,
+ "scheduling media clean to run every %s, starting from %s; next clean will run at %s",
+ cleanupEvery, cleanupFromStr, firstCleanupAt,
+ )
+
+ // Schedule the cleaning tasks to execute according to given schedule.
c.state.Workers.Scheduler.Schedule(sched.NewJob(func(start time.Time) {
log.Info(nil, "starting media clean")
c.Media().All(doneCtx, config.GetMediaRemoteCacheDays())
c.Emoji().All(doneCtx, config.GetMediaRemoteCacheDays())
log.Infof(nil, "finished media clean after %s", time.Since(start))
- }).EveryAt(midnight, day))
+ }).EveryAt(firstCleanupAt, cleanupEvery))
+
+ return nil
}