Flask-RQ2 icon indicating copy to clipboard operation
Flask-RQ2 copied to clipboard

Add on_success and on_failure callback kwargs

Open mzpqnxow opened this issue 4 years ago • 3 comments

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")

...

mzpqnxow avatar Oct 30 '21 19:10 mzpqnxow

@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!

mzpqnxow avatar Nov 20 '21 13:11 mzpqnxow

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

amks1 avatar Feb 19 '22 16:02 amks1

Any chance this might be merged?

mzpqnxow avatar Apr 02 '24 03:04 mzpqnxow