roman-right / beanie_batteries_queue

Simple queue system for Beanie
Apache License 2.0
2 stars 3 forks source link

Task categories functionality #3

Open pahrohfit opened 11 months ago

pahrohfit commented 11 months ago

Added support for optional categories of Task, to allow for focused task worker designs, such that load can be distributed across multiple workers based upon the type of task they should be processing.

# Producer(s)
from beanie_batteries_queue import Task

class SimpleTask(Task):
    s: str

task_empty = SimpleTask(s="test")
await task_empty.push()

task_cat_1 = SimpleTask(s="test_cat_1", category="cat_1")
await task_cat_1.push()

task_cat_2 = SimpleTask(s="test_cat_2", category="cat_2")
await task_cat_2.push()

You can then have your worker(s) focus on chosen categories:

# Consumer
async for task in SimpleTask.queue(): # no category
    assert task.s == "test"
    # Do some work
    await task.finish()
    break

# -or-
async for task in SimpleTask.queue(category="cat_1"): # one category
    assert task.s == "test_cat1"
    # Do some work
    await task.finish()
    break

# -or-
async for task in SimpleTask.queue(category=["cat_1", "cat_2"]): # multiple categories
    assert task.s in ["test_cat1", "test_cat2"]
    # Do some work
    await task.finish()
    break

Unit tests included, supports all existing functionality such as pop, and is backwards compatible with previous versions.

pahrohfit commented 11 months ago

Sorry, couldn't update the .rst on it, as its in the beanie repo

roman-right commented 11 months ago

Hi. Sorry for the delay. I was focused on pydantic v2 support. Will pick this up soon. Thank you very much for the PR!

pahrohfit commented 10 months ago

No problem

pahrohfit commented 8 months ago

Thoughts?

roman-right commented 8 months ago

Hi @pahrohfit , Sorry, I forgot about this PR - I'll make a notifier for this project too. I'll check you PR tomorrow.