summaryrefslogtreecommitdiff
path: root/admin-panel/src/server/routes/pipeline.ts
blob: 39df35c5707948671b60c4415b1fdb0b25fdddfd (plain)
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
import { Router, Request, Response } from 'express';
import { getAdminDb } from '../db.js';
import {
  runFullPipeline,
  runStep,
  stopPipeline,
  getPipelineStatus,
  isPipelineRunning,
} from '../services/pipeline.service.js';

const router = Router({ mergeParams: true });

type Params = { project: string; taskId: string };

function projectExists(name: string): boolean {
  const db = getAdminDb();
  return !!db.prepare('SELECT id FROM projects WHERE name = ?').get(name);
}

// POST /api/projects/:project/pipeline/run/:taskId
router.post('/run/:taskId', async (req: Request<Params>, res: Response) => {
  const { project, taskId } = req.params;
  if (!projectExists(project)) {
    res.status(404).json({ error: 'Project not found' });
    return;
  }

  const id = parseInt(taskId);
  if (isPipelineRunning(project, id)) {
    res.status(409).json({ error: 'Pipeline already running for this task' });
    return;
  }

  // Run async — don't block the response
  runFullPipeline(project, id).catch(err => {
    console.error(`Pipeline error for ${project}/${taskId}:`, err);
  });

  res.json({ status: 'started' });
});

// POST /api/projects/:project/pipeline/step/:taskId
router.post('/step/:taskId', async (req: Request<Params>, res: Response) => {
  const { project, taskId } = req.params;
  if (!projectExists(project)) {
    res.status(404).json({ error: 'Project not found' });
    return;
  }

  try {
    await runStep(project, parseInt(taskId));
    res.json({ status: 'completed' });
  } catch (err) {
    res.status(400).json({ error: (err as Error).message });
  }
});

// POST /api/projects/:project/pipeline/stop/:taskId
router.post('/stop/:taskId', (req: Request<Params>, res: Response) => {
  const { project, taskId } = req.params;
  if (!projectExists(project)) {
    res.status(404).json({ error: 'Project not found' });
    return;
  }

  stopPipeline(project, parseInt(taskId));
  res.json({ status: 'stopping' });
});

// GET /api/projects/:project/pipeline/status/:taskId
router.get('/status/:taskId', (req: Request<Params>, res: Response) => {
  const { project, taskId } = req.params;
  if (!projectExists(project)) {
    res.status(404).json({ error: 'Project not found' });
    return;
  }

  const status = getPipelineStatus(project, parseInt(taskId));
  res.json(status);
});

export default router;