corerender/ts/rendertron.taskmanager.ts

61 lines
2.1 KiB
TypeScript
Raw Permalink Normal View History

2025-01-01 07:33:33 +01:00
import { logger } from './rendertron.logging.js';
import { Rendertron } from './rendertron.classes.rendertron.js';
import * as plugins from './rendertron.plugins.js';
export class TaskManager {
rendertronRef: Rendertron;
public taskmanager: plugins.taskbuffer.TaskManager;
constructor(rendertronRefArg: Rendertron) {
this.rendertronRef = rendertronRefArg;
this.taskmanager = new plugins.taskbuffer.TaskManager();
this.taskmanager.addAndScheduleTask(
new plugins.taskbuffer.Task({
name: 'prerenderLocalDomains',
taskFunction: async () => {
logger.log('info', `starting domain prerender in 5 seconds`);
await plugins.smartdelay.delayFor(5000);
// get projects from lele-pubapiclient
const localDomains = []; // TODO: get from coreflow
for (const project of localDomains) {
logger.log('info', `Prerending project ${project.name} with url ${project.url}`);
const startTime = Date.now();
await this.rendertronRef.prerenderManager.prerenderDomain(
project.url.replace('https://', '')
);
logger.log(
'info',
`Prerended project ${project.name} with url ${project.url} in ${
Date.now() - startTime
}ms`
);
}
},
}),
'0 */30 * * * *'
);
this.taskmanager.addAndScheduleTask(
new plugins.taskbuffer.Task({
name: 'CleanupPrerenderResults',
taskFunction: async () => {
logger.log('info', `starting to delete old PrerenderResults in 5 seconds`);
await plugins.smartdelay.delayFor(2000);
await this.rendertronRef.prerenderManager.cleanPrerenderResults();
logger.log('success', `cleaned old prerender results`);
},
}),
'0 0 1 * * *'
);
}
public async start() {
this.taskmanager.start();
logger.log('info', 'triggering initial prerender task outside of schedule');
this.taskmanager.triggerTaskByName('prerenderLocalDomains');
}
public async stop() {
this.taskmanager.stop();
}
}