扫描 DynamoDB 属性,它是一个 cron 字符串并根据当前时间进行过滤

Scan DynamoDB attribute which is a cron string and filter based on current time

我有一个包含以下项目的 DynamoDB table

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

我需要根据当前时间扫描基于 cron 字符串的下一次执行时间在接下来的 5 分钟内的项目。 有没有一种方法可以在扫描时将 cron 转换为有效的下一次执行时间? 我在 AWS Lambda 中使用 node.js 和 cron-parser npm 库从 cron 字符串中提取 next_execution 时间。

请注意,扫描完整 table 会随着时间的推移变慢。您可能需要考虑一些其他数据存储或结构来存储此数据。

那说这样的东西可以工作:

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;
});

请注意,您实际上需要反复调用 scan(...),直到响应不包含 LastEvaluatedKey 属性。 See here for details.