File size: 8,557 Bytes
c4ae742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
import { randomUUID } from "node:crypto";
import pg from "pg";

import {
  type DetectedMode,
  type ItemRow,
  type ItemStatus,
  type JobRow,
  type JobStatus,
  type SetItemStatusMeta,
  type Store,
  nowMs,
  truncateError,
} from "./store.js";

const { Pool } = pg;

export const POSTGRES_TAKE_NEXT_ITEM_SQL = `
WITH next_item AS (
  SELECT id
  FROM items
  WHERE job_id = $1 AND status = 'queued'
  ORDER BY ord ASC
  LIMIT 1
  FOR UPDATE SKIP LOCKED
)
UPDATE items
SET status = 'running',
    attempt_count = attempt_count + 1,
    error_preview = NULL,
    last_stage = 'running',
    updated_at = $2
FROM next_item
WHERE items.id = next_item.id
RETURNING items.*
`;

const SCHEMA_SQL = `
CREATE TABLE IF NOT EXISTS jobs (
  id TEXT PRIMARY KEY,
  status TEXT NOT NULL,
  options_snapshot TEXT NOT NULL,
  created_at BIGINT NOT NULL,
  updated_at BIGINT NOT NULL
);

CREATE TABLE IF NOT EXISTS items (
  id TEXT PRIMARY KEY,
  job_id TEXT NOT NULL REFERENCES jobs(id) ON DELETE CASCADE,
  ord INTEGER NOT NULL,
  filename TEXT NOT NULL,
  upload_path TEXT NOT NULL,
  detected_mode TEXT NOT NULL,
  status TEXT NOT NULL,
  evidence_path TEXT,
  task_package_path TEXT,
  judge_result_path TEXT,
  export_zip_path TEXT,
  error_preview TEXT,
  error_details_path TEXT,
  attempt_count INTEGER NOT NULL DEFAULT 0,
  last_stage TEXT,
  created_at BIGINT NOT NULL,
  updated_at BIGINT NOT NULL
);

ALTER TABLE items ADD COLUMN IF NOT EXISTS error_details_path TEXT;
ALTER TABLE items ADD COLUMN IF NOT EXISTS attempt_count INTEGER NOT NULL DEFAULT 0;
ALTER TABLE items ADD COLUMN IF NOT EXISTS last_stage TEXT;

CREATE INDEX IF NOT EXISTS idx_items_job_ord ON items(job_id, ord);
CREATE INDEX IF NOT EXISTS idx_items_status ON items(status);
CREATE INDEX IF NOT EXISTS idx_items_job_status_ord ON items(job_id, status, ord);
`;

function mapJob(row: any): JobRow {
  return {
    ...row,
    created_at: Number(row.created_at),
    updated_at: Number(row.updated_at),
  };
}

function mapItem(row: any): ItemRow {
  return {
    ...row,
    attempt_count: Number(row.attempt_count || 0),
    created_at: Number(row.created_at),
    updated_at: Number(row.updated_at),
    evidence_path: row.evidence_path ?? null,
    task_package_path: row.task_package_path ?? null,
    judge_result_path: row.judge_result_path ?? null,
    export_zip_path: row.export_zip_path ?? null,
    error_preview: row.error_preview ?? null,
    error_details_path: row.error_details_path ?? null,
    last_stage: row.last_stage ?? null,
  };
}

export class PostgresStore implements Store {
  private pool: pg.Pool;

  constructor(databaseUrl: string) {
    if (!databaseUrl) throw new Error("DATABASE_URL is required for Postgres store");
    this.pool = new Pool({ connectionString: databaseUrl });
  }

  async init(): Promise<void> {
    await this.pool.query(SCHEMA_SQL);
  }

  async close(): Promise<void> {
    await this.pool.end();
  }

  async createJob(optionsSnapshot: object): Promise<JobRow> {
    const id = randomUUID();
    const t = nowMs();
    const result = await this.pool.query(
      `INSERT INTO jobs (id, status, options_snapshot, created_at, updated_at)
       VALUES ($1, $2, $3, $4, $5)
       RETURNING *`,
      [id, "pending", JSON.stringify(optionsSnapshot), t, t],
    );
    return mapJob(result.rows[0]);
  }

  async getJob(id: string): Promise<JobRow | null> {
    const result = await this.pool.query("SELECT * FROM jobs WHERE id = $1", [id]);
    return result.rows[0] ? mapJob(result.rows[0]) : null;
  }

  async setJobStatus(id: string, status: JobStatus): Promise<void> {
    await this.pool.query("UPDATE jobs SET status = $1, updated_at = $2 WHERE id = $3", [
      status,
      nowMs(),
      id,
    ]);
  }

  async updateJobOptions(id: string, optionsSnapshot: object): Promise<void> {
    await this.pool.query(
      "UPDATE jobs SET options_snapshot = $1, updated_at = $2 WHERE id = $3",
      [JSON.stringify(optionsSnapshot), nowMs(), id],
    );
  }

