2024-04-17 09:40:44 +00:00
|
|
|
const Queue = require('./queue');
|
2024-04-08 13:17:06 +00:00
|
|
|
|
|
|
|
module.exports = class QueueFk extends Queue {
|
|
|
|
constructor(consumer, name, conf) {
|
|
|
|
super(consumer, name, conf);
|
|
|
|
this.reset();
|
|
|
|
}
|
|
|
|
|
|
|
|
async consume() {
|
|
|
|
await super.consume();
|
|
|
|
this.flush();
|
|
|
|
}
|
|
|
|
|
|
|
|
reset() {
|
2024-05-07 09:56:42 +00:00
|
|
|
this.messages = [];
|
2024-04-08 13:17:06 +00:00
|
|
|
this.nMessages = 0;
|
2024-04-17 09:40:44 +00:00
|
|
|
this.scopes = new Map();
|
2024-04-08 13:17:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
flush(flushInterval) {
|
|
|
|
if (this.timeout) {
|
|
|
|
clearTimeout(this.timeout);
|
|
|
|
this.timeout = null;
|
|
|
|
}
|
|
|
|
this.timeout = setTimeout(
|
|
|
|
() => this.onFlushTimeout(), flushInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
async onFlushTimeout() {
|
2024-05-07 09:56:42 +00:00
|
|
|
const {messages} = this;
|
2024-04-08 13:17:06 +00:00
|
|
|
|
2024-05-07 09:56:42 +00:00
|
|
|
if (messages.length) {
|
2024-05-07 11:04:07 +00:00
|
|
|
const {consumer, scopes, channel} = this;
|
2024-04-08 13:17:06 +00:00
|
|
|
this.reset();
|
2024-04-17 15:01:13 +00:00
|
|
|
|
|
|
|
if (consumer.conf.debug)
|
|
|
|
console.debug('Flush:'.blue, this.name.yellow, scopes);
|
2024-04-08 13:17:06 +00:00
|
|
|
|
|
|
|
try {
|
2024-04-17 09:40:44 +00:00
|
|
|
for (const [scope, ids] of scopes) {
|
|
|
|
let query = this.conf.query[scope];
|
|
|
|
for (const id of ids) {
|
|
|
|
const params = {id};
|
|
|
|
//query = consumer.db.format(query, params);
|
|
|
|
if (consumer.conf.debug)
|
|
|
|
console.debug('SQL:'.blue, query, params);
|
|
|
|
if (!consumer.conf.testMode)
|
|
|
|
await consumer.db.query(query, params);
|
|
|
|
}
|
2024-04-08 13:17:06 +00:00
|
|
|
}
|
|
|
|
|
2024-05-07 11:04:07 +00:00
|
|
|
const promises = [];
|
|
|
|
for (const msg of messages)
|
|
|
|
promises.push(channel.ack(msg));
|
|
|
|
await Promise.all(promises);
|
2024-04-08 13:17:06 +00:00
|
|
|
} catch(err) {
|
2024-05-07 11:04:07 +00:00
|
|
|
for (const msg of messages)
|
|
|
|
await channel.nack(msg);
|
|
|
|
throw err;
|
2024-04-08 13:17:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.flush(this.conf.flushInterval);
|
|
|
|
}
|
|
|
|
|
2024-05-07 11:04:07 +00:00
|
|
|
onConsume(msg) {
|
2024-04-17 09:40:44 +00:00
|
|
|
const {conf} = this;
|
2024-04-08 13:17:06 +00:00
|
|
|
const consumer = this.consumer;
|
|
|
|
const data = JSON.parse(msg.content.toString());
|
|
|
|
|
|
|
|
if (consumer.conf.debug)
|
|
|
|
console.debug('Message:'.blue, this.name.yellow, data.table);
|
|
|
|
|
2024-04-17 09:40:44 +00:00
|
|
|
const tableConf = conf.includeSchema[data.schema][data.table];
|
|
|
|
|
|
|
|
const scope = tableConf.scope ?? data.table;
|
|
|
|
let ids = this.scopes.get(scope);
|
|
|
|
if (!ids) this.scopes.set(scope, ids = new Set());
|
2024-04-08 13:17:06 +00:00
|
|
|
|
2024-04-17 09:40:44 +00:00
|
|
|
const key = tableConf.key;
|
2024-04-08 13:17:06 +00:00
|
|
|
if (data.eventName === 'updaterows') {
|
|
|
|
for (const row of data.rows) {
|
|
|
|
ids.add(row.before[key]);
|
|
|
|
ids.add(row.after[key]);
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
for (const row of data.rows)
|
|
|
|
ids.add(row[key]);
|
|
|
|
|
2024-05-07 09:56:42 +00:00
|
|
|
this.messages.push(msg);
|
2024-05-07 11:04:07 +00:00
|
|
|
if (this.messages.length == conf.amqpPrefetch)
|
2024-04-08 13:17:06 +00:00
|
|
|
this.flush();
|
|
|
|
}
|
|
|
|
}
|