Files
spark/ts/spark.classes.taskmanager.ts
T

99 lines
3.2 KiB
TypeScript

import * as plugins from './spark.plugins.ts';
import { Spark } from './index.ts';
import { logger } from './spark.logging.ts';
export class SparkTaskManager {
public sparkRef: Spark;
public taskmanager: plugins.taskbuffer.TaskManager;
// tasks
public updateSpark: plugins.taskbuffer.Task;
public updateHost: plugins.taskbuffer.Task;
public updateServices: plugins.taskbuffer.Task;
constructor(sparkRefArg: Spark) {
this.sparkRef = sparkRefArg;
this.taskmanager = new plugins.taskbuffer.TaskManager();
// updateSpark
this.updateSpark = new plugins.taskbuffer.Task({
name: 'updateSpark',
taskFunction: async () => {
const smartupdateInstance = new plugins.smartupdate.SmartUpdate({
npmRegistryOptions: {
npmRegistryUrl: 'https://registry.npmjs.org',
},
});
const shouldUpdate = await smartupdateInstance.check(
this.sparkRef.sparkInfo.projectInfo.name,
this.sparkRef.sparkInfo.projectInfo.version
);
if (shouldUpdate) {
this.stop();
const smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash',
});
await smartshellInstance.exec(
`cd / && pnpm add --global @serve.zone/spark@latest && spark updatedaemon`,
);
logger.log('info', 'Cooling off before restart...');
await plugins.smartdelay.delayFor(5000);
logger.log('ok', '######## Trying to exit / Restart expected... ########');
Deno.exit(0);
}
},
});
this.updateHost = new plugins.taskbuffer.Task({
name: 'updateHost',
taskFunction: async () => {
this.stop();
const smartshellInstance = new plugins.smartshell.Smartshell({
executor: 'bash',
});
await smartshellInstance.exec(
`apt-get update && apt-get upgrade -y --force-yes && apt-get autoremove -y --force-yes && apt-get autoclean -y --force-yes`
);
await this.start();
},
});
/**
* only being run when mode is cloudly
*/
this.updateServices = new plugins.taskbuffer.Task({
name: 'updateServices',
taskFunction: async () => {
logger.log('info', 'now running updateServices task');
await this.sparkRef.sparkUpdateManager.updateServices();
},
});
}
/**
* start the taskmanager
*/
public async start() {
this.taskmanager.addAndScheduleTask(this.updateServices, '30 */2 * * * *');
this.taskmanager.addAndScheduleTask(this.updateSpark, '0 * * * * *');
const enableHostUpdates = await this.sparkRef.sparkConfig.kvStore.readKey('enableHostUpdates');
if (enableHostUpdates === 'true') {
this.taskmanager.addAndScheduleTask(this.updateHost, '0 0 0 * * *');
} else {
logger.log('info', 'host package updates are disabled. Pass --enableHostUpdates=true to enable them.');
}
this.taskmanager.start();
}
/**
* stops the taskmanager
*/
public stop() {
this.taskmanager.descheduleTask(this.updateSpark);
this.taskmanager.descheduleTask(this.updateHost);
this.taskmanager.descheduleTask(this.updateServices);
this.taskmanager.stop();
}
}