Flask-RQ2
Flask-RQ2 copied to clipboard
Add on_success and on_failure callback kwargs
As noted in #116 there is no support for on_success or on_failure callbacks. This adds them
I'm using this in my application and it seems to work fine. If someone else would like to test, it can be installed with the following:
Pip
$ pip install 'git+https://github.com/mzpqnxow/Flask-RQ2@failure-success-callbacks#egg=Flask-RQ2' # If your ~/.gitconfig requires HTTPS
$ pip install 'git+ssh://github.com/mzpqnxow/Flask-RQ2@failure-success-callbacks#egg=Flask-RQ2' # If your ~/.gitconfig requires SSH
Manual / from the filesystem using pip or setuptools
$ git clone https://github.com/mzpqnxow/Flask-RQ2 --branch failure-success-callbacks && cd Flask-RQ2
$ pip install .
or ...
$ python setup.py install
As a dependency in setuptools
In setup.cfg, use:
[options]
...
install_requires =
...
Flask-RQ2 @ git+https://github.com/mzpqnxow/Flask-RQ2@failure-success-callbacks#egg=Flask-RQ2
...
As a dependency in requirements.txt only
If using requirements.txt only:
...
git+https://github.com/mzpqnxow/Flask-RQ2@failure-success-callbacks#egg=Flask-RQ2
...
As a dependency in requirements.txt and constraints.txt (the "proper" way to do this)
requirements.txt:
...
Flask-RQ2
...
constraints.txt:
Flask-RQ2 @ git+https://github.com/mzpqnxow/Flask-RQ2@failure-success-callbacks#egg=Flask-RQ2
Caveats
I don't make exhaustive use of all of the features of Flask-RQ2 so it's quite possible I made a mistake. Anyone who may want to test/review is appreciated. This has NOT been reviewed by the Flask-RQ2 devs
Usage
The use is as one would expect, it just adds two kwargs to the queue() function. The usage is as described in #116 , duplicated here:
from flask_rq2 import RQ
from flask import Flask
app = Flask(__name__)
rq = RQ()
rq.init_app(app)
@rq.job()
def do_something():
return "OK"
def report_success(*args, **kargs):
print("success")
def report_failure(*args, **kargs):
print("fail")
def try_to_queue():
do_something.queue(queue="default", on_success = report_success, on_failure = report_failure)
try_to_queue()
Alternately, you can use the following style, opting to specify the callbacks when defining the function- same as with any other kwargs:
...
@rq.job(on_success = report_success, on_failure = report_failure)
def do_something():
return "OK"
...
def try_to_queue():
do_something.queue(queue="default")
...
@jezdez do you have any thoughts on this? Would you consider merging it? I'm currently using my own branch with this (minor but convenient) enhancement and haven't had any issues. I'd love to remove a line from my 'constraints.txt' :)
Thanks!
Thanks a lot for this @mzpqnxow. Do you think you can also add the retry= parameter that RQ added since v1.5?
EDIT: I've done it, the code is here: https://github.com/amks1/Flask-RQ2/tree/rq-retry
Any chance this might be merged?