Skip to content
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

Add on_success and on_failure callback kwargs #118

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mzpqnxow
Copy link

@mzpqnxow mzpqnxow commented Oct 30, 2021

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
Copy link
Author

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

@amks1
Copy link

amks1 commented Feb 19, 2022

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

@mzpqnxow
Copy link
Author

mzpqnxow commented Apr 2, 2024

Any chance this might be merged?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants