You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
The current implementation of callbacks is quite confusing. We must create a callback with the Callback class, while the class doesn't specify what keyword parameters should be included in a callback function. The only way to learn about the callback function protocol is to use the documentation.
To avoid using invalid callback functions in my project, I had to create protocols to enforce the format of the callback function.
This provides nice static-checking capability to my library. Invalid callback functions will be detected by the type checker and errors will be raised in the editor.
I would suggest implement something similar in the rq library.
"""RQ task"""fromdataclassesimportdataclassfromtypingimportAny, Callable, Protocol, runtime_checkableimportrqfromredisimportRedisfromrq.jobimportJob@runtime_checkableclassSuccessCallback(Protocol):
"""Protocol for success callbacks."""def__call__(self, job: Job, connection: Redis, *args: Any, **kwargs: Any) ->None: ...
@runtime_checkableclassFailureCallback(Protocol):
"""Protocol for failure callbacks."""def__call__(
self,
job: Job,
connection: Redis,
type: Any, # pylint: disable=redefined-builtinvalue: Any,
traceback: Any,
) ->None: ...
@runtime_checkableclassStoppedCallback(Protocol):
"""Protocol for stopped callbacks."""def__call__(self, job: Job, connection: Redis) ->None: ...
@dataclassclassTask:
"""Template class for a task to be submitted to an RQ queue."""queue: str|rq.Queuehandler: Callableargs: tuple|None=Nonekwargs: dict[str, Any] |None=Noneon_success: SuccessCallback|None=Noneon_failure: FailureCallback|None=Noneon_stopped: StoppedCallback|None=Nonedefsubmit(self) ->Job:
"""Submit the task to the queue."""ifisinstance(self.queue, str):
self.queue=rq.Queue(self.queue)
returnself.queue.enqueue(
self.handler,
args=self.args,
kwargs=self.kwargs,
on_success=self.on_success,
on_failure=self.on_failure,
on_stopped=self.on_stopped,
)
The text was updated successfully, but these errors were encountered:
The current implementation of callbacks is quite confusing. We must create a callback with the
Callback
class, while the class doesn't specify what keyword parameters should be included in a callback function. The only way to learn about the callback function protocol is to use the documentation.To avoid using invalid callback functions in my project, I had to create protocols to enforce the format of the callback function.
This provides nice static-checking capability to my library. Invalid callback functions will be detected by the type checker and errors will be raised in the editor.
I would suggest implement something similar in the
rq
library.The text was updated successfully, but these errors were encountered: