SIGINT handler to not notify about error on Ctrl-C

This commit is contained in:
Ralf Vogler 2023-02-22 00:15:03 +01:00
parent c1cd2db721
commit 66694d65e5
3 changed files with 24 additions and 6 deletions

View file

@ -13,6 +13,12 @@ console.log(datetime(), 'started checking epic-games');
const db = await jsonDb('epic-games.json'); const db = await jsonDb('epic-games.json');
db.data ||= {}; db.data ||= {};
let exit = false;
process.on('SIGINT', () => { // e.g. when killed by Ctrl-C
console.log('\nInterrupted by SIGINT. Exit! Exception shows where the script was:\n');
exit = true;
});
// https://www.nopecha.com extension source from https://github.com/NopeCHA/NopeCHA/releases/tag/0.1.16 // https://www.nopecha.com extension source from https://github.com/NopeCHA/NopeCHA/releases/tag/0.1.16
// const ext = path.resolve('nopecha'); // used in Chromium, currently not needed in Firefox // const ext = path.resolve('nopecha'); // used in Chromium, currently not needed in Firefox
@ -183,13 +189,13 @@ try {
} catch (error) { } catch (error) {
console.error(error); // .toString()? console.error(error); // .toString()?
process.exitCode = 1; process.exitCode = 1;
if (error.message && !error.message.includes('Target closed')) // e.g. when killed by Ctrl-C if (error.message && !exit)
notify(`epic-games failed: ${error.message.split('\n')[0]}`); notify(`epic-games failed: ${error.message.split('\n')[0]}`);
} finally { } finally {
await db.write(); // write out json db await db.write(); // write out json db
if (notify_games.filter(g => g.status != 'existed').length) { // don't notify if all were already claimed; TODO don't notify if killed? if (notify_games.filter(g => g.status != 'existed').length) { // don't notify if all were already claimed
notify(`epic-games:<br>${html_game_list(notify_games)}`); notify(`epic-games:<br>${html_game_list(notify_games)}`);
} }
} }
writeFileSync(path.resolve(cfg.dir.browser, 'cookies.json'), JSON.stringify(await context.cookies())); if (cfg.debug) writeFileSync(path.resolve(cfg.dir.browser, 'cookies.json'), JSON.stringify(await context.cookies()));
await context.close(); await context.close();

10
gog.js
View file

@ -10,6 +10,12 @@ console.log(datetime(), 'started checking gog');
const db = await jsonDb('gog.json'); const db = await jsonDb('gog.json');
db.data ||= {}; db.data ||= {};
let exit = false;
process.on('SIGINT', () => { // e.g. when killed by Ctrl-C
console.log('\nInterrupted by SIGINT. Exit! Exception shows where the script was:\n');
exit = true;
});
// https://playwright.dev/docs/auth#multi-factor-authentication // https://playwright.dev/docs/auth#multi-factor-authentication
const context = await firefox.launchPersistentContext(cfg.dir.browser, { const context = await firefox.launchPersistentContext(cfg.dir.browser, {
headless: cfg.headless, headless: cfg.headless,
@ -132,11 +138,11 @@ try {
} catch (error) { } catch (error) {
console.error(error); // .toString()? console.error(error); // .toString()?
process.exitCode = 1; process.exitCode = 1;
if (error.message && !error.message.includes('Target closed') && !error.message.includes('Browser closed')) // e.g. when killed by Ctrl-C if (error.message && !exit)
notify(`gog failed: ${error.message.split('\n')[0]}`); notify(`gog failed: ${error.message.split('\n')[0]}`);
} finally { } finally {
await db.write(); // write out json db await db.write(); // write out json db
if (notify_games.filter(g => g.status != 'existed').length) { // don't notify if all were already claimed; TODO don't notify if killed? if (notify_games.filter(g => g.status != 'existed').length) { // don't notify if all were already claimed
notify(`gog:<br>${html_game_list(notify_games)}`); notify(`gog:<br>${html_game_list(notify_games)}`);
} }
} }

View file

@ -12,6 +12,12 @@ console.log(datetime(), 'started checking prime-gaming');
const db = await jsonDb('prime-gaming.json'); const db = await jsonDb('prime-gaming.json');
db.data ||= {}; db.data ||= {};
let exit = false;
process.on('SIGINT', () => { // e.g. when killed by Ctrl-C
console.log('\nInterrupted by SIGINT. Exit! Exception shows where the script was:\n');
exit = true;
});
// https://playwright.dev/docs/auth#multi-factor-authentication // https://playwright.dev/docs/auth#multi-factor-authentication
const context = await firefox.launchPersistentContext(cfg.dir.browser, { const context = await firefox.launchPersistentContext(cfg.dir.browser, {
headless: cfg.headless, headless: cfg.headless,
@ -165,7 +171,7 @@ try {
} catch (error) { } catch (error) {
console.error(error); // .toString()? console.error(error); // .toString()?
process.exitCode = 1; process.exitCode = 1;
if (error.message && !error.message.includes('Target closed')) // e.g. when killed by Ctrl-C if (error.message && !exit)
notify(`prime-gaming failed: ${error.message.split('\n')[0]}`); notify(`prime-gaming failed: ${error.message.split('\n')[0]}`);
} finally { } finally {
await db.write(); // write out json db await db.write(); // write out json db