2

I have a long running Fabric task (~30mins) executed from Django.

An example code is here:

from django.http import HttpResponse
from fabric.tasks import execute
from fabric.api import sudo

@task
def runcmd():
    result = sudo('ls -l')


def executer(request):
    result = execute(
        runcmd,
        hosts=['localhost']
    )
    return HttpResponse(result['localhost'])

The problem here I don't want to wait to show the logs (stdout) for 30 minutes. I can get the result after the task completed. I just need the capture stdout while the task is running and send it to client.

I tried StreamingHttpResponse and ajax but didn't worked.

How can I solve this or is there any alternatives for that?

Regards,

1 Answers1

2

You can wrap the Fabric functions run/sudo with your own version and override their stdout there.

Docs says:

To override which local streams are used to display remote stdout and/or stderr, specify stdout or stderr. (By default, the regular sys.stdout and sys.stderr Python stream objects are used.)

Then, you can pass a StringIO (an in-memory file-like object) instance to the stdout parameter. Watch the StringIO instance for new lines and when a new line is arrived you can yield it to the StreamingHttpResponse (see this answer for an example).

Community
  • 1
  • 1
Ertuğ Karamatlı
  • 898
  • 1
  • 6
  • 7