rundeck-plugins / docker

Providers for docker
28 stars 23 forks source link

Shell output is not in realtime #17

Closed ryancurrah closed 6 years ago

ryancurrah commented 6 years ago

When running a docker exec or docker run the output is not printed to stdout in realtime. This makes it hard to debug a hanging command. I propose the ability to print stdout and stderr from the subprocess command in realtime.

This would require a while loop to poll() the status of the command execution and print the results to stdout instead of the wait() function.

ahonor commented 6 years ago

@ryancurrah I wonder if it is due to buffering.

ryancurrah commented 6 years ago

This is because we are waiting for the command to finish executing then printing the result to stdout see line 101.

I tried some code just now and it worked. As our command was running and printing lines it was appearing in the Rundeck console at the same time instead of it all appearing once the command finished. Below is the snippet....

p = Popen(
    shlex.split(command_string),
    shell=False,
    stdout=PIPE,
    stderr=PIPE
)

# Print lines to stdout while waiting for command to finish
while True:
    line = p.stdout.readline().rstrip()
    if line:
        print line
    if p.poll() is not None:
        break

exitcode = p.poll()

if exitcode != 0:
    log.error('Command execution failed with exit code: %s' % str(exitcode))
ahonor commented 6 years ago

@ryancurrah Thanks for that code example. If it works well, create a pull request.

ryancurrah commented 6 years ago

That was the plan. I'm going to test for a bit first, if it works well I will open a PR and close this issue.