Reputation: 53
I have a tasks.py file that looks like this:
from celery import Celery
import subprocess
from flask import flash
app = Celery('tasks', backend='amqp', broker='amqp://guest@localhost//')
@app.task(track_started=True)
def move_files(upRepo, filedir):
subprocess.call(['aptly', 'repo', 'add', upRepo, filedir])
@app.task(track_started=True)
def make_snapshot(existRepoName, snapName):
subprocess.call(['aptly', 'snapshot', 'create', snapName, 'from', 'repo', existRepoName])
The tasks go through fine, and when the subprocess.call
command succeeds everything works, it's just when subprocess.call
fails, the task still technically goes through, so Celery reports this as a 'Success'.
I've been looking at after_return
in the docs, but I'm not sure if that's what I want, nor do I really know how I would implement that...
thanks!
Upvotes: 0
Views: 520
Reputation: 15170
try subprocess.check_call
-- it'll throw an exception if something goes wrong.
test case:
import subprocess
subprocess.check_call('false')
Traceback (most recent call last):
File "zcheck.py", line 3, in <module>
subprocess.check_call('false')
File "/usr/lib/python2.7/subprocess.py", line 540, in check_call
raise CalledProcessError(retcode, cmd)
subprocess.CalledProcessError: Command 'false' returned non-zero exit status 1
For the original code, this becomes:
from celery import Celery
import subprocess
from flask import flash
app = Celery('tasks', backend='amqp', broker='amqp://guest@localhost//')
@app.task(track_started=True)
def move_files(upRepo, filedir):
subprocess.check_call(['aptly', 'repo', 'add', upRepo, filedir])
@app.task(track_started=True)
def make_snapshot(existRepoName, snapName):
subprocess.check_call(['aptly', 'snapshot', 'create', snapName, 'from', 'repo', existRepoName])
Upvotes: 1