mirror of
https://gitcode.com/gh_mirrors/hy/Hydro.git
synced 2026-10-20 01:38:19 +08:00
core: consumer: handle database read failure
This commit is contained in:
1 file changed
+24
-11
@@ -16,13 +16,20 @@ const argv = cac().parse();
|
|||||||
|
|
||||||
async function getFirst(query: Filter<Task>) {
|
async function getFirst(query: Filter<Task>) {
|
||||||
if (process.env.CI) return null;
|
if (process.env.CI) return null;
|
||||||
const q = { ...query };
|
try {
|
||||||
const res = await coll.findOneAndDelete(q, { sort: { priority: -1 } });
|
const q = { ...query };
|
||||||
if (res.value) {
|
const res = await coll.findOneAndDelete(q, { sort: { priority: -1 } });
|
||||||
logger.debug('%o', res.value);
|
if (res.value) {
|
||||||
return res.value;
|
logger.debug('%o', res.value);
|
||||||
|
return res.value;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
} catch (e) {
|
||||||
|
// Usually caused by the database being down
|
||||||
|
// Should recover once it is back
|
||||||
|
logger.error(e);
|
||||||
|
return null;
|
||||||
}
|
}
|
||||||
return null;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export class Consumer {
|
export class Consumer {
|
||||||
@@ -163,11 +170,17 @@ export async function apply(ctx: Context) {
|
|||||||
logger.info('No replica set found.');
|
logger.info('No replica set found.');
|
||||||
// eslint-disable-next-line no-constant-condition
|
// eslint-disable-next-line no-constant-condition
|
||||||
while (true) {
|
while (true) {
|
||||||
// eslint-disable-next-line no-await-in-loop
|
let res;
|
||||||
const res = await collEvent.findOneAndUpdate(
|
try {
|
||||||
{ expire: { $gt: new Date() }, ack: { $nin: [id] } },
|
// eslint-disable-next-line no-await-in-loop
|
||||||
{ $push: { ack: id } },
|
res = await collEvent.findOneAndUpdate(
|
||||||
);
|
{ expire: { $gt: new Date() }, ack: { $nin: [id] } },
|
||||||
|
{ $push: { ack: id } },
|
||||||
|
);
|
||||||
|
} catch (e) {
|
||||||
|
logger.error(e);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
if (argv.options.showEvent) logger.info('Event: %o', res.value);
|
if (argv.options.showEvent) logger.info('Event: %o', res.value);
|
||||||
// eslint-disable-next-line no-await-in-loop
|
// eslint-disable-next-line no-await-in-loop
|
||||||
await (res.value ? handleEvent(res.value) : sleep(500));
|
await (res.value ? handleEvent(res.value) : sleep(500));
|
||||||
|
|||||||
Reference in new issue
Block a user