-1

I have a DynamoDB table with the following items

{
   "jobId":<job1>,
   "cron" : "* 5 * * *"
},
{
   "jobId":<job2>,
   "cron" : "* 8 * * *"
}

I need to scan items who next execution time based on cron string is within the next 5 minutes, based on current time. Is there a way I can convert the cron to a valid next execution time while scanning? I am using node.js in AWS Lambda and cron-parser npm library to extract next_execution time from cron string.

Deesha
  • 538
  • 8
  • 27

1 Answers1

0

Note that scanning the full table will slow down over time. You may want to consider some other data store or structure to store this data.

That said something like this could work:

const results = await client.scan({ TableName: 'tableName' }).promise();
const cronItems = results.Items;

const intervals = cronItems.map((item) => {
    return cronParser.parseExpression(item.cron);
});

const now = new Date();
const fiveMinMillis = 300 * 1000;

const within5Mins = intervals.filter((interval) => {
    const timeUntil = interval.next().valueOf() - now.valueOf();
    return timeUntil < fiveMinMillis;
});

Note you will actually need to call scan(...) iteratively until the response does not include a LastEvaluatedKey attribute. See here for details.

Peter Wagener
  • 2,073
  • 13
  • 20