2024-12-09 01:52:14 +00:00
|
|
|
import * as plugins from './plugins.js';
|
2024-12-09 01:39:31 +00:00
|
|
|
import { Smartshell, type IExecResultStreaming } from './classes.smartshell.js';
|
|
|
|
|
2024-12-09 01:52:14 +00:00
|
|
|
export interface IDeferred<T> {
|
|
|
|
resolve: (value?: T | PromiseLike<T>) => void;
|
|
|
|
reject: (reason?: any) => void;
|
|
|
|
promise: Promise<T>;
|
|
|
|
}
|
|
|
|
|
2024-12-09 01:39:31 +00:00
|
|
|
export class SmartExecution {
|
|
|
|
public smartshell: Smartshell;
|
|
|
|
public currentStreamingExecution: IExecResultStreaming;
|
|
|
|
public commandString: string;
|
|
|
|
|
2024-12-09 01:52:14 +00:00
|
|
|
private isRestartInProgress = false;
|
|
|
|
private isAnotherRestartRequested = false;
|
|
|
|
|
2024-12-09 01:39:31 +00:00
|
|
|
constructor(commandStringArg: string) {
|
|
|
|
this.commandString = commandStringArg;
|
|
|
|
}
|
|
|
|
|
2024-12-09 01:52:14 +00:00
|
|
|
/**
|
|
|
|
* Schedules a restart. If a restart is currently in progress, any additional calls
|
|
|
|
* to restart will merge into a single additional restart request, which will only execute
|
|
|
|
* once the current restart completes.
|
|
|
|
*/
|
|
|
|
public async restart(): Promise<void> {
|
|
|
|
if (this.isRestartInProgress) {
|
|
|
|
// If there's already a restart in progress, just mark that another restart was requested
|
|
|
|
this.isAnotherRestartRequested = true;
|
|
|
|
return;
|
2024-12-09 01:39:31 +00:00
|
|
|
}
|
2024-12-09 01:52:14 +00:00
|
|
|
|
|
|
|
this.isRestartInProgress = true;
|
|
|
|
try {
|
|
|
|
if (!this.smartshell) {
|
|
|
|
this.smartshell = new Smartshell({
|
|
|
|
executor: 'bash',
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (this.currentStreamingExecution) {
|
|
|
|
await this.currentStreamingExecution.kill();
|
|
|
|
}
|
|
|
|
this.currentStreamingExecution = await this.smartshell.execStreaming(this.commandString);
|
|
|
|
} finally {
|
|
|
|
this.isRestartInProgress = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If another restart was requested while we were busy, we handle it now
|
|
|
|
if (this.isAnotherRestartRequested) {
|
|
|
|
this.isAnotherRestartRequested = false;
|
|
|
|
await this.restart();
|
2024-12-09 01:39:31 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|