In BullMQ, there's no built-in functionality to automatically prevent the addition of duplicate jobs. However, you can implement this yourself by assigning unique job IDs.
The job ID must be unique for every job in a given queue. If you try to add a job with an ID that already exists in the queue, BullMQ will throw an error. Therefore, you can use this feature to prevent the addition of duplicate jobs.
Here's how you can do it:
In this example, we're using the data as the job ID, which means that a job with the same data cannot be added twice. This can effectively prevent duplicate jobs based on your definition of duplication.
Remember, choosing a job ID depends on how you define a "duplicate" job. If two jobs with different data but doing the same kind of work are considered duplicates, it might make sense to generate job IDs based on the type of work instead of the data. Just ensure that the chosen job ID accurately represents a unique piece of work in your application context.