add services/mam-api/src/routes/jobs.js
This commit is contained in:
parent
5cb9ccaefc
commit
55b765698c
1 changed files with 117 additions and 0 deletions
117
services/mam-api/src/routes/jobs.js
Normal file
117
services/mam-api/src/routes/jobs.js
Normal file
|
|
@ -0,0 +1,117 @@
|
|||
import express from 'express';
|
||||
import pool from '../db/pool.js';
|
||||
import { requireAuth } from '../middleware/auth.js';
|
||||
import { Queue } from 'bullmq';
|
||||
import { v4 as uuidv4 } from 'uuid';
|
||||
|
||||
const router = express.Router();
|
||||
|
||||
router.use(requireAuth);
|
||||
|
||||
// Initialize BullMQ queue for conform jobs
|
||||
const conformQueue = new Queue('conform', {
|
||||
connection: {
|
||||
host: process.env.REDIS_HOST || 'localhost',
|
||||
port: process.env.REDIS_PORT || 6379,
|
||||
},
|
||||
});
|
||||
|
||||
// GET / - List jobs
|
||||
router.get('/', async (req, res, next) => {
|
||||
try {
|
||||
const { type, status, asset_id } = req.query;
|
||||
|
||||
let query = 'SELECT * FROM jobs WHERE 1=1';
|
||||
const params = [];
|
||||
let paramCount = 1;
|
||||
|
||||
if (type) {
|
||||
query += ` AND type = $${paramCount++}`;
|
||||
params.push(type);
|
||||
}
|
||||
|
||||
if (status) {
|
||||
query += ` AND status = $${paramCount++}`;
|
||||
params.push(status);
|
||||
}
|
||||
|
||||
if (asset_id) {
|
||||
query += ` AND asset_id = $${paramCount++}`;
|
||||
params.push(asset_id);
|
||||
}
|
||||
|
||||
query += ' ORDER BY created_at DESC';
|
||||
|
||||
const result = await pool.query(query, params);
|
||||
res.json(result.rows);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
// GET /:id - Single job with progress
|
||||
router.get('/:id', async (req, res, next) => {
|
||||
try {
|
||||
const { id } = req.params;
|
||||
|
||||
const result = await pool.query(
|
||||
'SELECT * FROM jobs WHERE id = $1',
|
||||
[id]
|
||||
);
|
||||
|
||||
if (result.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'Job not found' });
|
||||
}
|
||||
|
||||
const job = result.rows[0];
|
||||
|
||||
res.json(job);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
// POST /conform - Submit conform job
|
||||
router.post('/conform', async (req, res, next) => {
|
||||
try {
|
||||
const { edl, project_id, output_format } = req.body;
|
||||
|
||||
if (!edl || !project_id || !output_format) {
|
||||
return res.status(400).json({
|
||||
error: 'edl, project_id, and output_format are required',
|
||||
});
|
||||
}
|
||||
|
||||
const jobId = uuidv4();
|
||||
|
||||
// Create job record in database
|
||||
const result = await pool.query(
|
||||
`INSERT INTO jobs (id, type, status, project_id, metadata, created_at, updated_at)
|
||||
VALUES ($1, $2, $3, $4, $5, NOW(), NOW())
|
||||
RETURNING *`,
|
||||
[
|
||||
jobId,
|
||||
'conform',
|
||||
'pending',
|
||||
project_id,
|
||||
JSON.stringify({ edl, output_format }),
|
||||
]
|
||||
);
|
||||
|
||||
const job = result.rows[0];
|
||||
|
||||
// Add to BullMQ queue
|
||||
await conformQueue.add('conform-task', {
|
||||
jobId,
|
||||
edl,
|
||||
project_id,
|
||||
output_format,
|
||||
});
|
||||
|
||||
res.status(201).json(job);
|
||||
} catch (err) {
|
||||
next(err);
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
Loading…
Reference in a new issue