-
Notifications
You must be signed in to change notification settings - Fork 49
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
uow: update TaskOp; add TaskRevokeOp #579
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -104,6 +104,7 @@ def on_commit(self, uow): | |
|
||
from functools import wraps | ||
|
||
from celery import current_app | ||
from invenio_db import db | ||
|
||
from ..tasks import send_change_notifications | ||
|
@@ -261,10 +262,32 @@ def __init__(self, celery_task, *args, **kwargs): | |
self._celery_task = celery_task | ||
self._args = args | ||
self._kwargs = kwargs | ||
self.celery_kwargs = {} | ||
|
||
def on_post_commit(self, uow): | ||
"""Run the post task operation.""" | ||
self._celery_task.delay(*self._args, **self._kwargs) | ||
self._celery_task.apply_async( | ||
args=self._args, kwargs=self._kwargs, **self.celery_kwargs | ||
) | ||
|
||
@classmethod | ||
def for_async_apply(cls, celery_task, args=None, kwargs=None, **celery_kwargs): | ||
"""Create TaskOp that supports apply_async args.""" | ||
temp = cls(celery_task, *(args or tuple()), **(kwargs or {})) | ||
temp.celery_kwargs = celery_kwargs | ||
return temp | ||
|
||
|
||
class TaskRevokeOp(Operation): | ||
"""A celery task stopping operation.""" | ||
|
||
def __init__(self, task_id: str) -> None: | ||
"""Initialize the task operation.""" | ||
self.task_id = task_id | ||
|
||
def on_post_commit(self, uow) -> None: | ||
"""Run the revoke post commit.""" | ||
current_app.control.revoke(self.task_id, terminate=True) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shall we add a check if There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you have a recommendation of what to do if it's not? Raise or pass? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe you can get the current celery app from the |
||
|
||
|
||
class ChangeNotificationOp(Operation): | ||
|
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.
Quick question: is this added just as a helper/util to set the defaults args to☺️
tuple
?I think we call this
def create
in another places, unless I am misunderstanding the needThere 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.
I should have mentioned the use case before, I have updated it here: #579 (comment)