104 lines
2.6 KiB
TypeScript
104 lines
2.6 KiB
TypeScript
import * as plugins from './plugins.js';
|
|
import * as paths from '../paths.js';
|
|
|
|
/**
|
|
* Manages TSPM daemon as a systemd service via smartdaemon
|
|
*/
|
|
export class TspmServiceManager {
|
|
private smartDaemon: plugins.smartdaemon.SmartDaemon;
|
|
private service: any = null; // SmartDaemonService type is not exported
|
|
|
|
constructor() {
|
|
this.smartDaemon = new plugins.smartdaemon.SmartDaemon();
|
|
}
|
|
|
|
/**
|
|
* Get or create the TSPM daemon service configuration
|
|
*/
|
|
private async getOrCreateService(): Promise<any> {
|
|
if (!this.service) {
|
|
const cliPath = plugins.path.join(paths.packageDir, 'cli.js');
|
|
|
|
// Create service configuration
|
|
this.service = await this.smartDaemon.addService({
|
|
name: 'tspm-daemon',
|
|
description: 'TSPM Process Manager Daemon',
|
|
command: `${process.execPath} ${cliPath} daemon start-service`,
|
|
workingDir: process.env.HOME || process.cwd(),
|
|
version: '1.0.0',
|
|
});
|
|
}
|
|
return this.service;
|
|
}
|
|
|
|
/**
|
|
* Enable the TSPM daemon as a system service
|
|
*/
|
|
public async enableService(): Promise<void> {
|
|
const service = await this.getOrCreateService();
|
|
|
|
// Save service configuration
|
|
await service.save();
|
|
|
|
// Enable service to start on boot
|
|
await service.enable();
|
|
|
|
// Start the service immediately
|
|
await service.start();
|
|
}
|
|
|
|
/**
|
|
* Disable the TSPM daemon service
|
|
*/
|
|
public async disableService(): Promise<void> {
|
|
const service = await this.getOrCreateService();
|
|
|
|
// Stop the service if running
|
|
try {
|
|
await service.stop();
|
|
} catch (error) {
|
|
// Service might not be running
|
|
console.log('Service was not running');
|
|
}
|
|
|
|
// Disable service from starting on boot
|
|
await service.disable();
|
|
}
|
|
|
|
/**
|
|
* Get the current status of the systemd service
|
|
*/
|
|
public async getServiceStatus(): Promise<{
|
|
enabled: boolean;
|
|
running: boolean;
|
|
status: string;
|
|
}> {
|
|
try {
|
|
await this.getOrCreateService();
|
|
|
|
// Note: SmartDaemon doesn't provide direct status methods,
|
|
// so we'll need to check via systemctl commands
|
|
// This is a simplified implementation
|
|
return {
|
|
enabled: true, // Would need to check systemctl is-enabled
|
|
running: true, // Would need to check systemctl is-active
|
|
status: 'active',
|
|
};
|
|
} catch (error) {
|
|
return {
|
|
enabled: false,
|
|
running: false,
|
|
status: 'inactive',
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Reload the systemd service configuration
|
|
*/
|
|
public async reloadService(): Promise<void> {
|
|
const service = await this.getOrCreateService();
|
|
await service.reload();
|
|
}
|
|
}
|