samsondav / rihanna

Rihanna is a high performance postgres-backed job queue for Elixir
MIT License
439 stars 47 forks source link

Feature request: job priority #56

Closed KushalP closed 5 years ago

KushalP commented 5 years ago

Summary

All jobs are not created equal. Some need to be scheduled earlier than others if there aren't enough workers to work on all of the enqueued jobs. Allow defining a priority against all jobs to make sure that higher priority jobs are scheduled.

An example

You are running a subscription business and have two background jobs defined:

In this example you want to send the email to the customer to make sure they have feedback on the receipt of their subscription. This would take a higher priority over notifying a Slack channel.

Design idea

Create a column priority (smallint/integer) that allows defining an integer priority. This can conform to the niceness values used in Linux — lower values are more important. When trying to poll for jobs, order the result by priority.

samsondav commented 5 years ago

@KushalP Thanks for opening this issue!

Yup, the idea sounds good. Shouldn't be too hard. We're going to need a migration since we have to add a column and either add/modify an index to cover priority.

Would you like to take a crack at implementing this?

KushalP commented 5 years ago

Makes sense. How should the priority be defined on a Rihanna.Job level for people implementing their own priority levels?

samsondav commented 5 years ago

You asked for the feature... how would you like to use it? :)

KushalP commented 5 years ago

A field that could be overridden makes sense: @priorty 10.

samsondav commented 5 years ago

Sounds great. Could be even better as a function:

def priority, do: 10

This has two advantages:

  1. It can be defined in the behaviour as an optional (or overridable) callback
  2. It can be calculated dynamically if the client really wants to implement that