102 lines
3.8 KiB
JavaScript
102 lines
3.8 KiB
JavaScript
"use strict";
|
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
exports.createExportJob = createExportJob;
|
|
exports.getExportJobForUser = getExportJobForUser;
|
|
exports.claimNextQueuedJob = claimNextQueuedJob;
|
|
exports.requeueStaleRunningJobs = requeueStaleRunningJobs;
|
|
exports.requeueAllRunningJobs = requeueAllRunningJobs;
|
|
exports.updateExportJobProgress = updateExportJobProgress;
|
|
exports.completeExportJob = completeExportJob;
|
|
exports.failExportJob = failExportJob;
|
|
exports.deleteExpiredExportJobs = deleteExpiredExportJobs;
|
|
const connection_1 = require("../db/connection");
|
|
async function createExportJob(input) {
|
|
const rows = await (0, connection_1.query)(`INSERT INTO admin_export_jobs (
|
|
id, requested_by, status, file_name, request_payload, format, max_rows, columns, expires_at,
|
|
job_kind, report_key
|
|
)
|
|
VALUES ($1, $2, 'queued', $3, $4::jsonb, $5, $6, $7::jsonb, $8, $9, $10)
|
|
RETURNING *`, [
|
|
input.id,
|
|
input.requestedBy,
|
|
input.fileName,
|
|
JSON.stringify(input.requestPayload),
|
|
input.format,
|
|
input.maxRows,
|
|
JSON.stringify(input.columns),
|
|
input.expiresAt,
|
|
input.jobKind ?? 'users',
|
|
input.reportKey ?? null,
|
|
]);
|
|
return rows[0];
|
|
}
|
|
async function getExportJobForUser(jobId, requestedBy) {
|
|
const rows = await (0, connection_1.query)(`SELECT * FROM admin_export_jobs WHERE id = $1 AND requested_by = $2`, [jobId, requestedBy]);
|
|
return rows[0] ?? null;
|
|
}
|
|
async function claimNextQueuedJob(workerId) {
|
|
const rows = await (0, connection_1.query)(`UPDATE admin_export_jobs j
|
|
SET status = 'running',
|
|
locked_at = NOW(),
|
|
locked_by = $1,
|
|
started_at = COALESCE(j.started_at, NOW())
|
|
FROM (
|
|
SELECT id FROM admin_export_jobs
|
|
WHERE status = 'queued'
|
|
ORDER BY created_at ASC
|
|
LIMIT 1
|
|
FOR UPDATE SKIP LOCKED
|
|
) picked
|
|
WHERE j.id = picked.id
|
|
RETURNING j.*`, [workerId]);
|
|
return rows[0] ?? null;
|
|
}
|
|
async function requeueStaleRunningJobs(staleMinutes = 30) {
|
|
const rows = await (0, connection_1.query)(`UPDATE admin_export_jobs
|
|
SET status = 'queued', locked_at = NULL, locked_by = NULL
|
|
WHERE status = 'running'
|
|
AND locked_at IS NOT NULL
|
|
AND locked_at < NOW() - ($1::text || ' minutes')::interval
|
|
RETURNING id`, [staleMinutes]);
|
|
return rows.length;
|
|
}
|
|
async function requeueAllRunningJobs() {
|
|
const rows = await (0, connection_1.query)(`UPDATE admin_export_jobs
|
|
SET status = 'queued', locked_at = NULL, locked_by = NULL
|
|
WHERE status = 'running'
|
|
RETURNING id`);
|
|
return rows.length;
|
|
}
|
|
async function updateExportJobProgress(jobId, progress, rowCount, totalEstimate) {
|
|
await (0, connection_1.query)(`UPDATE admin_export_jobs
|
|
SET progress = $2, row_count = $3,
|
|
total_estimate = COALESCE($4, total_estimate)
|
|
WHERE id = $1`, [jobId, progress, rowCount, totalEstimate ?? null]);
|
|
}
|
|
async function completeExportJob(jobId, filePath, rowCount) {
|
|
await (0, connection_1.query)(`UPDATE admin_export_jobs
|
|
SET status = 'completed',
|
|
file_path = $2,
|
|
progress = $3,
|
|
row_count = $3,
|
|
completed_at = NOW(),
|
|
locked_at = NULL,
|
|
locked_by = NULL,
|
|
error = NULL
|
|
WHERE id = $1`, [jobId, filePath, rowCount]);
|
|
}
|
|
async function failExportJob(jobId, error) {
|
|
await (0, connection_1.query)(`UPDATE admin_export_jobs
|
|
SET status = 'failed',
|
|
error = $2,
|
|
completed_at = NOW(),
|
|
locked_at = NULL,
|
|
locked_by = NULL
|
|
WHERE id = $1`, [jobId, error]);
|
|
}
|
|
async function deleteExpiredExportJobs() {
|
|
return (0, connection_1.query)(`DELETE FROM admin_export_jobs
|
|
WHERE expires_at < NOW()
|
|
RETURNING id, file_path`);
|
|
}
|
|
//# sourceMappingURL=adminExportJobRepository.js.map
|