Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

peek #28

Open
ronag opened this issue Jul 3, 2015 · 3 comments
Open

peek #28

ronag opened this issue Jul 3, 2015 · 3 comments

Comments

@ronag
Copy link

ronag commented Jul 3, 2015

Would be nice if a worked could peek a task before actually trying to process it. i.e. to check whether or not it can process it.

@cbraynor
Copy link
Contributor

cbraynor commented Jul 6, 2015

This is interesting - perhaps something like reject(false) on the processing function or some other sentinel value to indicate that the job wasn't processed but it didn't fail either.

In this case though, there's nothing stopping the first worker from picking up the job again. Actually, it has a very tiny advantage in that it knows before any other worker that the task is up for grabs again. In that case, it's possible (though not probable) the worker will get into a loop and never process the item and also not process any other requests, but at the very least it might spin its wheels for a while.

Another possibility might be to have another function you pass in to the constructor that performs some kind of evaluation on each task - only new tasks that also pass the predicate are consumed by the worker.

In your use-case, is it easy to determine whether the job can be processed, or does it require some asynchronous or time-consuming operation?

@ronag
Copy link
Author

ronag commented Jul 6, 2015

I don't see why whether it is time consuming or not is an issue as the predicate check could (should) be a asynchronous operation.

In my use case the check is whether the worker has a specific file in its cache.

@cbraynor
Copy link
Contributor

cbraynor commented Jul 6, 2015

I asked because it changes the implementation details of the predicate check - if it's synchronous and quick (like a lodash predicate function for example) it could be inside a Firebase transaction function, which is where the worker claims a task. Any code in there could be run many times for every attempt to claim a task (the transaction is essentially a compare and swap implementation). A worker won't ever even claim a job it can't process, so there are no race conditions and it fits really easily into the current model.

If it's slow or asynchronous, the check will have to be after the job has been claimed but before it starts being processed. It will take a significant reworking and could slow every task down (a worker will have to claim it before checking if it can process it, and then discard it before possibly claiming it again, ad infinitum) not to mention the additional possibility of the worker claiming another job while the predicate is running. This is basically the same issue described with the reject(false) suggestion above. It could be worked around with some additional checks to see if the task has been attempted before, but it gets ugly quick.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants