smartdaemon/ts/smartdaemon.classes.service.ts

88 lines
2.2 KiB
TypeScript
Raw Normal View History

2019-09-03 13:21:30 +00:00
import * as plugins from './smartdaemon.plugins';
import * as paths from './smartdaemon.paths';
import { SmartDaemon } from './smartdaemon.classes.smartdaemon';
2019-09-05 09:15:17 +00:00
export interface ISmartDaemonServiceConstructorOptions {
2019-09-03 13:21:30 +00:00
name: string;
2019-09-03 17:58:08 +00:00
description: string;
2019-09-03 13:21:30 +00:00
command: string;
workingDir: string;
2019-09-05 09:15:17 +00:00
version: string;
2019-09-03 13:21:30 +00:00
}
/**
* represents a service that is being spawned by SmartDaemon
*/
2019-09-05 09:15:17 +00:00
export class SmartDaemonService implements ISmartDaemonServiceConstructorOptions {
public static async createFromOptions(smartdaemonRef: SmartDaemon, optionsArg: ISmartDaemonServiceConstructorOptions) {
2019-09-03 13:21:30 +00:00
const service = new SmartDaemonService(smartdaemonRef);
for (const key of Object.keys(optionsArg)) {
service[key] = optionsArg[key];
}
2019-09-03 14:50:24 +00:00
return service;
2019-09-03 13:21:30 +00:00
}
2019-09-05 09:15:17 +00:00
public options: ISmartDaemonServiceConstructorOptions;
2019-09-03 13:21:30 +00:00
public name: string;
2019-09-05 09:15:17 +00:00
public version: string;
2019-09-03 13:21:30 +00:00
public command: string;
public workingDir: string;
2019-09-05 09:15:17 +00:00
public description: string;
2019-09-03 13:21:30 +00:00
public smartdaemonRef: SmartDaemon;
constructor(smartdaemonRegfArg: SmartDaemon) {
this.smartdaemonRef = smartdaemonRegfArg;
}
/**
* enables the service
*/
public async enable() {
2019-09-03 17:58:08 +00:00
await this.save();
await this.smartdaemonRef.systemdManager.enableService(this.name);
2019-09-03 13:21:30 +00:00
}
/**
* disables the service
*/
public async disable() {
2019-09-03 17:58:08 +00:00
await this.smartdaemonRef.systemdManager.disableService(this.name);
}
2019-09-03 13:21:30 +00:00
2019-09-03 17:58:08 +00:00
/**
* starts a service
*/
public async start() {
await this.smartdaemonRef.systemdManager.startService(this.name);
2019-09-03 13:21:30 +00:00
}
/**
2019-09-03 17:58:08 +00:00
* stops a service
2019-09-03 13:21:30 +00:00
*/
2019-09-03 17:58:08 +00:00
public async stop() {
await this.smartdaemonRef.systemdManager.stopService(this.name);
}
2019-09-03 14:50:24 +00:00
2019-09-03 17:58:08 +00:00
// Save and Delete
public async save() {
2019-09-05 09:15:17 +00:00
const serviceTemplate = this.smartdaemonRef.templateManager.generateServiceTemplate({
name: this.name,
version: this.version,
2019-09-03 17:58:08 +00:00
command: this.command,
2019-09-05 09:15:17 +00:00
workkingDir: this.workingDir,
2019-09-03 17:58:08 +00:00
description: this.description,
2019-09-05 09:15:17 +00:00
});
await this.smartdaemonRef.systemdManager.saveService(this.name, serviceTemplate);
2019-09-03 17:58:08 +00:00
}
2019-09-03 14:50:24 +00:00
2019-09-05 09:15:17 +00:00
/**
* deletes the service
*/
2019-09-03 17:58:08 +00:00
public async delete() {
await this.smartdaemonRef.systemdManager.deleteService(this.name);
2019-09-03 14:50:24 +00:00
}
2019-09-03 13:21:30 +00:00
}