Bull队列:通过在jobId中使用部分时间戳来确保时间段内作业的唯一性

aly*_*lyx 5 priority-queue message-queue job-queue node.js bull.js

我需要确保添加到队列中的同一作业在一段时间内不会重复。

\n\n

是否值得D/M/Y-HH:M在我的独特中包含部分时间戳(即)jobId,以便仅在不在同一分钟内时才进行处理?

\n\n

如果在 处添加一项作业,在 处添加12:01另一项作业,它仍然会重复12:09\xe2\x80\x93,它仍然会重复,或者 Bull 是否有更好的方法来做到这一点?

\n

zen*_*eni 0

我觉得你应该使用 Bull 的 API 来检查作业是否正在运行,然后你决定是否将作业添加到队列中(如果没有)(在生产者上打补丁)。

您还可以决定在运行作业时(在流程函数内)检查类似的作业是否已经在运行,并提前返回而不是执行作业(在消费者上打补丁)。

您可以使用 Queue getJobs 函数来执行此操作:

getJobs(types: string[], start?: number, end?: number, asc?: boolean):Promise<Job[]>

"Returns a promise that will return an array of job instances of the given types. Optional parameters for range and ordering are provided."
Run Code Online (Sandbox Code Playgroud)

来自文档: https: //github.com/OptimalBits/bull/blob/develop/REFERENCE.md#queuegetjobs

工作项目应提供足够的数据,以便您可以找到所需的工作。