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
Add the ability to set a priority #3
Conversation
Thanks to the ordering of the file system, by prefixing the filename with a number, we get a priority functionality for (essentially) free.
This is a cool idea but I have some issues with it:
Thanks for the contribution! |
Another issue is that you relay on that fs.readDir will return the files sorted alphanumerically. While this might be true for now I would rather to have an explicit sort in getNextMessageId |
Isn't the "reading" part always be in the order of priority (or before this PR in order of timestamp)? I am not quite sure I understand the limitation. Is this about parallel processing, in which case both could start at the same time and the low priority one could finish first?
That's cool, I'll update this PR soon.
Damn, I didn't realize that. That indeed is an issue already: 1 - 10 - 3 - 50 is the order on OSX. A possible solution would be limiting the priority to 1-9 and emitting the sorting - that should work with the native alphasort that is used for ordering by timestamp already. Sorting the files manually seems like a performance killer for many messages to me. Speaking of that tho, it looks like Edit: Tested sorting for 10.000 messages, took about 75ms per sort on average on my machine. |
10,000/75ms might sound fast, but for the current implementation we would need to sort for each consumed message separately. Lets say that more messages are coming it and the queue is steady at 10000. Consuming the first 10000 would take 750 seconds. There might be some solutions for this -
It seems that it's not 'free' as we previously thought. You are welcome to try to re-implement it, I'll keep this PR open. |
…get around sorting
Alright, changes I made:
This does not solve the issue of |
lib/storage.js
Outdated
const filePath = path.join(this.pendingDir, `${nowNanoSeconds}`); | ||
const filePath = path.join( | ||
this.pendingDir, | ||
`${options.priority}${nowNanoSeconds}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Add an _ between to priority and the nanoSeconds. Just for human readability
README.md
Outdated
@@ -70,6 +70,7 @@ queue.publish(message); | |||
``` | |||
Publishes a new message to the job queue. Returns a promise that resolves when publishing is done | |||
- message - plain js object containing message data | |||
- You can order messages by adding a `_priority` property to the message object (1 - 9). Messages will be consumed in order of priority, lowest first (1, 2, 3...). The default priority is "5". There is no assurance that a message with some priority will be processed fully before an other message with a lower priority, just that they will be read in order of priority. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You forgot up update the docs about the move of priority into an options object
Fixed both issues. 🙂 |
Awesome, thanks! |
Thanks to the ordering of the file system, by prefixing the filename with a number, we get a priority functionality for (essentially) free.