feat(worker): add safe failed attachment replay CLI
This commit is contained in:
@@ -272,3 +272,87 @@ func TestWorkerProcessingTimeout(t *testing.T) {
|
||||
t.Fatal("parent worker restarted after the image timeout")
|
||||
}
|
||||
}
|
||||
|
||||
func TestWorkerReplay(t *testing.T) {
|
||||
user := createUser(t)
|
||||
chat := createChat(t, user, user.ID)
|
||||
stopService(t, "worker")
|
||||
if workerQueue(t) != "0:0" {
|
||||
t.Fatal("run on an idle stand with an empty attachment queue")
|
||||
}
|
||||
att, source := workerUpload(t, user, chat.ID)
|
||||
id := att.ID.String()
|
||||
where := "attachment_id='" + id + "'"
|
||||
|
||||
testWorker := "worker-replay-" + uuid.NewString()
|
||||
t.Cleanup(func() {
|
||||
if testWorker != "" {
|
||||
workerCommand(t, "rm", "-f", testWorker)
|
||||
}
|
||||
})
|
||||
workerCommand(t, "compose", "run", "-d", "--no-deps", "--name", testWorker,
|
||||
"-e", "JOB_TIMEOUT_SECONDS=3", "-e", "JOB_MAX_ATTEMPTS=1",
|
||||
"-e", "S3_ENDPOINT=http://127.0.0.1:1", "worker")
|
||||
waitAttachment(t, user, att.ID, "failed", 15*time.Second)
|
||||
workerCommand(t, "rm", "-f", testWorker)
|
||||
testWorker = ""
|
||||
|
||||
runCLI := func(args ...string) (string, error) {
|
||||
t.Helper()
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
||||
defer cancel()
|
||||
cmd := exec.CommandContext(ctx, "docker", append([]string{
|
||||
"compose", "run", "--rm", "--no-deps", "worker",
|
||||
}, args...)...)
|
||||
cmd.Dir = ".."
|
||||
out, err := cmd.CombinedOutput()
|
||||
return string(out), err
|
||||
}
|
||||
replayArgs := []string{"-replay", id, "-operator", "synthetic-operator", "-reason", "S3 restored"}
|
||||
if out, err := runCLI("-list-failed"); err != nil || !strings.Contains(out, id) {
|
||||
t.Fatalf("failed registry did not list attachment: %v\n%s", err, out)
|
||||
}
|
||||
if out, err := runCLI("-replay", id); err == nil || !strings.Contains(out, "nonempty operator and reason") {
|
||||
t.Fatalf("replay without audit details was accepted: %v\n%s", err, out)
|
||||
}
|
||||
|
||||
// A stale DB key must not make a missing S3 source replayable.
|
||||
key := workerSQL(t, "SELECT staging_key FROM attachments WHERE id='"+id+"'")
|
||||
workerSQL(t, "UPDATE attachments SET staging_key='staging/"+uuid.NewString()+"' WHERE id='"+id+"'")
|
||||
out, err := runCLI(replayArgs...)
|
||||
workerSQL(t, "UPDATE attachments SET staging_key='"+key+"' WHERE id='"+id+"'")
|
||||
if err == nil || !strings.Contains(out, "verify replay source") {
|
||||
t.Fatalf("missing S3 source was accepted: %v\n%s", err, out)
|
||||
}
|
||||
if workerSQL(t, "SELECT status||':'||attempts||':'||retry_base FROM attachments WHERE id='"+id+"'") != "failed:1:0" ||
|
||||
workerSQL(t, "SELECT count(*) FROM attachment_replays WHERE "+where) != "0" ||
|
||||
workerSQL(t, "SELECT count(*) FROM outbox WHERE "+where) != "1" {
|
||||
t.Fatal("rejected replay changed attachment, audit or outbox")
|
||||
}
|
||||
|
||||
if out, err := runCLI(replayArgs...); err != nil {
|
||||
t.Fatalf("replay failed: %v\n%s", err, out)
|
||||
}
|
||||
if workerSQL(t, "SELECT status||':'||attempts||':'||retry_base FROM attachments WHERE id='"+id+"'") != "queued:1:1" ||
|
||||
workerSQL(t, "SELECT attempt||':'||operator||':'||reason||':'||previous_error_code FROM attachment_replays WHERE "+where) !=
|
||||
"2:synthetic-operator:S3 restored:retry_exhausted" ||
|
||||
workerSQL(t, "SELECT count(*) FROM outbox WHERE "+where) != "2" {
|
||||
t.Fatal("replay did not atomically queue the next attempt with audit and outbox")
|
||||
}
|
||||
if out, err := runCLI(replayArgs...); err == nil || !strings.Contains(out, "not replayable") {
|
||||
t.Fatalf("queued replay was accepted twice: %v\n%s", err, out)
|
||||
}
|
||||
|
||||
compose(t, "start", "worker")
|
||||
ready := waitAttachment(t, user, att.ID, "ready", 30*time.Second)
|
||||
if len(ready.Variants) != 3 || !bytes.Equal(workerContent(t, user, att.ID, "original"), source) {
|
||||
t.Fatal("replay did not complete the same attachment with unchanged original")
|
||||
}
|
||||
if out, err := runCLI(replayArgs...); err == nil || !strings.Contains(out, "not replayable") {
|
||||
t.Fatalf("ready replay was accepted: %v\n%s", err, out)
|
||||
}
|
||||
if workerSQL(t, "SELECT count(*) FROM attachment_replays WHERE "+where) != "1" ||
|
||||
workerSQL(t, "SELECT attempts FROM attachments WHERE id='"+id+"'") != "2" {
|
||||
t.Fatal("duplicate replay changed audit or attempt counter")
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user