mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-19 17:38:19 +08:00
module.add(user,problem,record)
This commit is contained in:
commit
d5b1f623f8
43 files changed
+5965
No files matched your search
@@ -0,0 +1,53 @@
|
||||
const options = require('../options');
|
||||
if (options.mq) {
|
||||
const amqplib = require('amqplib');
|
||||
/**
|
||||
* @type {import('amqplib').Connection}
|
||||
*/
|
||||
let conn = null;
|
||||
let ch = null;
|
||||
|
||||
module.exports = {
|
||||
async connect() {
|
||||
conn = await amqplib.connect(options.mq.url);
|
||||
ch = conn.createChannel();
|
||||
},
|
||||
async assertQueue(queue) {
|
||||
return await ch.assertQueue(queue);
|
||||
},
|
||||
async publish(queue, data) {
|
||||
return await ch.sendToQueue(queue, Buffer.from(data));
|
||||
},
|
||||
async consume(queue, callback) {
|
||||
ch.consume(queue, function (msg) {
|
||||
if (msg !== null) {
|
||||
callback(msg.content.toString(), () => { ch.ack(msg); });
|
||||
ch.ack(msg);
|
||||
}
|
||||
});
|
||||
}
|
||||
};
|
||||
} else {
|
||||
let q = {};
|
||||
module.exports = {
|
||||
async connect() { },
|
||||
async assertQueue(queue) {
|
||||
q[queue] = q[queue] || [];
|
||||
},
|
||||
async publish(queue, data) {
|
||||
q[queue].push(data);
|
||||
},
|
||||
async consume(queue, callback) {
|
||||
while (1) {
|
||||
if (q[queue].length) {
|
||||
let data = q[queue][0];
|
||||
q[queue].splice(0, 1);
|
||||
callback(data, () => { });
|
||||
}
|
||||
await new Promise(resolve => {
|
||||
setTimeout(resolve, 500);
|
||||
});
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
Reference in new issue
Block a user