media: purge guards before retirement; the pass retires gone accounts
Task 7 fix round 2 (Astra re-review, two P2s). - The expiry pass's early purge, the trash sweep and the demo claim abort run release.PurgeAppGuards at the marker clock before mediaPurgeRetire (demo expiry already did), so a purge its guards refuse, a provisioned store above all, retires no media job and a restored app keeps its done jobs. Both lifetime branches sit inside. The source guard gains the rule. - mediajobs.Pass runs retireGone: accounts with an unretired row, a retired one and no task unconfirmed are asked about through Deps.AccountGone (the console: no live accounts row), and a gone one is retired again, which completes Retire's second phase, so a reaped account's rows prune.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
8 files changed,
+348
−36
internal/console/demo_claim_transfer.go+6 −2internal/console/expirypass.go+24 −18internal/console/lifetimes_purge.go+20 −15internal/console/mediaretire_guard_test.go+34 −0internal/console/mediaretire_test.go+117 −0internal/console/mediawire.go+13 −0internal/mediajobs/jobs.go+64 −1internal/mediajobs/jobs_test.go+70 −0
CI failed — run details
diff --git a/internal/console/demo_claim_transfer.go b/internal/console/demo_claim_transfer.go| index 41e88bf5b..3bce45b0a 100644 |
| --- a/internal/console/demo_claim_transfer.go |
| +++ b/internal/console/demo_claim_transfer.go |
| @@ -926,8 +926,12 @@ func (s *Server) finishDemoClaimAbort(ctx context.Context, op claimOp) error { |
| if err := release.TrashApp(ctx, s.cfg.Releases, op.Target.Sqid, op.Target.App, "demo-claim", at); err != nil { |
| return err |
| } |
| - // The target app's media jobs, before its content goes (media |
| - // conversion spec §5.3); a later run resumes while a task stops. |
| + // The purge's guards, then the target app's media jobs, before its |
| + // content goes (media conversion spec §5.3): a purge the guards refuse |
| + // retires nothing, and a later run resumes while a task stops. |
| + if err := release.PurgeAppGuards(ctx, s.cfg.Releases, s.cfg.ReleasePub, op.Target.Sqid, op.Target.App, at, time.Now()); err != nil { |
| + return err |
| + } |
| if refusal, err := s.mediaPurgeRetire(ctx, op.Target.Sqid, op.Target.App); err != nil { |
| return err |
| } else if refusal != "" { |
diff --git a/internal/console/expirypass.go b/internal/console/expirypass.go| index d60b77327..8053a4684 100644 |
| --- a/internal/console/expirypass.go |
| +++ b/internal/console/expirypass.go |
| @@ -243,24 +243,30 @@ func (s *Server) purgeOne(ctx context.Context, r AppRow, now time.Time, ev evide |
| s.audit(acct, r.App, expiryActor, "purge-refused", "did not delete "+r.App+": it has email set up. Delete it from the Trash page") |
| return |
| } |
| - // The app's media jobs, before any purge step and after the refusals |
| - // above: while a task is still stopping this pass leaves the app and a |
| - // later one purges it (media conversion spec §5.3). |
| - if refusal, err := s.mediaPurgeRetire(ctx, r.Sqid, r.App); err != nil { |
| - log.Printf("console: expiry pass: retire the media jobs of %s/%s: %v; not purging", r.Sqid, r.App, err) |
| - jobFailed(ctx, fmt.Errorf("expiry pass: retire the media jobs of %s/%s: %w", r.Sqid, r.App, err)) |
| - return |
| - } else if refusal != "" { |
| - log.Printf("console: expiry pass: %s/%s: %s Not purging it this pass.", r.Sqid, r.App, refusal) |
| - return |
| - } |
| - s.noteRecordDeletion(r.Sqid, r.App, time.Now()) // the canary-retire guard's evidence, first |
| - if lifetimesUnit(ctx) { |
| - // Every lifetime of the app retired first, under the lock this |
| - // pass holds (lifetimes_purge.go). |
| - err = s.purgeAppWithLifetimes(ctx, r.Sqid, r.App, time.Unix(life.PurgeFor, 0), now) |
| - } else { |
| - err = release.PurgeAppIfTrashedAt(ctx, s.cfg.Releases, s.cfg.ReleasePub, r.Sqid, r.App, time.Unix(life.PurgeFor, 0), now) |
| + // The purge's own guards first, at the marker clock this decision was |
| + // made about: a purge they refuse (store estate, a production flag, a |
| + // restore since) retires no media job. Then the app's media jobs, |
| + // before any purge step: while a task is still stopping this pass |
| + // leaves the app and a later one purges it (media conversion spec |
| + // §5.3). Both purges below run the guards again. |
| + err = release.PurgeAppGuards(ctx, s.cfg.Releases, s.cfg.ReleasePub, r.Sqid, r.App, time.Unix(life.PurgeFor, 0), now) |
| + if err == nil { |
| + if refusal, err := s.mediaPurgeRetire(ctx, r.Sqid, r.App); err != nil { |
| + log.Printf("console: expiry pass: retire the media jobs of %s/%s: %v; not purging", r.Sqid, r.App, err) |
| + jobFailed(ctx, fmt.Errorf("expiry pass: retire the media jobs of %s/%s: %w", r.Sqid, r.App, err)) |
| + return |
| + } else if refusal != "" { |
| + log.Printf("console: expiry pass: %s/%s: %s Not purging it this pass.", r.Sqid, r.App, refusal) |
| + return |
| + } |
| + s.noteRecordDeletion(r.Sqid, r.App, time.Now()) // the canary-retire guard's evidence, first |
| + if lifetimesUnit(ctx) { |
| + // Every lifetime of the app retired first, under the lock this |
| + // pass holds (lifetimes_purge.go). |
| + err = s.purgeAppWithLifetimes(ctx, r.Sqid, r.App, time.Unix(life.PurgeFor, 0), now) |
| + } else { |
| + err = release.PurgeAppIfTrashedAt(ctx, s.cfg.Releases, s.cfg.ReleasePub, r.Sqid, r.App, time.Unix(life.PurgeFor, 0), now) |
| + } |
| } |
| switch { |
| case err == nil: |
diff --git a/internal/console/lifetimes_purge.go b/internal/console/lifetimes_purge.go| index a28cf822d..8913586c4 100644 |
| --- a/internal/console/lifetimes_purge.go |
| +++ b/internal/console/lifetimes_purge.go |
| @@ -241,22 +241,27 @@ func (s *Server) sweepTrashedApp(ctx context.Context, a release.TrashedApp, now |
| s.lifetimeLogHourly("trash-sweep "+a.Account+"/"+a.App, "console: trash sweep: %s/%s has email estate; not purging it. Delete it from the Trash page", a.Account, a.App) |
| return |
| } |
| - // The app's media jobs, before any purge step: while a task is still |
| + // The purge's own guards first, at the marker's clock: a purge they |
| + // refuse (store estate, a production flag) retires no media job. Then |
| + // the app's media jobs, before any purge step: while a task is still |
| // stopping the sweep leaves the app for a later run (media conversion |
| - // spec §5.3). |
| - if refusal, err := s.mediaPurgeRetire(ctx, a.Account, a.App); err != nil { |
| - log.Printf("console: trash sweep: retire the media jobs of %s/%s: %v; not purging", a.Account, a.App, err) |
| - jobFailed(ctx, fmt.Errorf("trash sweep: retire the media jobs of %s/%s: %w", a.Account, a.App, err)) |
| - return |
| - } else if refusal != "" { |
| - s.lifetimeLogHourly("trash-sweep "+a.Account+"/"+a.App, "console: trash sweep: %s/%s: %s Not purging it this run.", a.Account, a.App, refusal) |
| - return |
| - } |
| - if lifetimesUnit(ctx) { |
| - err = s.purgeAppWithLifetimes(ctx, a.Account, a.App, tomb.DeletedAt, now) |
| - } else { |
| - s.noteRecordDeletion(a.Account, a.App, time.Now()) // the canary-retire guard's evidence, first (#415) |
| - err = release.PurgeAppIfTrashedAt(ctx, s.cfg.Releases, s.cfg.ReleasePub, a.Account, a.App, tomb.DeletedAt, now) |
| + // spec §5.3). Both purges below run the guards again. |
| + err = release.PurgeAppGuards(ctx, s.cfg.Releases, s.cfg.ReleasePub, a.Account, a.App, tomb.DeletedAt, now) |
| + if err == nil { |
| + if refusal, err := s.mediaPurgeRetire(ctx, a.Account, a.App); err != nil { |
| + log.Printf("console: trash sweep: retire the media jobs of %s/%s: %v; not purging", a.Account, a.App, err) |
| + jobFailed(ctx, fmt.Errorf("trash sweep: retire the media jobs of %s/%s: %w", a.Account, a.App, err)) |
| + return |
| + } else if refusal != "" { |
| + s.lifetimeLogHourly("trash-sweep "+a.Account+"/"+a.App, "console: trash sweep: %s/%s: %s Not purging it this run.", a.Account, a.App, refusal) |
| + return |
| + } |
| + if lifetimesUnit(ctx) { |
| + err = s.purgeAppWithLifetimes(ctx, a.Account, a.App, tomb.DeletedAt, now) |
| + } else { |
| + s.noteRecordDeletion(a.Account, a.App, time.Now()) // the canary-retire guard's evidence, first (#415) |
| + err = release.PurgeAppIfTrashedAt(ctx, s.cfg.Releases, s.cfg.ReleasePub, a.Account, a.App, tomb.DeletedAt, now) |
| + } |
| } |
| if err != nil { |
| log.Printf("console: trash sweep: purge %s/%s: %v", a.Account, a.App, err) |
diff --git a/internal/console/mediaretire_guard_test.go b/internal/console/mediaretire_guard_test.go| index a0c5476a6..e0e37a96a 100644 |
| --- a/internal/console/mediaretire_guard_test.go |
| +++ b/internal/console/mediaretire_guard_test.go |
| @@ -73,6 +73,14 @@ var retireRules = []retireRule{ |
| }, |
| retire: []string{"mediaPurgeRetire", "purgeAppStores"}, result: true, |
| }, |
| + { |
| + // Fix round 2 (Astra P2): an automatic purge retires media jobs |
| + // only once its own guards have passed, so a purge they refuse |
| + // (store estate, a production flag, a restore) hides nothing. |
| + what: "media purge retire", |
| + match: func(_ string, c *ast.CallExpr) bool { return selectorCall(c, "", "mediaPurgeRetire") }, |
| + retire: []string{"PurgeAppGuards"}, result: true, |
| + }, |
| } |
| // mediaRetireExempt are the files whose guarded calls need no retirement, |
| @@ -331,12 +339,16 @@ func TestEveryStoreTeardownRetiresMedia(t *testing.T) { |
| "appsettings.go: handleStoreDestroy: objstore teardown", |
| "claim_purge.go: teardown: objstore teardown", |
| "demo_claim_transfer.go: finishDemoClaimAbort: app purge", |
| + "demo_claim_transfer.go: finishDemoClaimAbort: media purge retire", |
| "demo_expiry.go: retireDemoLocked: app purge", |
| "demo_expiry.go: retireDemoLocked: app purge", |
| + "demo_expiry.go: retireDemoLocked: media purge retire", |
| "expirypass.go: purgeOne: app purge", |
| "expirypass.go: purgeOne: app purge", |
| + "expirypass.go: purgeOne: media purge retire", |
| "lifetimes_purge.go: sweepTrashedApp: app purge", |
| "lifetimes_purge.go: sweepTrashedApp: app purge", |
| + "lifetimes_purge.go: sweepTrashedApp: media purge retire", |
| "materialize.go: materializeAccounts: account reap", |
| "ownership.go: deleteAccountNow: account deletion", |
| "ownership.go: handlePurgeApp: app purge", |
| @@ -392,6 +404,28 @@ func TestRetireGuardRefusesEachBypass(t *testing.T) { |
| }) |
| } |
| + // Fix round 2: an automatic purge's guards before its media retirement. |
| + for _, c := range []struct { |
| + name, src string |
| + ok bool |
| + }{ |
| + {"guards first", "\terr := release.PurgeAppGuards(ctx, nil, nil, a, b, at, now)\n\tif err == nil {\n\t\tif refusal, err := s.mediaPurgeRetire(ctx, a, b); err != nil || refusal != \"\" {\n\t\t\treturn err\n\t\t}\n\t\terr = release.PurgeAppIfTrashedAt(ctx, nil, nil, a, b, at, now)\n\t}\n\treturn err\n", true}, |
| + {"retire before the guards", "\tif refusal, err := s.mediaPurgeRetire(ctx, a, b); err != nil || refusal != \"\" {\n\t\treturn err\n\t}\n\tif err := release.PurgeAppGuards(ctx, nil, nil, a, b, at, now); err != nil {\n\t\treturn err\n\t}\n\treturn release.PurgeAppIfTrashedAt(ctx, nil, nil, a, b, at, now)\n", false}, |
| + } { |
| + t.Run(c.name, func(t *testing.T) { |
| + src := "package console\n\nfunc (s *Server) sweep(ctx context.Context) error {\n" + c.src + "}\n" |
| + fset := token.NewFileSet() |
| + f, err := parser.ParseFile(fset, "fixture.go", src, 0) |
| + if err != nil { |
| + t.Fatalf("%v\n%s", err, src) |
| + } |
| + sites, problems := retireGuard(fset, "fixture.go", f) |
| + if len(sites) != 2 || c.ok != (len(problems) == 0) { |
| + t.Fatalf("sites %v problems %v, want ok %v:\n%s", sites, problems, c.ok, src) |
| + } |
| + }) |
| + } |
| + |
| // The account rules: DeleteAccount and ReapAccount on any receiver. |
| for _, c := range []struct { |
| name, src string |
diff --git a/internal/console/mediaretire_test.go b/internal/console/mediaretire_test.go| index d68bb4861..8ad0e5cca 100644 |
| --- a/internal/console/mediaretire_test.go |
| +++ b/internal/console/mediaretire_test.go |
| @@ -748,3 +748,120 @@ func TestReapedAccountRetiresItsJobs(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// TestRefusedAutomaticPurgeRetiresNothing (fix round 2, Astra P2): an |
| +// automatic purge its own guards refuse (here the app's provisioned store) |
| +// retires no media job. The done job stays unretired through the expiry |
| +// pass's early purge and the trash sweep, and once the app leaves the |
| +// trash GET answers it as before. |
| +func TestRefusedAutomaticPurgeRetiresNothing(t *testing.T) { |
| + t.Run("expiry pass", func(t *testing.T) { |
| + f := newMediaDoor(t, "code-media-guard-expiry") |
| + doneID := f.done(mediaTestKey) |
| + now := f.clock.Truncate(time.Second).Add(time.Hour) |
| + if _, err := f.st.db.Exec(`UPDATE apps SET expires_at = ? WHERE account_id = ? AND app = 'titogo'`, now.Add(-time.Minute).Unix(), OpsAccountID); err != nil { |
| + t.Fatal(err) |
| + } |
| + f.srv.expiryPass(context.Background(), now) |
| + if markerOf(t, f.consoleFixture, "titogo") == nil { |
| + t.Fatal("fixture: the pass did not trash titogo") |
| + } |
| + f.srv.expiryPass(context.Background(), now.Add(72*time.Hour)) |
| + f.storeStanding("an early purge refused by its guards") |
| + if r := f.mediaRowOf(doneID); r.retiredAt != 0 { |
| + t.Fatalf("a purge its guards refused retired the done job: %+v", r) |
| + } |
| + }) |
| + t.Run("trash sweep", func(t *testing.T) { |
| + f := newMediaDoor(t, "code-media-guard-sweep") |
| + ctx := context.Background() |
| + doneID := f.done(mediaTestKey) |
| + now := time.Now() |
| + if err := release.TrashApp(ctx, f.rel, f.sqid, "titogo", "test", now.Add(-release.TrashCooloff-time.Hour)); err != nil { |
| + t.Fatal(err) |
| + } |
| + f.srv.trashSweep(ctx, now) |
| + f.storeStanding("a sweep refused by its guards") |
| + if tomb, err := release.ReadAppTombstone(ctx, f.rel, f.sqid, "titogo"); err != nil || tomb == nil { |
| + t.Fatalf("fixture: the marker went (%v)", err) |
| + } |
| + if r := f.mediaRowOf(doneID); r.retiredAt != 0 { |
| + t.Fatalf("a sweep its guards refused retired the done job: %+v", r) |
| + } |
| + // The app leaves the trash: its done job answers as before. |
| + if err := f.rel.Delete(ctx, release.AppTombstoneKey(f.sqid, "titogo")); err != nil { |
| + t.Fatal(err) |
| + } |
| + if r := f.get(doneID); r.status != http.StatusOK || r.job.State != "done" { |
| + t.Fatalf("GET after the refused sweep: %d %s", r.status, r.raw) |
| + } |
| + }) |
| +} |
| + |
| +// TestPassFinishesAReapedAccount (fix round 2): an account reaped while a |
| +// task was still stopping keeps its done row unretired after the reap's |
| +// retire. Once the task settles, the media pass retires the rest, and both |
| +// rows prune at their prune time. |
| +func TestPassFinishesAReapedAccount(t *testing.T) { |
| + fx, _, _, sqid := deletableFixture(t) |
| + m := armMedia(fx) |
| + ctx := context.Background() |
| + acct, err := fx.st.AccountBySqid(sqid) |
| + if err != nil || acct == nil { |
| + t.Fatal(acct, err) |
| + } |
| + arn := m.task(t, "r") |
| + m.insert(t, sqid, "shop", "r1", "running", strings.Repeat("r", 32), arn, 0) |
| + if _, err := fx.st.db.Exec(`INSERT INTO media_jobs |
| + (account, app, incarnation, job_id, key, etag, bytes, preset, class, bucket, state, submitted_at, |
| + execution_id, task_arn, dispatched_at, granted_s, deadline_s, task_stopped, settled, charged_ms, source_ms, ended_at) |
| + VALUES (?, 'shop', 'inc', 'd1', 'uploads/d.mov', 'e', 1, 'video-720p', 'short', 'b', 'done', ?, ?, '', ?, 600, 600, 1, 1, 1000, 1000, ?)`, |
| + sqid, m.clock.UnixMilli(), strings.Repeat("d", 32), m.clock.UnixMilli(), m.clock.UnixMilli()); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := fx.srv.tombstoneAccount(ctx, acct); err != nil { |
| + t.Fatal(err) |
| + } |
| + if _, err := materializeAccounts(ctx, fx.rel, fx.st, nil, fx.srv.media); err != nil { |
| + t.Fatal(err) |
| + } |
| + if d := readMediaRows(t, fx, sqid, "shop", "d1"); len(d) != 1 || d[0].retiredAt != 0 { |
| + t.Fatalf("fixture: the done row after the reap: %+v", d) |
| + } |
| + |
| + if err := fx.srv.media.Pass(ctx, m.advance(time.Minute)); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, id := range []string{"d1", "r1"} { |
| + if r := readMediaRows(t, fx, sqid, "shop", id); len(r) != 1 || r[0].retiredAt == 0 || !r[0].settled || !r[0].taskStopped { |
| + t.Fatalf("row %s after the pass: %+v", id, r) |
| + } |
| + } |
| + |
| + if err := fx.srv.media.Pass(ctx, m.advance(media.FailedRetention+time.Hour)); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, id := range []string{"d1", "r1"} { |
| + if r := readMediaRows(t, fx, sqid, "shop", id); len(r) != 0 { |
| + t.Fatalf("row %s not pruned at its prune time: %+v", id, r) |
| + } |
| + } |
| +} |
| + |
| +// TestMediaAccountGone: the pass's AccountGone is false for a live account |
| +// and true once it is deleted, or for one never seen. |
| +func TestMediaAccountGone(t *testing.T) { |
| + fx, c, csrf, sqid := deletableFixture(t) |
| + ctx := context.Background() |
| + if gone, err := fx.srv.mediaAccountGone(ctx, sqid); err != nil || gone { |
| + t.Fatalf("a live account: gone %v, %v", gone, err) |
| + } |
| + if status, _, body := postGated(t, fx, c, fx.ts.URL+"/a/"+sqid+"/delete", url.Values{"csrf": {csrf}, "confirm": {"Testco"}}); status != http.StatusSeeOther { |
| + t.Fatalf("delete = %d:\n%s", status, body) |
| + } |
| + for _, a := range []string{sqid, "zzzz"} { |
| + if gone, err := fx.srv.mediaAccountGone(ctx, a); err != nil || !gone { |
| + t.Fatalf("account %s: gone %v, %v", a, gone, err) |
| + } |
| + } |
| +} |
diff --git a/internal/console/mediawire.go b/internal/console/mediawire.go| index a4582a178..9fdfb7067 100644 |
| --- a/internal/console/mediawire.go |
| +++ b/internal/console/mediawire.go |
| @@ -43,9 +43,22 @@ func (s *Server) newMediaJobs() *mediajobs.Jobs { |
| Allowance: s.mediaAllowance, |
| Limits: s.cfg.MediaLimits, |
| Logf: log.Printf, |
| + // A deleted or reaped account's remaining rows are retired by the |
| + // pass once its tasks are confirmed stopped (Task 7 fix round 2). |
| + AccountGone: s.mediaAccountGone, |
| }) |
| } |
| +// mediaAccountGone answers whether account is no longer a live account |
| +// here: no row, or a row marked deleted (DeleteAccount, ReapAccount). |
| +func (s *Server) mediaAccountGone(ctx context.Context, account string) (bool, error) { |
| + acct, err := s.store.AccountBySqid(account) |
| + if err != nil { |
| + return false, err |
| + } |
| + return acct == nil, nil |
| +} |
| + |
| // validMediaLimits refuses media limits NewServer cannot run with, as the |
| // PDF limits are refused: every figure positive (a zero PerMinute would |
| // divide by zero in the limiter), account_minutes at least 0 (0 is no |
diff --git a/internal/mediajobs/jobs.go b/internal/mediajobs/jobs.go| index dc9901c7d..8dcdb03ab 100644 |
| --- a/internal/mediajobs/jobs.go |
| +++ b/internal/mediajobs/jobs.go |
| @@ -80,6 +80,10 @@ type Deps struct { |
| Allowance func(ctx context.Context, account string) (minutes int64, err error) |
| Limits media.Limits |
| Logf func(string, ...any) |
| + // AccountGone answers whether an account no longer exists (deleted or |
| + // reaped). The pass asks it only of accounts retireGone's query picks; |
| + // nil asks nothing. |
| + AccountGone func(ctx context.Context, account string) (bool, error) |
| } |
| // Jobs is the job table and its pass. |
| @@ -412,7 +416,66 @@ func (j *Jobs) Pass(ctx context.Context, now time.Time) error { |
| errs = append(errs, j.dispatchAll(ctx, class, now)) |
| } |
| } |
| - errs = append(errs, j.Cleanup(ctx, now), j.Prune(ctx, now)) |
| + errs = append(errs, j.retireGone(ctx, now), j.Cleanup(ctx, now), j.Prune(ctx, now)) |
| + return errors.Join(errs...) |
| +} |
| + |
| +// retireGone finishes Retire for every account that no longer exists |
| +// (Task 7 fix round 2). A reaped account is retired once, best effort, |
| +// right after its reap; when a task was still stopping then, Retire's |
| +// second phase (the done and failed rows) waited, and nothing retires a |
| +// deleted account again. Here, once such an account has no row whose task |
| +// is unconfirmed, Retire runs again and completes, so its rows prune. |
| +// |
| +// The query is cheap and narrow: an account with an unretired row, a |
| +// retired one (a retire began) and no row whose task may still run. Only |
| +// those are asked about through AccountGone, outside any transaction (the |
| +// console reads its own table on the one connection), and no lock is |
| +// taken: a gone account has no app for a submission or a dispatch to use, |
| +// as for account deletion. |
| +func (j *Jobs) retireGone(ctx context.Context, now time.Time) error { |
| + if j.d.AccountGone == nil { |
| + return nil |
| + } |
| + rs, err := j.db.QueryContext(ctx, `SELECT DISTINCT m.account FROM media_jobs m |
| + WHERE m.retired_at = 0 |
| + AND EXISTS (SELECT 1 FROM media_jobs r WHERE r.account = m.account AND r.retired_at > 0) |
| + AND NOT EXISTS (SELECT 1 FROM media_jobs p WHERE p.account = m.account AND p.task_stopped = 0) |
| + ORDER BY m.account`) |
| + if err != nil { |
| + return err |
| + } |
| + var accounts []string |
| + for rs.Next() { |
| + var a string |
| + if err := rs.Scan(&a); err != nil { |
| + rs.Close() |
| + return err |
| + } |
| + accounts = append(accounts, a) |
| + } |
| + if err := rs.Close(); err != nil { |
| + return err |
| + } |
| + if err := rs.Err(); err != nil { |
| + return err |
| + } |
| + var errs []error |
| + for _, a := range accounts { |
| + gone, err := j.d.AccountGone(ctx, a) |
| + if err != nil { |
| + errs = append(errs, fmt.Errorf("mediajobs: is account %s gone: %w", a, err)) |
| + continue |
| + } |
| + if !gone { |
| + continue |
| + } |
| + if pending, err := j.Retire(ctx, Scope{Account: a}, now); err != nil { |
| + errs = append(errs, err) |
| + } else if pending == 0 { |
| + j.logf("retired the remaining rows of gone account %s", a) |
| + } |
| + } |
| return errors.Join(errs...) |
| } |
diff --git a/internal/mediajobs/jobs_test.go b/internal/mediajobs/jobs_test.go| index ffa92d326..22f6ab475 100644 |
| --- a/internal/mediajobs/jobs_test.go |
| +++ b/internal/mediajobs/jobs_test.go |
| @@ -506,6 +506,76 @@ func TestRetireIsTwoPhase(t *testing.T) { |
| } |
| } |
| +// TestPassRetiresGoneAccounts (Task 7 fix round 2): a reaped account's |
| +// retire left its done row unretired while a task stopped. The pass |
| +// retires the rest once nothing of the account is pending, asking about |
| +// only the accounts its query picks, and the rows then prune. |
| +func TestPassRetiresGoneAccounts(t *testing.T) { |
| + h := newHarness(t, "", nil) |
| + var asked []string |
| + h.jobs.d.AccountGone = func(_ context.Context, a string) (bool, error) { |
| + asked = append(asked, a) |
| + return a == "acct1", nil |
| + } |
| + s := h.store("acct1", "shop") |
| + done := Job{Account: "acct1", App: "shop", Incarnation: s.Incarnation, ID: strings.Repeat("d", 32), Key: "uploads/d.mov", ETag: "e", |
| + Bytes: 1, Preset: video, Class: media.ClassShort, Bucket: s.Bucket, State: stateDone, SubmittedAt: h.now, |
| + Execution: strings.Repeat("d", 32), DispatchedAt: h.now, GrantedS: 600, DeadlineS: 900, TaskStopped: true, Settled: true, |
| + ChargedMS: 1000, SourceMS: 1000, EndedAt: h.now} |
| + h.insert(done) |
| + a := h.submit("acct1", "shop", "uploads/a.mov", video, "", 10<<20) |
| + h.run(a) |
| + // A live account with a retired row and an unretired one: never retired |
| + // by the pass. |
| + live := h.history("acct2", strings.Repeat("c", 32), video, media.ClassShort, 100) |
| + b := h.submit("acct2", "shop", "uploads/b.mov", video, "", 10<<20) |
| + if _, err := h.jobs.Retire(tctx, Scope{"acct2", "shop"}, h.now); err != nil { |
| + t.Fatal(err) |
| + } |
| + if n, err := h.jobs.Retire(tctx, Scope{Account: "acct1"}, h.now); err != nil || n != 1 { |
| + t.Fatalf("the reap's retire: pending %d, %v", n, err) |
| + } |
| + |
| + if err := h.jobs.retireGone(tctx, h.now); err != nil { |
| + t.Fatal(err) |
| + } |
| + if r := h.row(done); !r.RetiredAt.IsZero() { |
| + t.Fatal("retired while the account's task was still stopping") |
| + } |
| + h.now = h.now.Add(time.Minute) |
| + if err := h.jobs.Pass(tctx, h.now); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, j := range []Job{done, a} { |
| + if r := h.row(j); r.RetiredAt.IsZero() || !r.TaskStopped || !r.Settled { |
| + t.Fatalf("row %s after the pass: %+v", j.ID, r) |
| + } |
| + } |
| + if r := h.row(live); !r.RetiredAt.IsZero() { |
| + t.Fatalf("a live account's row was retired: %+v", r) |
| + } |
| + if r := h.row(b); r.RetiredAt.IsZero() { |
| + t.Fatal("fixture: acct2's queued row was not retired") |
| + } |
| + for _, x := range asked { |
| + if x != "acct1" && x != "acct2" { |
| + t.Fatalf("asked about %s", x) |
| + } |
| + } |
| + |
| + h.now = h.now.Add(media.FailedRetention + time.Hour) |
| + if err := h.jobs.Pass(tctx, h.now); err != nil { |
| + t.Fatal(err) |
| + } |
| + var n int |
| + if err := h.db.QueryRow(`SELECT COUNT(*) FROM media_jobs WHERE account = 'acct1'`).Scan(&n); err != nil { |
| + t.Fatal(err) |
| + } |
| + if n != 0 { |
| + t.Fatalf("%d rows of the gone account left after their prune time", n) |
| + } |
| +} |
| + |
| // TestDispatchFitsTheReservation (pre-flight F19: two long places per |
| // account, so only the allowance holds the second row). |
| func TestDispatchFitsTheReservation(t *testing.T) { |