smartdaemon/ts/smartdaemon.classes.service.ts

83 lines
2.0 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';
export interface SmartDaemonServiceConstructorOptions {
name: string;
2019-09-03 17:58:08 +00:00
description: string;
2019-09-03 13:21:30 +00:00
command: string;
workingDir: string;
}
/**
* represents a service that is being spawned by SmartDaemon
*/
export class SmartDaemonService implements SmartDaemonServiceConstructorOptions {
public static async createFromOptions(smartdaemonRef: SmartDaemon, optionsArg: SmartDaemonServiceConstructorOptions) {
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
}
public options: SmartDaemonServiceConstructorOptions;
public name: string;
2019-09-03 17:58:08 +00:00
public description: string;
2019-09-03 13:21:30 +00:00
public command: string;
public workingDir: string;
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() {
await this.smartdaemonRef.systemdManager.saveService(this.name, this.smartdaemonRef.templateManager.generateServiceTemplate({
command: this.command,
description: this.description,
pathWorkkingDir: this.workingDir,
serviceName: this.name,
serviceVersion: 'x.x.x'
}));
}
2019-09-03 14:50:24 +00:00
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
}