smartdaemon/ts/smartdaemon.classes.service.ts

81 lines
1.9 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-05 11:50:11 +00:00
public alreadyExists = false;
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-05 11:50:11 +00:00
await this.smartdaemonRef.systemdManager.enableService(this);
2019-09-03 13:21:30 +00:00
}
/**
* disables the service
*/
public async disable() {
2019-09-05 11:50:11 +00:00
await this.smartdaemonRef.systemdManager.disableService(this);
2019-09-03 17:58:08 +00:00
}
2019-09-03 13:21:30 +00:00
2019-09-03 17:58:08 +00:00
/**
* starts a service
*/
public async start() {
2019-09-05 11:50:11 +00:00
await this.smartdaemonRef.systemdManager.startService(this);
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() {
2019-09-05 11:50:11 +00:00
await this.smartdaemonRef.systemdManager.stopService(this);
2019-09-03 17:58:08 +00:00
}
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 11:50:11 +00:00
await this.smartdaemonRef.systemdManager.saveService(this);
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() {
2019-09-05 11:50:11 +00:00
await this.smartdaemonRef.systemdManager.deleteService(this);
2019-09-03 14:50:24 +00:00
}
2019-09-03 13:21:30 +00:00
}