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() {
|
|
|
|
this.lastMessage = null;
|
|
|
|
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-04-17 09:40:44 +00:00
|
|
|
if (this.nMessages) {
|
|
|
|
const {consumer} = this;
|
|
|
|
|
2024-04-08 13:17:06 +00:00
|
|
|
if (consumer.conf.debug)
|
|
|
|
console.debug('Flush:'.blue, this.name.yellow, this.ids);
|
|
|
|
|
2024-04-17 09:40:44 +00:00
|
|
|
const scopes = this.scopes;
|
2024-04-08 13:17:06 +00:00
|
|
|
const lastMessage = this.lastMessage;
|
|
|
|
this.reset();
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
await this.channel.ack(lastMessage, true);
|
|
|
|
} catch(err) {
|
|
|
|
await this.channel.nack(lastMessage, true);
|
|
|
|
console.error(err);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
this.flush(this.conf.flushInterval);
|
|
|
|
}
|
|
|
|
|
|
|
|
async 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]);
|
|
|
|
|
|
|
|
this.nMessages++;
|
|
|
|
this.lastMessage = msg;
|
|
|
|
|
|
|
|
if (this.nMessages == consumer.conf.amqpPrefetch)
|
|
|
|
this.flush();
|
|
|
|
}
|
|
|
|
}
|