  async listResumableJobs(): Promise<JobRow[]> {
    const result = await this.pool.query(
      "SELECT * FROM jobs WHERE status = 'running' ORDER BY created_at",
    );
    return result.rows.map(mapJob);
  }

  async recoverInterruptedJob(id: string): Promise<void> {
    const t = nowMs();
    const client = await this.pool.connect();
    try {
      await client.query("BEGIN");
      await client.query(
        `UPDATE items
         SET status = 'queued',
             error_preview = NULL,
             last_stage = 'recovered',
             updated_at = $1
         WHERE job_id = $2 AND status IN ('running', 'judging')`,
        [t, id],
      );
      await client.query("UPDATE jobs SET status = 'running', updated_at = $1 WHERE id = $2", [
        t,
        id,
      ]);
      await client.query("COMMIT");
    } catch (err) {
      await client.query("ROLLBACK");
      throw err;
    } finally {
      client.release();
    }
  }

  async createItem(input: {
    jobId: string;
    ord: number;
    filename: string;
    uploadPath: string;
    detectedMode: DetectedMode;
  }): Promise<ItemRow> {
    const id = randomUUID();
    const t = nowMs();
    const result = await this.pool.query(
      `INSERT INTO items
       (id, job_id, ord, filename, upload_path, detected_mode, status,
        created_at, updated_at)
       VALUES ($1, $2, $3, $4, $5, $6, 'queued', $7, $8)
       RETURNING *`,
      [id, input.jobId, input.ord, input.filename, input.uploadPath, input.detectedMode, t, t],
    );
    return mapItem(result.rows[0]);
  }

  async getItem(id: string): Promise<ItemRow | null> {
    const result = await this.pool.query("SELECT * FROM items WHERE id = $1", [id]);
    return result.rows[0] ? mapItem(result.rows[0]) : null;
  }

  async listItemsByJob(jobId: string): Promise<ItemRow[]> {
    const result = await this.pool.query(
      "SELECT * FROM items WHERE job_id = $1 ORDER BY ord ASC",
      [jobId],
    );
    return result.rows.map(mapItem);
  }

  async setItemStatus(
    id: string,
    status: ItemStatus,
    errorPreview?: string,
    meta: SetItemStatusMeta = {},
  ): Promise<void> {
    await this.pool.query(
      `UPDATE items
       SET status = $1,
           error_preview = $2,
           last_stage = COALESCE($3, last_stage),
           error_details_path = COALESCE($4, error_details_path),
           updated_at = $5
       WHERE id = $6`,
      [
        status,
        truncateError(errorPreview),
        meta.lastStage ?? null,
        meta.errorDetailsPath ?? null,
        nowMs(),
        id,
      ],
    );
  }

  async setItemPaths(
    id: string,
    paths: Partial<{
      evidencePath: string;
      taskPackagePath: string;
      judgeResultPath: string;
      exportZipPath: string;
    }>,
  ): Promise<void> {
    const fields: string[] = [];
    const values: unknown[] = [];
    const add = (column: string, value: unknown) => {
      values.push(value);
      fields.push(`${column} = $${values.length}`);
    };
    if (paths.evidencePath !== undefined) add("evidence_path", paths.evidencePath);
    if (paths.taskPackagePath !== undefined) add("task_package_path", paths.taskPackagePath);
    if (paths.judgeResultPath !== undefined) add("judge_result_path", paths.judgeResultPath);
    if (paths.exportZipPath !== undefined) add("export_zip_path", paths.exportZipPath);
    if (!fields.length) return;
    values.push(nowMs());
    fields.push(`updated_at = $${values.length}`);
    values.push(id);
    await this.pool.query(`UPDATE items SET ${fields.join(", ")} WHERE id = $${values.length}`, values);
  }

  async takeNextQueuedItem(jobId: string): Promise<ItemRow | null> {
    const client = await this.pool.connect();
    try {
      await client.query("BEGIN");
      const result = await client.query(POSTGRES_TAKE_NEXT_ITEM_SQL, [jobId, nowMs()]);
      await client.query("COMMIT");
      return result.rows[0] ? mapItem(result.rows[0]) : null;
    } catch (err) {
      await client.query("ROLLBACK");
      throw err;
    } finally {
      client.release();
    }
  }

  async resetItemForRetry(id: string): Promise<void> {
    await this.pool.query(
      `UPDATE items
       SET status = 'queued',
           error_preview = NULL,
           error_details_path = NULL,
           attempt_count = 0,
           last_stage = NULL,
           evidence_path = NULL,
           task_package_path = NULL,
           judge_result_path = NULL,
           export_zip_path = NULL,
           updated_at = $1
       WHERE id = $2`,
      [nowMs(), id],
    );
  }
}