Skip to content

Commit

Permalink
feat: ability to call tasks directly w/o enqueue-ing them
Browse files Browse the repository at this point in the history
  • Loading branch information
roks0n committed Jul 22, 2024
1 parent b9fe1e5 commit e986152
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
21 changes: 16 additions & 5 deletions kolona/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ async def enqueue(
await q.put(task_item)
return task_item.id

async def __call__(self, *args, **kwargs):
return await self.func(*args, **kwargs)


class Task(GlobalTask):
"""
Expand Down Expand Up @@ -161,14 +164,22 @@ def task(
"""

def wrapper(func):
task = GlobalTask(
task_obj = GlobalTask(
func,
queue=queue,
max_retries=max_retries,
retry_intervals=retry_intervals,
delay=delay,
)
update_wrapper(task, func)
return task

return wrapper

async def callable_task(*args, **kwargs):
if kwargs.pop('enqueue', False):
return await task_obj.enqueue(*args, **kwargs)
else:
return await task_obj(*args, **kwargs)

update_wrapper(callable_task, func)
callable_task.enqueue = task_obj.enqueue
return callable_task

return wrapper
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[metadata]
name = kolona
version = 0.2.0
version = 0.2.1
description = A minimalistic in-memory async Python Task queue
long_description = file: README.md
long_description_content_type = text/markdown
Expand Down
11 changes: 11 additions & 0 deletions tests/test_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,3 +319,14 @@ async def runner():

assert log_spy.warning.call_count == 1
assert queue.qsize() == 0


async def test_calling_task_without_enqueing():

@task()
async def random_task():
return True

result = await random_task()
assert result == True

0 comments on commit e986152

Please sign in to comment.