0

I am building a simple apm.

I batch events and store them in db batched.

I also want to store anything left in the batch once the process ends/killed/error

This is my code


export function startApm(): void {
    process.on('SIGINT', async () => {
        try {
            console.log('APM - Graceful shutdown');
            console.log(`Will insert ${batched.length} entries`);
            await insertExecutionTimes(batched);
            console.log('Execution times inserted successfully');
        } catch (error) {
            console.error('Error inserting apm entries:', error);
        } finally {
            process.exit(0);
        }
    });

    setInterval(async () => {
        if (batched.length >= batchSize) {
            console.log(`Storing batch of ${batchSize}, total length ${batched.length}`);
            await insertExecutionTimes(batched.splice(0, batchSize));
            console.log('Length after', batched.length);
        }
    }, 250);
}

'SIGINT' handler is executed fine

I see console logs console.log('APM - Graceful shutdown'); but nothing in store in the database, seems like process dies before await insertExecutionTimes(batched); is finished.

async function insertExecutionTimes(data: ApmEntry[]): Promise<void> {
    const trx = await db.transaction();

    try {
        await trx.batchInsert(Tables.Apm, data);
        await trx.commit();
        console.log(`Inserted ${data.length} records successfully`);
    } catch (error) {
        await trx.rollback();
        console.error('Error inserting execution times', error);
    }
}

Any ideas would be appreciated

10
  • You ask about the problem that doesn't exist in general. The process won't end until you call process.exit, or it's ended in some otherway. Console isn't the best way to debug, use a debugger. But you should see other console entries besides "Graceful shutdown" Commented Feb 12 at 15:58
  • What do you mean with "You ask about the problem that doesn't exist in general" ? Commented Feb 12 at 16:16
  • I used console because I see no data in the database, so the await is clearly not waiting for the insert to finish Commented Feb 12 at 16:17
  • 1
    Whatever happens on your side, the question needs to reflect this because this is very specific to your case. The question needs to contain stackoverflow.com/help/mcve, currently it's not reproducible. I already mentioned what could be done to narrow down the problem. You need to debug what happens in startApm, not rely on indirect evidences like database writes. You should be able to do this with console output and debugger breakpoints. For this you could use a minimal reproduction. Use the exact event you're addressing (ctrl+c), not obscure things that could happen in ide Commented Feb 13 at 17:36
  • 1
    And for realworld use you'd need also sigterm and sighup handlers for a proper cleanup. It's unknown how much their absence affects your case. This could at least be so in ide, as the link suggests Commented Feb 13 at 18:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.