Node.js 倾听议程工作的完成情况

Node.js 倾听议程工作的完成情况,node.js,agenda,Node.js,Agenda,我正在尝试建立一个加密取款系统,需要确保系统不会重复,等等。所以我决定使用Agenda来帮助我。我遇到的问题是,我需要监听作业的完成情况,以便从新发送的事务中检索事务ID。这将只在作业完成后可用,我找不到任何关于为一个作业使用侦听器的信息 以下是我的代码的一部分: let pendingUserWithdrawJob = await Job.count({ name: "withdraw_order", "data.userId": user._id, completed: { $ne: tru

我正在尝试建立一个加密取款系统,需要确保系统不会重复,等等。所以我决定使用Agenda来帮助我。我遇到的问题是,我需要监听作业的完成情况,以便从新发送的事务中检索事务ID。这将只在作业完成后可用,我找不到任何关于为一个作业使用侦听器的信息

以下是我的代码的一部分:

let pendingUserWithdrawJob = await Job.count({ name: "withdraw_order", "data.userId": user._id, completed: { $ne: true } });
    if (pendingUserWithdrawJob > 0) return 1;

    const job = global.agenda.create('withdraw_order', {userId: user._id, recipientAddress: addr, amount, txid: null });
    job.save((err) => {
        if (err) return false;

        return msg.reply('Successfully withdrawn! TXID: ' + job.attrs.data.txid);
    });
这是在任何类型的完成检查之前,txid返回null,这是预期的

以下是我的议程定义代码:

agenda.define('withdraw_order', async function(job, done) {
    let result = await paymentProcessor.performWithdraw(job);
    if (result.error) return done(result.error);
    done();
});
有问题的performWithdraw函数是:

async performWithdraw(options) {
    try {
        await this.withdraw(options);
        return { success: true };
    } catch(e) {
        this.reportException(e);
        return { error: e };
    }
}
本条撤回:

async withdraw(job) {
    // parameters
    const userId            = job.attrs.data.userId;
    const recipientAddress  = job.attrs.data.recipientAddress;
    const amount            = job.attrs.data.amount;

    // Validate if user is present
    let user = await models.User.findById(userId);
    if (!user) throw new Error(`User ${userId} not found`);
    await models.User.validateWithdrawAmount(user, amount);

    // Step 1: Process transaction
    let sendID;

    if (job.attrs.sendStepCompleted) {
        sendID = job.attrs.txid;
    } else {
        const sent = await this.pivxClient.send(recipientAddress, amount);
        if (sent.error) throw new Error(sent.error);
        await models.Job.findOneAndUpdate({ _id: job.attrs._id} , { "data.sendStepCompleted": true, "data.txid": sent.txid });
    }

    // Step 2: Update user balance
    if (!job.attrs.userStepCompleted) {
        await models.User.withdraw(user, amount);
        await models.Job.findByIdAndUpdate(job.attrs._id, { "data.userStepCompleted": true });
    }

    // Step 3: Record Transaction
    if (!job.attrs.transactionStepCompleted) {
        await models.Transaction.create({ userId: userId, withdraw: amount, txid: sendID });
        await models.Job.findByIdAndUpdate(job.attrs._id, { "data.transactionStepCompleted": true });
    }

    return sendID;
}
事先为混乱的代码道歉,我只是想确保没有遗漏任何可能重要的内容。在不使用setInterval和检查transactionStepCompleted的情况下,如何侦听要完成的作业

议程实例将发出以下事件:

开始-在作业开始前调用

开始:作业名称-在指定作业开始之前调用

complete—在作业完成时调用,无论它成功还是失败

complete:作业名称-在作业完成时调用,无论作业成功或失败


议程.on('start',job=>{
console.log('Job%s starting',Job.attrs.name);
});
议程。在('complete',job=>{
log(`Job${Job.attrs.name}finished`);
});