fp/services/worker/src/index.ts
CJ_Clippy 87b054e66f
Some checks failed
ci / test (push) Failing after 4m45s
fp/our CI/CD / build (push) Successful in 58s
restrict playback to logged in visitors
2025-11-19 17:46:35 -08:00

117 lines
3.4 KiB
TypeScript

import { createBullBoard } from '@bull-board/api';
import { BullMQAdapter } from '@bull-board/api/bullMQAdapter';
import { ExpressAdapter } from '@bull-board/express';
import { type JobsOptions } from 'bullmq';
import express, { type Request, type Response } from 'express';
import { generalQueue } from './queues/generalQueue.ts';
import { gpuQueue } from './queues/gpuQueue.ts';
import { highPriorityQueue } from './queues/highPriorityQueue.ts';
import env from '../.config/env.ts';
import { version } from '../package.json';
const run = async () => {
const app = express();
const serverAdapter = new ExpressAdapter();
serverAdapter.setBasePath('/ui');
createBullBoard({
queues: [
new BullMQAdapter(highPriorityQueue),
new BullMQAdapter(generalQueue),
new BullMQAdapter(gpuQueue),
],
serverAdapter,
});
console.log('importing workers');
if (process.env.NODE_ENV === 'development') {
await import('./workers/highPriorityWorker.ts');
await import('./workers/generalWorker.ts');
await import('./workers/gpuWorker.ts');
} else {
// @todo I separated these so that they can be ran on multiple machines.
// @todo we should activate these based on environment variable feature flags, not NODE_ENV
await import('./workers/highPriorityWorker.ts');
await import('./workers/generalWorker.ts');
// await import('./workers/gpuWorker.ts'); // @todo implement
}
app.get('/', (req: Request, res: Response) => {
res.send(`
<style>
html {
color-scheme: light dark;
}
body {
font-family: system-ui;
font-size: 1.25rem;
line-height: 1.5;
}
img,
svg,
video {
max-width: 100%;
display: block;
}
main {
max-width: min(70ch, 100% - 4rem);
margin-inline: auto;
}
</style>
<h1>FP Worker version ${version}.</h1>
<h2>Actions</h2>
<p><i>If you find yourself clicking one of these links a lot, consider automating it by putting the job in a queue on a every or pattern timer.</i></p>
<ul>
<li><a href="/ui">Bull Dashboard</a></li>
<li><a href="/task?name=presignMuxAssets">Task: presignMuxAssets</a></li>
<li><a href="/task?name=copyV1VideoAll">Task: copyV1VideoAll</a></li>
<li><a href="/task?name=createTorrent&vodId=1234">Task: createTorrent</a></li>
<li><a href="/task?name=createMuxAsset&vodId=">Task: createMuxAsset</a></li>
</ul>
`)
})
app.use('/ui', serverAdapter.getRouter());
app.get('/task', async (req: Request, res: Response) => {
const name = req.query.name as string;
const vodId = req.query.vodId as string;
// console.log('vodId', vodId, 'name', name);
// console.log(JSON.stringify(req.query, null, 2))
const data = { vodId };
switch (name) {
case '':
throw new Error('job name was missing');
case 'scheduleVodProcessing':
await gpuQueue.add(name, data);
break;
default:
await highPriorityQueue.add(name, data);
break;
}
res.json({ ok: true });
});
app.listen(env.WORKER_PORT, () => {
console.log(`Bull Dashboard running at http://localhost:${env.WORKER_PORT}/ui`);
console.log(`Actions Menu: http://localhost:${env.WORKER_PORT}`);
});
};
run().catch((e) => console.error(e));