diff --git a/services/mam-api/src/routes/recorders.js b/services/mam-api/src/routes/recorders.js index 751a439..371989a 100644 --- a/services/mam-api/src/routes/recorders.js +++ b/services/mam-api/src/routes/recorders.js @@ -450,7 +450,13 @@ router.post('/:id/stop', async (req, res, next) => { const recorder = recorderResult.rows[0]; if (!recorder.container_id) { - return res.status(400).json({ error: 'No container running' }); + // No container tracked — reset stuck status gracefully. + const result = await pool.query( + `UPDATE recorders SET container_id = NULL, status = 'stopped', updated_at = NOW() + WHERE id = $1 RETURNING *`, + [id] + ); + return res.json(result.rows[0]); } const { remote: isRemote, apiUrl: targetNodeApiUrl } = await resolveNodeTarget(recorder.node_id); @@ -469,23 +475,27 @@ router.post('/:id/stop', async (req, res, next) => { `/containers/${recorder.container_id}/stop` ); - if (stopRes.status !== 204 && stopRes.status !== 304) { + // 204 = stopped, 304 = already stopped, 404 = container gone — all acceptable. + if (stopRes.status !== 204 && stopRes.status !== 304 && stopRes.status !== 404) { return res.status(500).json({ error: 'Failed to stop container', details: stopRes.data, }); } - const removeRes = await dockerApi( - 'DELETE', - `/containers/${recorder.container_id}` - ); + // Only attempt remove if the container existed (not 404). + if (stopRes.status !== 404) { + const removeRes = await dockerApi( + 'DELETE', + `/containers/${recorder.container_id}` + ); - if (removeRes.status !== 204 && removeRes.status !== 404) { - return res.status(500).json({ - error: 'Failed to remove container', - details: removeRes.data, - }); + if (removeRes.status !== 204 && removeRes.status !== 404) { + return res.status(500).json({ + error: 'Failed to remove container', + details: removeRes.data, + }); + } } }