|
|
|
@@ -9,6 +9,7 @@ const alarmas = require('../engine/alarmas');
|
|
|
|
|
|
|
|
|
|
const apply = require('../engine/apply');
|
|
|
|
|
const P = require('../engine/posicion');
|
|
|
|
|
const { asyncHandler: ah } = require('../http');
|
|
|
|
|
|
|
|
|
|
// Como máximo 2 canales andando a la vez: el reconocimiento y la traducción van a
|
|
|
|
|
// gpu02, que es compartido (076-agregado: ~1 s de whisper por segmento de 6 s).
|
|
|
|
@@ -24,11 +25,11 @@ function fail(res, e) {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
router.get('/', async (req, res) => {
|
|
|
|
|
router.get('/', ah(async (req, res) => {
|
|
|
|
|
const chs = allChannels();
|
|
|
|
|
const st = await status.all(chs);
|
|
|
|
|
res.json({ control: true, max_running: MAX_RUNNING, channels: chs.map(c => ({ ...c, status: st[c.id], busy: busy.has(c.id), alarms: alarmas.activas(c.slug) })) });
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Una operación a la vez por canal, y como mucho 5 arranques en 10 min (el mismo
|
|
|
|
|
// throttle que las unidades systemd; caso TVR-hls de v1: 15k restarts por un loop).
|
|
|
|
@@ -86,14 +87,14 @@ router.get('/:id/frame', (req, res) => {
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
// Lo que muestra "Ver canal": últimos subtítulos por idioma y números.
|
|
|
|
|
router.get('/:id/view', async (req, res) => {
|
|
|
|
|
router.get('/:id/view', ah(async (req, res) => {
|
|
|
|
|
const ch = getChannel(req.params.id);
|
|
|
|
|
if (!ch) return res.status(404).json({ error: 'Canal inexistente' });
|
|
|
|
|
const st = (await status.all([ch]))[ch.id];
|
|
|
|
|
st.metrics = status.metrics(ch, { last: 8 });
|
|
|
|
|
res.json({ channel: ch, status: st, failures: await status.failures(ch),
|
|
|
|
|
alarms: alarmas.activas(ch.slug), alarm_history: alarmas.historial(ch.slug) });
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
router.post('/', (req, res) => {
|
|
|
|
|
let ch;
|
|
|
|
@@ -106,7 +107,7 @@ router.post('/', (req, res) => {
|
|
|
|
|
res.status(201).json(getChannel(r.lastInsertRowid));
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
router.put('/:id', async (req, res) => {
|
|
|
|
|
router.put('/:id', ah(async (req, res) => {
|
|
|
|
|
const old = getChannel(req.params.id);
|
|
|
|
|
if (!old) return res.status(404).json({ error: 'Canal inexistente' });
|
|
|
|
|
let ch;
|
|
|
|
@@ -114,37 +115,39 @@ router.put('/:id', async (req, res) => {
|
|
|
|
|
ch = V.channel(req.body);
|
|
|
|
|
V.conflicts(ch, allChannels().filter(o => o.id !== old.id));
|
|
|
|
|
} catch (e) { return fail(res, e); }
|
|
|
|
|
// Renombrar el id corto con el canal andando dejaría instancias huérfanas con el nombre viejo.
|
|
|
|
|
if (ch.slug !== old.slug) {
|
|
|
|
|
const st = (await status.all([old]))[old.id];
|
|
|
|
|
if (st.units.some(u => u.active === 'active' || u.active === 'activating')) {
|
|
|
|
|
return res.status(409).json({ error: 'Detén el canal antes de cambiarle el id corto', field: 'slug' });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
// Ocupado se toma antes del primer await: si no, un start/stop/delete que entra mientras
|
|
|
|
|
// se mira systemd encontraba el canal libre (revisión externa, 2.2).
|
|
|
|
|
if (busy.has(old.id)) return res.status(409).json({ error: 'El canal está ocupado con otra operación' });
|
|
|
|
|
const save = c => db.prepare("UPDATE channels SET slug = ?, name = ?, enabled = ?, cfg = ?, updated_at = datetime('now') WHERE id = ?")
|
|
|
|
|
.run(c.slug, c.name, c.enabled ? 1 : 0, JSON.stringify(c.cfg), old.id);
|
|
|
|
|
save(ch);
|
|
|
|
|
// Solo cambió la posición del subtítulo (082): se reescribe el .pos.json, que los procesos
|
|
|
|
|
// releen en cada segmento. No se reinicia nada ni se corta el video.
|
|
|
|
|
// Solo nombre, id corto y configuración: la fila de la base trae además id y fechas.
|
|
|
|
|
const sinPos = c => JSON.stringify({ name: c.name, slug: c.slug, cfg: { ...c.cfg, pos: undefined } });
|
|
|
|
|
// Un .env de antes de la 082 no les pasa --pos-file a los procesos: esa vez se reinicia.
|
|
|
|
|
let conPos = false;
|
|
|
|
|
try { conPos = fs.readFileSync(`${process.env.ORBISUBS_ETC || '/etc/orbisubs'}/${old.slug}.env`, 'utf8').includes('--pos-file'); } catch { /* sin .env */ }
|
|
|
|
|
if (conPos && sinPos(ch) === sinPos(old) && JSON.stringify(ch.cfg.pos) !== JSON.stringify(old.cfg.pos) && await isUp(old)) {
|
|
|
|
|
apply.writePos(ch);
|
|
|
|
|
const seg = ch.cfg.hls.seg_s || 6;
|
|
|
|
|
const en_s = Math.round(seg + (apply.holdBack(ch).hb || seg * 3) + 3);
|
|
|
|
|
event('info', `Posición del subtítulo de ${ch.name}: ${P.NOMBRE[ch.cfg.pos.modo]} (${ch.cfg.pos.pct} %)`, { channel: ch.slug, username: req.user.username });
|
|
|
|
|
return res.json({ ...getChannel(old.id), applied: 'pos', applied_in_s: en_s });
|
|
|
|
|
}
|
|
|
|
|
// Andando: se aplica ya. Si no arranca con lo nuevo, vuelve todo (servicios y base)
|
|
|
|
|
// a lo anterior, para no dejar el canal a medias.
|
|
|
|
|
if (await isUp(old)) {
|
|
|
|
|
if (throttled(old.id)) { save(old); return res.status(429).json({ error: 'Demasiados reinicios de este canal: espera unos minutos' }); }
|
|
|
|
|
busy.add(old.id);
|
|
|
|
|
try {
|
|
|
|
|
busy.add(old.id);
|
|
|
|
|
try {
|
|
|
|
|
// Renombrar el id corto con el canal andando dejaría instancias huérfanas con el nombre viejo.
|
|
|
|
|
if (ch.slug !== old.slug) {
|
|
|
|
|
const st = (await status.all([old]))[old.id];
|
|
|
|
|
if (st.units.some(u => u.active === 'active' || u.active === 'activating')) {
|
|
|
|
|
return res.status(409).json({ error: 'Detén el canal antes de cambiarle el id corto', field: 'slug' });
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
const save = c => db.prepare("UPDATE channels SET slug = ?, name = ?, enabled = ?, cfg = ?, updated_at = datetime('now') WHERE id = ?")
|
|
|
|
|
.run(c.slug, c.name, c.enabled ? 1 : 0, JSON.stringify(c.cfg), old.id);
|
|
|
|
|
save(ch);
|
|
|
|
|
// Solo cambió la posición del subtítulo (082): se reescribe el .pos.json, que los procesos
|
|
|
|
|
// releen en cada segmento. No se reinicia nada ni se corta el video.
|
|
|
|
|
// Solo nombre, id corto y configuración: la fila de la base trae además id y fechas.
|
|
|
|
|
const sinPos = c => JSON.stringify({ name: c.name, slug: c.slug, cfg: { ...c.cfg, pos: undefined } });
|
|
|
|
|
// Un .env de antes de la 082 no les pasa --pos-file a los procesos: esa vez se reinicia.
|
|
|
|
|
let conPos = false;
|
|
|
|
|
try { conPos = fs.readFileSync(`${process.env.ORBISUBS_ETC || '/etc/orbisubs'}/${old.slug}.env`, 'utf8').includes('--pos-file'); } catch { /* sin .env */ }
|
|
|
|
|
if (conPos && sinPos(ch) === sinPos(old) && JSON.stringify(ch.cfg.pos) !== JSON.stringify(old.cfg.pos) && await isUp(old)) {
|
|
|
|
|
apply.writePos(ch);
|
|
|
|
|
const seg = ch.cfg.hls.seg_s || 6;
|
|
|
|
|
const en_s = Math.round(seg + (apply.holdBack(ch).hb || seg * 3) + 3);
|
|
|
|
|
event('info', `Posición del subtítulo de ${ch.name}: ${P.NOMBRE[ch.cfg.pos.modo]} (${ch.cfg.pos.pct} %)`, { channel: ch.slug, username: req.user.username });
|
|
|
|
|
return res.json({ ...getChannel(old.id), applied: 'pos', applied_in_s: en_s });
|
|
|
|
|
}
|
|
|
|
|
// Andando: se aplica ya. Si no arranca con lo nuevo, vuelve todo (servicios y base)
|
|
|
|
|
// a lo anterior, para no dejar el canal a medias.
|
|
|
|
|
if (await isUp(old)) {
|
|
|
|
|
if (throttled(old.id)) { save(old); return res.status(429).json({ error: 'Demasiados reinicios de este canal: espera unos minutos' }); }
|
|
|
|
|
const r = await apply.start({ ...ch, id: old.id }, { prevCh: old });
|
|
|
|
|
if (!r.ok) {
|
|
|
|
|
save(old);
|
|
|
|
@@ -152,38 +155,44 @@ router.put('/:id', async (req, res) => {
|
|
|
|
|
return res.status(409).json({ error: `No arrancó con la configuración nueva: ${r.errors.join(' · ')}` });
|
|
|
|
|
}
|
|
|
|
|
for (const w of r.warnings || []) event('warn', `${ch.name}: ${w}`, { channel: ch.slug, username: req.user.username });
|
|
|
|
|
} finally { busy.delete(old.id); }
|
|
|
|
|
event('info', `Canal editado y reiniciado: ${ch.name}`, { channel: ch.slug, username: req.user.username });
|
|
|
|
|
} else {
|
|
|
|
|
event('info', `Canal editado: ${ch.name}`, { channel: ch.slug, username: req.user.username });
|
|
|
|
|
}
|
|
|
|
|
res.json(getChannel(old.id));
|
|
|
|
|
});
|
|
|
|
|
event('info', `Canal editado y reiniciado: ${ch.name}`, { channel: ch.slug, username: req.user.username });
|
|
|
|
|
} else {
|
|
|
|
|
event('info', `Canal editado: ${ch.name}`, { channel: ch.slug, username: req.user.username });
|
|
|
|
|
}
|
|
|
|
|
res.json(getChannel(old.id));
|
|
|
|
|
} finally { busy.delete(old.id); }
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
router.delete('/:id', async (req, res) => {
|
|
|
|
|
router.delete('/:id', ah(async (req, res) => {
|
|
|
|
|
const ch = getChannel(req.params.id);
|
|
|
|
|
if (!ch) return res.status(404).json({ error: 'Canal inexistente' });
|
|
|
|
|
const st = (await status.all([ch]))[ch.id];
|
|
|
|
|
if (st.units.some(u => u.active === 'active' || u.active === 'activating')) {
|
|
|
|
|
return res.status(409).json({ error: 'El canal está andando: detenlo antes de borrarlo' });
|
|
|
|
|
}
|
|
|
|
|
db.prepare('DELETE FROM channels WHERE id = ?').run(ch.id);
|
|
|
|
|
apply.removeEnv(ch.slug);
|
|
|
|
|
event('warn', `Canal borrado: ${ch.name}`, { channel: ch.slug, username: req.user.username });
|
|
|
|
|
res.json({ ok: true });
|
|
|
|
|
});
|
|
|
|
|
// Borrar mientras arranca (start en curso) dejaba las unidades andando sin canal (2.2).
|
|
|
|
|
if (busy.has(ch.id)) return res.status(409).json({ error: 'El canal está ocupado con otra operación' });
|
|
|
|
|
busy.add(ch.id);
|
|
|
|
|
try {
|
|
|
|
|
const st = (await status.all([ch]))[ch.id];
|
|
|
|
|
if (st.units.some(u => u.active === 'active' || u.active === 'activating')) {
|
|
|
|
|
return res.status(409).json({ error: 'El canal está andando: detenlo antes de borrarlo' });
|
|
|
|
|
}
|
|
|
|
|
db.prepare('DELETE FROM channels WHERE id = ?').run(ch.id);
|
|
|
|
|
apply.removeEnv(ch.slug);
|
|
|
|
|
event('warn', `Canal borrado: ${ch.name}`, { channel: ch.slug, username: req.user.username });
|
|
|
|
|
res.json({ ok: true });
|
|
|
|
|
} finally { busy.delete(ch.id); }
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
router.post('/:id/start', async (req, res) => {
|
|
|
|
|
router.post('/:id/start', ah(async (req, res) => {
|
|
|
|
|
const ch = getChannel(req.params.id);
|
|
|
|
|
if (!ch) return res.status(404).json({ error: 'Canal inexistente' });
|
|
|
|
|
if (busy.has(ch.id)) return res.status(409).json({ error: 'El canal está ocupado con otra operación' });
|
|
|
|
|
const others = await runningOthers(ch);
|
|
|
|
|
if (others.length >= MAX_RUNNING) {
|
|
|
|
|
return res.status(409).json({ error: `Ya hay ${others.length} canales andando (${others.join(', ')}): el máximo es ${MAX_RUNNING} por gpu02` });
|
|
|
|
|
}
|
|
|
|
|
if (throttled(ch.id)) return res.status(429).json({ error: 'Demasiados reinicios de este canal: espera unos minutos' });
|
|
|
|
|
// Ocupado antes del primer await (2.2), como en PUT y DELETE.
|
|
|
|
|
busy.add(ch.id);
|
|
|
|
|
try {
|
|
|
|
|
const others = await runningOthers(ch);
|
|
|
|
|
if (others.length >= MAX_RUNNING) {
|
|
|
|
|
return res.status(409).json({ error: `Ya hay ${others.length} canales andando (${others.join(', ')}): el máximo es ${MAX_RUNNING} por gpu02` });
|
|
|
|
|
}
|
|
|
|
|
if (throttled(ch.id)) return res.status(429).json({ error: 'Demasiados reinicios de este canal: espera unos minutos' });
|
|
|
|
|
const r = await apply.start(ch);
|
|
|
|
|
if (!r.ok) {
|
|
|
|
|
event('error', `No arrancó ${ch.name}: ${r.errors.join(' · ')}`, { channel: ch.slug, username: req.user.username });
|
|
|
|
@@ -193,9 +202,9 @@ router.post('/:id/start', async (req, res) => {
|
|
|
|
|
event('info', `Canal iniciado: ${ch.name}`, { channel: ch.slug, username: req.user.username });
|
|
|
|
|
res.json({ ok: true });
|
|
|
|
|
} finally { busy.delete(ch.id); }
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
router.post('/:id/stop', async (req, res) => {
|
|
|
|
|
router.post('/:id/stop', ah(async (req, res) => {
|
|
|
|
|
const ch = getChannel(req.params.id);
|
|
|
|
|
if (!ch) return res.status(404).json({ error: 'Canal inexistente' });
|
|
|
|
|
if (busy.has(ch.id)) return res.status(409).json({ error: 'El canal está ocupado con otra operación' });
|
|
|
|
@@ -203,7 +212,7 @@ router.post('/:id/stop', async (req, res) => {
|
|
|
|
|
try { await apply.stop(ch); } finally { busy.delete(ch.id); }
|
|
|
|
|
event('info', `Canal parado: ${ch.name}`, { channel: ch.slug, username: req.user.username });
|
|
|
|
|
res.json({ ok: true });
|
|
|
|
|
});
|
|
|
|
|
}));
|
|
|
|
|
|
|
|
|
|
// Base nueva: T13 (el canal de la 076-078, configurado a mano) entra como un canal
|
|
|
|
|
// más, con lo que dice su /etc/orbisubs/t13.env. Solo se lee.
|
|
|
|
|