feat(SmartExecution): Add support for scheduling restarts to SmartExecution

This commit is contained in:
Philipp Kunz 2024-12-09 02:52:14 +01:00
parent b854715940
commit 522fbfc42c
3 changed files with 47 additions and 10 deletions

View File

@ -1,5 +1,11 @@
# Changelog # Changelog
## 2024-12-09 - 3.2.0 - feat(SmartExecution)
Add support for scheduling restarts to SmartExecution
- Introduced the ability to handle consecutive restarts efficiently in SmartExecution.
- Ensures that multiple restart requests merge into a single additional restart request if one is already in progress.
## 2024-12-09 - 3.1.0 - feat(core) ## 2024-12-09 - 3.1.0 - feat(core)
Refactor codebase and update dependencies. Refactor codebase and update dependencies.

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@push.rocks/smartshell', name: '@push.rocks/smartshell',
version: '3.1.0', version: '3.2.0',
description: 'A library for executing shell commands using promises.' description: 'A library for executing shell commands using promises.'
} }

View File

@ -1,24 +1,55 @@
import * as plugins from './plugins.js' import * as plugins from './plugins.js';
import { Smartshell, type IExecResultStreaming } from './classes.smartshell.js'; import { Smartshell, type IExecResultStreaming } from './classes.smartshell.js';
export interface IDeferred<T> {
resolve: (value?: T | PromiseLike<T>) => void;
reject: (reason?: any) => void;
promise: Promise<T>;
}
export class SmartExecution { export class SmartExecution {
public smartshell: Smartshell; public smartshell: Smartshell;
public currentStreamingExecution: IExecResultStreaming; public currentStreamingExecution: IExecResultStreaming;
public commandString: string; public commandString: string;
private isRestartInProgress = false;
private isAnotherRestartRequested = false;
constructor(commandStringArg: string) { constructor(commandStringArg: string) {
this.commandString = commandStringArg; this.commandString = commandStringArg;
} }
public async restart() { /**
if (!this.smartshell) { * Schedules a restart. If a restart is currently in progress, any additional calls
this.smartshell = new Smartshell({ * to restart will merge into a single additional restart request, which will only execute
executor: 'bash', * 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;
} }
if (this.currentStreamingExecution) {
await this.currentStreamingExecution.kill(); 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();
} }
this.currentStreamingExecution = await this.smartshell.execStreaming(this.commandString);
} }
} }