Skip to content

Commit ae78a32

Browse files
authored
Merge pull request #41 from bigjools/failure_signal_context
Ensure flask context for job_failure handlers
2 parents 634c8da + f566d89 commit ae78a32

File tree

1 file changed

+20
-0
lines changed

1 file changed

+20
-0
lines changed

spinach/contrib/flask_spinach.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,14 @@ def job_finished(*args, job=None, **kwargs):
5353
# This means we didn't create the context. Ignore.
5454
pass
5555

56+
@signals.job_failed.connect_via(namespace)
57+
def job_failed(args, job=None, **kwargs):
58+
if not flask.has_app_context():
59+
ctx = app.app_context()
60+
ctx.push()
61+
flask.g.spinach_ctx = ctx
62+
self.job_failed(job)
63+
5664
@classmethod
5765
def job_started(cls, *args, job=None, **kwargs):
5866
"""Callback for subclasses to receive job_started signals.
@@ -73,6 +81,18 @@ def job_finished(cls, *args, job=None, **kwargs):
7381
"""
7482
pass
7583

84+
@classmethod
85+
def job_failed(cls, *args, job=None, **kwargs):
86+
"""Callback for subclasses to receive job_failed signals.
87+
88+
There's no guarantee of ordering for Signal's callbacks,
89+
so use this callback instead to make sure the app context
90+
was pushed. If the signal is called as part of dead broker
91+
detection, you will need to use this as normal signals may
92+
not have been called with the app context pushed.
93+
"""
94+
pass
95+
7696
@property
7797
def spin(self):
7898
if self.app is not None:

0 commit comments

Comments
 (0)