1

我正在运行一个简单的任务,从 django 视图触发:

task = mock_deploy.delay()

mock_deploy 定义为:

from celery.decorators import task as ctask
from project.fabscripts.task.mock import *

@ctask(name="mock_deploy")
def mock_deploy():
    print "hi form celery task b4 mockdeploy 1234"
    output = execute(mock_deploy2)
    return "out: %s" % (output)

而fabric任务本身定义为:

@task
def mock_deploy2():
    lrun("ls -l /")
    lrun("ifconfig eth0")
    # i need to get the full output from those commands and save them to db

现在......我试图替换标准输出,覆盖结构执行功能:

def execute(task):
    output = StringIO()
    error = StringIO()
    sys.stdout = output
    sys.stderr = error
    task()
    sys.stdout = sys.__stdout__
    sys.stderr = sys.__stderr__
    return (output.getvalue(), error.getvalue())

我试图在织物任务中替换标准输出。不管我做了什么,我得到的唯一输出是第一行“面料想要做什么”

out: [localhost] local: ls -l / 

然后, ls 命令的整个输出在 celery 日志中打印得非常好。除了缺少一行 out: [localhost] local: ls -l / `9the one 我设法得到作为输出)

[2012-06-14 21:33:56,587: DEBUG/MainProcess] TaskPool: Apply <function execute_and_trace at 0x36710c8> (args:('mock_deploy', '2a90d920-130a-4942-829b-87f4d5ebe80f', [], {}) kwargs:{'hostname': 's16079364', 'request': {'retries': 0, 'task': 'mock_deploy', 'utc': False, 'loglevel': 10, 'delivery_info': {'routing_key': u'celery', 'exchange': u'celery'}, 'args': [], 'expires': None, 'is_eager': False, 'eta': None, 'hostname': 's16079364', 'kwargs': {}, 'logfile': None, 'id': '2a90d920-130a-4942-829b-87f4d5ebe80f'}})
[2012-06-14 21:33:56,591: DEBUG/MainProcess] Task accepted: mock_deploy[2a90d920-130a-4942-829b-87f4d5ebe80f] pid:22214
hi form celery task b4 mockdeploy 1234
total 3231728
-rw-r--r--   1 root root 3305551148 2012-06-13 14:43 dumpling.sql
drwxr-xr-x   2 root root       4096 2012-05-09 17:42 bin
drwxr-xr-x   4 root root       4096 2012-02-14 15:21 boot
drwxr-xr-x   2 root root       4096 2012-03-09 14:10 build
drwxr-xr-x   2 root root       4096 2010-05-11 19:58 cdrom
-rw-------   1 root root    2174976 2012-05-23 11:23 core
drwxr-xr-x  15 root root       4080 2012-06-11 12:55 dev
drwxr-xr-x 135 root root      12288 2012-06-14 21:15 etc
drwxr-xr-x   6 root root         77 2012-05-21 14:41 home

...

一个可怕的解决方法是包装一个结构运行命令,在每个命令上添加一个“>/tmp/logfile.log”,然后当任务完成时,我会用 scp 检索文件......

简而言之,我的问题是如何在用芹菜触发时获得织物任务的完整输出?

以下是诀窍:

@ctask(name="mock_deploy")
def mock_deploy():
    env.roledefs.update({'remote':  ['root@1.1.1.1',]})
    output = StringIO()
    sys.stdout = output
    execute(mock_deploy2)
    sys.stdout = sys.__stdout__
    return output.getvalue()
4

0 回答 0