grantcarthew / node-rethinkdb-job-queue

A persistent job or task queue backed by RethinkDB.
https://github.com/grantcarthew/node-rethinkdb-job-queue/wiki
MIT License
156 stars 16 forks source link

job uniqueness #7

Closed TomKaltz closed 8 years ago

TomKaltz commented 8 years ago

Is there an easy way to prevent submitting a job with the same payload/params more than once.

grantcarthew commented 8 years ago

Hi again @TomKaltz,

A few weeks ago I thought about adding a filter/find method to the Queue object. This would achieve uniqueness but would require the user to check for existence first.

Something like this.


q.findJob({ email: tom@kaltz.com }).then((results) => {
  if (results.length < 1) {
    return q.addJob(newJob)
  }
})

This would not be hard to add. I didn't really have a good reason not to do it. Just decided it probably wouldn't be used. The whole KISS principle and all.

I'll add this one in if you think is it a good solution?

TomKaltz commented 8 years ago

I think that would be a good solution solution in lieu of automatic uniqueness checking. I would definitely use the findJob() filtering feature.

grantcarthew commented 8 years ago

OK, I'll add it in.

grantcarthew commented 8 years ago

Hey @TomKaltz.

I'm going to think on the other issues you have raised. Thanks very much by the way. This one I am really happy with. The API is as you can see above although you can also pass in a function predicate for advanced filtering.

See this document for more detail on the filtering: https://www.rethinkdb.com/api/javascript/filter/

It is published in v0.2.1. I will update the Wiki documentation tomorrow. I'm in Australia and it's getting late. There are other things I need to be doing.

Thanks again.

sagivf commented 7 years ago

@grantcarthew I"m confused on whether this is implemented or not? I think it's important as it's nice to be a able to add jobs without worrying that another process running the same code had already added the job.

grantcarthew commented 7 years ago

@sagivf Yes mate: https://github.com/grantcarthew/node-rethinkdb-job-queue/wiki/Queue.findJob

As it says in that document, do a find then add is not guaranteed to prevent adding the same job though. If two Queue objects are trying to add the job at the same time, it could still happen.

sagivf commented 7 years ago

So how about adding an option for self creating an id or name to ensure uniqueness? The thing is it's very useful to have the same code run on multiple processes with the insurance things don't get duplicated. otherwise you would need a separate process/code for creating jobs and handling them. Not sure approach which is better to be honest...