Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pencilcheck/758e36120f6a106328698379682c828a to your computer and use it in GitHub Desktop.
Save pencilcheck/758e36120f6a106328698379682c828a to your computer and use it in GitHub Desktop.
const dev = process.env.NODE_ENV !== 'production';
if (dev) {
require('dotenv').config();
}
const { parentPort } = require('worker_threads');
const Cabin = require('cabin');
const { Signale } = require('signale');
// initialize cabin
const cabin = new Cabin({
axe: {
logger: new Signale()
}
});
global.cabin = cabin; // global
// store boolean if the job is cancelled
let isCancelled = false;
// handle cancellation (this is a very simple example)
if (parentPort)
parentPort.once('message', message => {
if (message === 'cancel') isCancelled = true;
});
(async () => {
cabin.log('what is going on?')
// initialize client
const { Client } = require("pg").native;
const connectArgs = {
user: null,
password: null,
host: 'localhost',
port: 5432,
database: 'postgres',
ssl: false,
};
const client = new Client(connectArgs);
cabin.log('checkpoint 2')
try {
const res = await client.query(`
SELECT *
FROM TABLE_NAME
`);
cabin.log('checkpoint 3')
const rows = res.rows;
cabin.log('rows', rows)
if (rows.length === 0) {
cabin.info(`no rows to process...EXITING`);
cabin.log('checkpoint 4')
// signal to parent that the job is done
if (parentPort) parentPort.postMessage('done');
else process.exit(0);
}
} catch (err) {
cabin.error(err);
}
// if we've already cancelled this job then return early
if (isCancelled) return;
cabin.log('checkpoint 5')
// end connection
await client.end();
// signal to parent that the job is done
if (parentPort) parentPort.postMessage('done');
else process.exit(0);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment