Skip to content

Commit

Permalink
fix: wait for start commands to run to completion at startup
Browse files Browse the repository at this point in the history
This fixes a race condition that could make the server crash on startup
in some cases.
  • Loading branch information
TBubba committed Jan 24, 2020
1 parent bee69b6 commit 00775d7
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions src/back/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ async function onProcessMessage(message: any, sendHandle: any): Promise<void> {
if (state.serviceInfo) {
// Run start commands
for (let i = 0; i < state.serviceInfo.start.length; i++) {
await execProcess(state.serviceInfo.start[i]);
execProcessSync(state.serviceInfo.start[i]);
}
// Run processes
if (state.serviceInfo.server) {
Expand Down Expand Up @@ -1539,7 +1539,7 @@ function exit() {
}
// Run stop commands
for (let i = 0; i < state.serviceInfo.stop.length; i++) {
execProcess(state.serviceInfo.stop[i], true);
execProcessSync(state.serviceInfo.stop[i]);
}
}

Expand Down Expand Up @@ -1686,15 +1686,18 @@ function searchGames(opts: SearchGamesOpts): IGameInfo[] {
return foundGames;
}

async function execProcess(proc: IBackProcessInfo, sync?: boolean): Promise<void> {
/**
* Execute a back process synchronously (wait for it to exit).
* @param proc Back process to run.
*/
function execProcessSync(proc: IBackProcessInfo): void {
const cwd: string = path.join(state.config.flashpointPath, proc.path);
log({
source: servicesSource,
content: `Executing "${proc.filename}" ${stringifyArray(proc.arguments)} in "${proc.path}"`
});
try {
if (sync) { child_process.execFileSync( proc.filename, proc.arguments, { cwd: cwd }); }
else { await child_process.execFile(proc.filename, proc.arguments, { cwd: cwd }); }
child_process.execFileSync(proc.filename, proc.arguments, { cwd: cwd });
} catch (error) {
log({
source: servicesSource,
Expand Down

0 comments on commit 00775d7

Please sign in to comment.