coreflow/ts/coreflow.classes.taskmanager.ts
2024-05-09 00:05:16 +02:00

95 lines
3.1 KiB
TypeScript

import * as plugins from './coreflow.plugins.js';
import { Coreflow } from './coreflow.classes.coreflow.js';
import { logger } from './coreflow.logging.js';
export class CoreflowTaskmanager {
public coreflowRef: Coreflow;
public taskmanager: plugins.taskbuffer.TaskManager;
// checkin tasks
public checkinTask: plugins.taskbuffer.Task;
// event based tasks
/**
* updates baseservices
* namely: coretraffic
*/
public updateBaseServicesTask: plugins.taskbuffer.Task;
public updateWorkloadServicesTask: plugins.taskbuffer.Task;
// timed
public updateTrafficRoutingTask: plugins.taskbuffer.Task;
public updateConfigTask: plugins.taskbuffer.Task;
constructor(coreflowRefArg: Coreflow) {
this.coreflowRef = coreflowRefArg;
this.taskmanager = new plugins.taskbuffer.TaskManager();
this.updateBaseServicesTask = new plugins.taskbuffer.Task({
name: 'updateBaseServices',
buffered: true,
bufferMax: 1,
taskFunction: async () => {
logger.log('info', 'running provisioning task for base services');
await this.coreflowRef.clusterManager.provisionBaseServices();
logger.log('success', 'provisioning task for base services completed!');
},
afterTask: () => {
return this.updateWorkloadServicesTask;
},
});
this.updateWorkloadServicesTask = new plugins.taskbuffer.Task({
name: 'updateWorkloadServices',
buffered: true,
bufferMax: 1,
taskFunction: async () => {
logger.log('info', 'now updating workloadServices');
const config = await this.coreflowRef.cloudlyConnector.getConfigFromCloudly();
await this.coreflowRef.clusterManager.provisionWorkloadServices(config);
logger.log('success', 'provisioning task for workload services completed!');
},
afterTask: () => {
return this.updateTrafficRoutingTask;
},
});
this.updateTrafficRoutingTask = new plugins.taskbuffer.Task({
name: 'updateTrafficRouting',
buffered: true,
bufferMax: 1,
taskFunction: async () => {
logger.log('info', 'now updating traffic routing');
const config = await this.coreflowRef.cloudlyConnector.getConfigFromCloudly();
await this.coreflowRef.clusterManager.updateTrafficRouting(config);
logger.log('success', 'traffic routing completed!');
},
});
}
/**
* starts the task manager
*/
public async start() {
logger.log('info', 'starting task manager async in 10 seconds (unrefed)');
plugins.smartdelay.delayFor(10000, null, true).then(async () => {
try {
await this.updateBaseServicesTask.trigger();
logger.log(
'success',
'initial tasks successfully executed! Now handing over to longterm taskmanager!'
);
} catch (e) {
console.log(e);
}
this.taskmanager.addAndScheduleTask(this.updateBaseServicesTask, '0 0 * * * *');
// note: workload services are updated after the fact
this.taskmanager.start();
});
}
public async stop() {
this.taskmanager.stop();
}
}