Skip to content

Commit

Permalink
Expire entries from the Media ID hold table
Browse files Browse the repository at this point in the history
Fixes #426
  • Loading branch information
turt2live committed Aug 15, 2023
1 parent 66d2292 commit 5309d9a
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 5 deletions.
15 changes: 13 additions & 2 deletions database/table_media_hold.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"

"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/util"
)

type DbHeldMedia struct {
Expand All @@ -19,10 +20,12 @@ const (
ForCreateHeldReason HeldReason = "media_create"
)

const insertHeldMedia = "INSERT INTO media_id_hold (origin, media_id, reason) VALUES ($1, $2, $3);"
const insertHeldMedia = "INSERT INTO media_id_hold (origin, media_id, reason, held_ts) VALUES ($1, $2, $3, $4);"
const deleteHeldMedia = "DELETE FROM media_id_hold WHERE reason = $1 AND held_ts <= $2;"

type heldMediaTableStatements struct {
insertHeldMedia *sql.Stmt
deleteHeldMedia *sql.Stmt
}

type heldMediaTableWithContext struct {
Expand All @@ -37,6 +40,9 @@ func prepareHeldMediaTables(db *sql.DB) (*heldMediaTableStatements, error) {
if stmts.insertHeldMedia, err = db.Prepare(insertHeldMedia); err != nil {
return nil, errors.New("error preparing insertHeldMedia: " + err.Error())
}
if stmts.deleteHeldMedia, err = db.Prepare(deleteHeldMedia); err != nil {
return nil, errors.New("error preparing deleteHeldMedia: " + err.Error())
}

return stmts, nil
}
Expand All @@ -49,6 +55,11 @@ func (s *heldMediaTableStatements) Prepare(ctx rcontext.RequestContext) *heldMed
}

func (s *heldMediaTableWithContext) TryInsert(origin string, mediaId string, reason HeldReason) error {
_, err := s.statements.insertHeldMedia.ExecContext(s.ctx, origin, mediaId, reason)
_, err := s.statements.insertHeldMedia.ExecContext(s.ctx, origin, mediaId, reason, util.NowMillis())
return err
}

func (s *heldMediaTableWithContext) DeleteOlderThan(reason HeldReason, olderThanTs int64) error {
_, err := s.statements.deleteHeldMedia.ExecContext(s.ctx, reason, olderThanTs)
return err
}
1 change: 1 addition & 0 deletions migrations/24_add_timestamp_to_media_id_hold_down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE media_id_hold DROP COLUMN held_ts;
1 change: 1 addition & 0 deletions migrations/24_add_timestamp_to_media_id_hold_up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE media_id_hold ADD COLUMN held_ts BIGINT NOT NULL DEFAULT 0;
1 change: 1 addition & 0 deletions tasks/all.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ func StartAll() {
scheduleHourly(RecurringTaskPurgeRemoteMedia, task_runner.PurgeRemoteMedia)
scheduleHourly(RecurringTaskPurgeThumbnails, task_runner.PurgeThumbnails)
scheduleHourly(RecurringTaskPurgePreviews, task_runner.PurgePreviews)
scheduleHourly(RecurringTaskPurgeHeldMediaIds, task_runner.PurgeHeldMediaIds)

scheduleUnfinished()
}
Expand Down
7 changes: 4 additions & 3 deletions tasks/schedule.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,10 @@ const (
TaskImportData TaskName = "import_data"
)
const (
RecurringTaskPurgeThumbnails RecurringTaskName = "recurring_purge_thumbnails"
RecurringTaskPurgePreviews RecurringTaskName = "recurring_purge_previews"
RecurringTaskPurgeRemoteMedia RecurringTaskName = "recurring_purge_remote_media"
RecurringTaskPurgeThumbnails RecurringTaskName = "recurring_purge_thumbnails"
RecurringTaskPurgePreviews RecurringTaskName = "recurring_purge_previews"
RecurringTaskPurgeRemoteMedia RecurringTaskName = "recurring_purge_remote_media"
RecurringTaskPurgeHeldMediaIds RecurringTaskName = "recurring_purge_held_media_ids"
)

const ExecutingMachineId = int64(0)
Expand Down
20 changes: 20 additions & 0 deletions tasks/task_runner/purge_held_media_ids.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package task_runner

import (
"github.com/getsentry/sentry-go"
"github.com/turt2live/matrix-media-repo/common/rcontext"
"github.com/turt2live/matrix-media-repo/database"
"github.com/turt2live/matrix-media-repo/util"
)

func PurgeHeldMediaIds(ctx rcontext.RequestContext) {
// dev note: don't use ctx for config lookup to avoid misreading it

beforeTs := util.NowMillis() - int64(7*24*60*60*1000) // 7 days
db := database.GetInstance().HeldMedia.Prepare(ctx)

if err := db.DeleteOlderThan(database.ForCreateHeldReason, beforeTs); err != nil {
ctx.Log.Error("Error deleting held media IDs: ", err)
sentry.CaptureException(err)
}
}

0 comments on commit 5309d9a

Please sign in to comment.