Skip to content

Conversation

@jtwaleson
Copy link
Contributor

Here we add a priority queue to the postgres bg worker. This way, we can prioritize jobs.

Now we can do Worker::perform_later_with_priority(ctx, params, Some(100)) to schedule a high prio job or with Some(0) for a low prio job.

I brought this up in #1462

What I'm using this for: we sync and analyze 100s of git repos. Each one has 3 stages: fetch, quick analysis, thorough analysis. As we want to show the results of the quick analysis as soon as possible, we give the following prios: fetch: 5, quick analysis: 10, thorough analysis: 1. This way, we always fetch first, but after fetching, we do a quick analysis right away. When everything is fetched and "quick analyzed", we handle the thorough analysis.

Please let me know what you think, and if we would want this for all queue backends or if we are fine with just postgres. If we don't want this at all, also fine.

@kaplanelad
Copy link
Contributor

Yes, this feature is required across all backends.

@jtwaleson
Copy link
Contributor Author

Yes, this feature is required across all backends.

Great, I'll see what I can do.

As Redis used a pure list, it needed to change to ZSET so we can order
on priority when popping new tasks. Added extra tests to test some edge
cases here.

This required a bit of refactoring, as we now need a peek-and-acquire
strategy to prevent concurrency issues.
1. Use `ZRANGE` to scan the queue without removing jobs.
2. Check job tags in the application.
3. If a match is found, use a Lua script (`ACQUIRE_JOB_SCRIPT`) to atomically verify
   existence, remove from queue, and move to processing set.

To test for this, we added a regression test
`test_dequeue_skips_mismatched_tags_no_infinite_loop`.
@jtwaleson jtwaleson marked this pull request as ready for review January 25, 2026 09:43
@jtwaleson
Copy link
Contributor Author

Hey @kaplanelad , I've added support for sqlite and redis, and rebased my commits. The redis implementation needed some work as the native LPOP operations are incompatible with a priority queue report, and the combination with tags made it still a bit more difficult, hence the updated Lua script.

While creating the test cases I found some flaky behavior but I've fixed all the issues.

I've been running in production with the postgres implementation for months now, but I've not battle-tested sqlt and redis.

I believe it's ready for review & merge.

@jtwaleson jtwaleson changed the title WIP: Priority queue Priority queue Jan 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants