138 lines
3.1 KiB
JavaScript
138 lines
3.1 KiB
JavaScript
|
|
||
|
const fs = require('fs');
|
||
|
const path = require('path');
|
||
|
const defaultConfig = require('./config.json');
|
||
|
|
||
|
const mysql = require('mysql2/promise');
|
||
|
const amqp = require('amqplib');
|
||
|
require('colors');
|
||
|
|
||
|
const config = Object.assign({}, defaultConfig);
|
||
|
const localPath = path.join(__dirname, 'config.local.json');
|
||
|
if (fs.existsSync(localPath)) {
|
||
|
const localConfig = require(localPath);
|
||
|
Object.assign(config, localConfig);
|
||
|
}
|
||
|
|
||
|
class Consumer {
|
||
|
constructor(config) {
|
||
|
this.config = config;
|
||
|
}
|
||
|
|
||
|
async start() {
|
||
|
if (this.config.testMode)
|
||
|
console.log('Test mode enabled, just logging queries to console.');
|
||
|
|
||
|
console.log('Starting process.');
|
||
|
await this.init();
|
||
|
console.log('Process started.');
|
||
|
}
|
||
|
|
||
|
async stop() {
|
||
|
console.log('Stopping process.');
|
||
|
await this.end();
|
||
|
console.log('Process stopped.');
|
||
|
}
|
||
|
|
||
|
async init() {
|
||
|
this.onErrorListener = err => this.onError(err);
|
||
|
|
||
|
this.db = await mysql.createConnection(this.config.consumerDb);
|
||
|
this.db.on('error', this.onErrorListener);
|
||
|
|
||
|
this.pingInterval = setInterval(
|
||
|
() => this.connectionPing(), this.config.pingInterval * 1000);
|
||
|
|
||
|
this.consumer = await amqp.connect(this.config.amqp);
|
||
|
this.channel = await this.consumer.createChannel();
|
||
|
this.channel.assertQueue(this.config.queue, {
|
||
|
durable: true
|
||
|
});
|
||
|
this.channel.consume(this.config.queue, msg => this.onConsume(msg));
|
||
|
}
|
||
|
|
||
|
async end(silent) {
|
||
|
clearInterval(this.pingInterval);
|
||
|
|
||
|
await this.consumer.close();
|
||
|
|
||
|
this.db.off('error', this.onErrorListener);
|
||
|
// FIXME: mysql2/promise bug, db.end() ends process
|
||
|
this.db.on('error', () => {});
|
||
|
try {
|
||
|
await this.db.end();
|
||
|
} catch (err) {
|
||
|
if (!silent)
|
||
|
console.error(err);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async connectionPing() {
|
||
|
this.debug('Ping', 'Sending ping to database.');
|
||
|
await this.db.ping();
|
||
|
}
|
||
|
|
||
|
async onConsume(msg) {
|
||
|
const fks = JSON.parse(msg.content.toString());
|
||
|
if (this.config.debug)
|
||
|
console.debug('RabbitMQ message'.blue, fks);
|
||
|
|
||
|
for (const fk of fks) {
|
||
|
if (this.config.debug)
|
||
|
console.debug('Query'.blue, this.config.recalcQuery.yellow);
|
||
|
await this.db.query(this.config.recalcQuery, fk);
|
||
|
}
|
||
|
|
||
|
this.channel.ack(msg);
|
||
|
}
|
||
|
|
||
|
async tryRestart() {
|
||
|
try {
|
||
|
await this.init();
|
||
|
console.log('Process restarted.');
|
||
|
} catch(err) {
|
||
|
setTimeout(() => this.tryRestart(), 30);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
async onError(err) {
|
||
|
console.log(`Error: ${err.code}: ${err.message}`);
|
||
|
try {
|
||
|
await this.end(true);
|
||
|
} catch(e) {}
|
||
|
|
||
|
switch (err.code) {
|
||
|
case 'PROTOCOL_CONNECTION_LOST':
|
||
|
case 'ECONNRESET':
|
||
|
console.log('Trying to restart process.');
|
||
|
await this.tryRestart();
|
||
|
break;
|
||
|
default:
|
||
|
process.exit();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
debug(namespace, message) {
|
||
|
if (this.config.debug)
|
||
|
console.debug(`${namespace}:`.blue, message.yellow);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
let consumer;
|
||
|
|
||
|
async function main() {
|
||
|
consumer = new Consumer(config)
|
||
|
await consumer.start();
|
||
|
|
||
|
process.on('SIGINT', async function() {
|
||
|
console.log('Got SIGINT.');
|
||
|
try {
|
||
|
await consumer.stop();
|
||
|
} catch (err) {
|
||
|
console.error(err);
|
||
|
}
|
||
|
process.exit();
|
||
|
});
|
||
|
}
|
||
|
main();
|