3

I have to delete all the OTP records which expired after every three months.

In /sheduled-tasks/OTPRecords.js

const prisma = require("../services/prisma");

const THREE_MONTHS = 1000 * 60 * 60 * 24 * 90;

async function deleteExpiredOTPRecords() {
    console.log("first");
    setInterval(async () => {
      console.log("INTERVAL", THREE_MONTHS);
      const today = new Date();
      const yesterday = new Date(today.setDate(today.getDate() - 1));
      const records = await prisma.otp.deleteMany({
        where: { expires_at: { lte: yesterday } },
      });
      console.log(records);
    }, THREE_MONTHS);
}
module.exports = deleteExpiredOTPRecords;

In /index.js

 deleteExpiredOTPRecords();

I was expecting this function run after every THREE_MONTHS but my code runs indefinitely in a loop, i am unable to figure out where am i going wrong.

OUTPUT

INTERVAL 7776000000
INTERVAL 7776000000
{ count: 0 }
{ count: 0 }
INTERVAL 7776000000
{ count: 0 }
INTERVAL 7776000000
{ count: 0 }
INTERVAL 7776000000
{ count: 0 }
INTERVAL 7776000000
{ count: 0 }
INTERVAL 7776000000
{ count: 0 }
INTERVAL 7776000000
{ count: 0 }
INTERVAL 7776000000
{ count: 0 }
INTERVAL 7776000000
{ count: 0 }
INTERVAL 7776000000
{ count: 0 }
INTERVAL 7776000000
{ count: 0 }
INTERVAL 7776000000
INTERVAL 7776000000
{ count: 0 }
^C(node:33075) [DEP0164] DeprecationWarning: Implicit coercion to integer for exit code is 
deprecated.
  • I am not clear on the logic of this. Why attempt to wait for three months - how would records created the day after the previous three months be caught at the right time? And is this JS really running uninterrupted forever? – A Haworth Mar 10 '23 at 09:05

1 Answers1

2

The interval is stored in a signed 32-bit int.so what happens here is if the mili-second value is greater than 2^31-1 which is 2147483647 the interval becomes invalid hence infinite loop.

check the documentation

So your value is 7776000000 is greater than 4294967295 (2**31 - 1).

Shankaja Aroshana
  • 551
  • 1
  • 3
  • 17