Skip to content

Commit

Permalink
Improved error handling
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-centore committed May 14, 2024
1 parent e474326 commit c935c75
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 30 deletions.
42 changes: 41 additions & 1 deletion src/main/gitlib/terminal.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { BrowserWindow } from 'electron';
import { isNil, omitBy, pickBy } from 'lodash';
import * as pty from 'node-pty';
import { Command } from '../../types/types';
import { sleep } from '../main-util';

let ptyProcess: pty.IPty | null = null;

Expand All @@ -16,7 +17,7 @@ export const escapeShellArg = (arg: string) => {
return `'${arg.replace(/'/g, `'\\''`)}'`;
};

export const spawnTerminal = async ({
const spawnTerminalHelper = async ({
command: { cmd, args },
dir,
mainWindow,
Expand Down Expand Up @@ -73,6 +74,45 @@ export const spawnTerminal = async ({
});
};

export const spawnTerminal = async ({
command,
dir,
mainWindow,
}: {
command: Command;
dir: string;
mainWindow: BrowserWindow;
}): Promise<{ out?: string; returnCode: number }> => {
let retries = 3;
let result: { out?: string; returnCode: number } | null = null;
while (retries > 0) {
// eslint-disable-next-line no-await-in-loop
result = await spawnTerminalHelper({
command,
dir,
mainWindow,
});

if (result.returnCode === 0) {
return result;
}

await sleep(300);

if (
result.out?.includes(
'Another git process seems to be running in this repository',
)
) {
console.log('Lock exists, retrying...');
} else {
console.log(`Error, retrying... (${retries} remaining`);
retries--;
}
}
return result ?? { returnCode: -1 };
};

export const terminalIn = (str: string) => {
ptyProcess?.write(str);
};
Expand Down
37 changes: 8 additions & 29 deletions src/main/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -123,35 +123,14 @@ ipcMain.handle('run-cmds', async (_event, data) => {
}

for (const command of data as Command[]) {
let retries = 5;
while (retries > 0) {
// eslint-disable-next-line no-await-in-loop
const result = await spawnTerminal({
command,
dir,
mainWindow,
});
console.log({ result });
if (result.returnCode === 0) {
break;
} else {
// eslint-disable-next-line no-lonely-if
if (
result.out?.includes(
'Another git process seems to be running in this repository',
)
) {
retries--;
console.log(
`Lock exists, retrying... (${retries} remaining)`,
);
// Retry if a lock causes the git process to fail
// eslint-disable-next-line no-await-in-loop
await sleep(300);
} else {
return result.returnCode;
}
}
// eslint-disable-next-line no-await-in-loop
const result = await spawnTerminal({
command,
dir,
mainWindow,
});
if (result.returnCode !== 0) {
return result.returnCode;
}

// eslint-disable-next-line no-await-in-loop
Expand Down

0 comments on commit c935c75

Please sign in to comment.