Skip to content
Snippets Groups Projects
worker.js 1.19 KiB
Newer Older
process.title = 'Jetsam Queue Worker'

const { config, boot } = require('bootstrap')
const debug = require('debug')('server:worker:boot')
const Sentry = require('@sentry/node')
const bindQueue = require('core/utils/queue')
const pkg = require('./package.json')

let close = null
let done = false

async function main() {
	await boot()
	bindSentry()

	const { queue } = require('services')
	if (queue._initialise) {
		await queue._initialise()
	}

	bindQueue()

	close = await queue.listen()
	await new Promise(async r => {
		debug('Starting worker spin loop')
		while (!done) { await new Promise(rr => setTimeout(rr, 10)) }
		debug('Ending worker spin loop')
		r(true)
	})
}

function bindSentry() {
	const sentryUtil = require('vendor/sentry')
	sentryUtil.configure()

	debug('Binding sentry to process level errors')

	process.on("error", (err) => {
		Sentry.captureException(err);
	});
}

main()
	.catch(e => {
		console.error(e)
		Sentry.captureException(e);
		process.exit(1)
	})


const cleanupsigs = [
	'SIGINT',
	'SIGTERM',
	'SIGUSR2',
]

cleanupsigs.forEach(signal => {
	process.on(signal, () => {
		if (close) {
			close()
		}
		done = true
		Sentry.close(2000).then(() => {
			process.exit(0)
		})
	})
})