shell_command: Add retries to "run_with_response" method

Signed-off-by:  Eric Callahan <arksine.code@gmail.com>
This commit is contained in:
Arksine 2021-01-01 20:34:02 -05:00
parent 67f992dabd
commit c261ee51f3
1 changed files with 11 additions and 3 deletions

View File

@ -7,7 +7,6 @@ import os
import shlex import shlex
import subprocess import subprocess
import logging import logging
import tornado
from tornado import gen from tornado import gen
from tornado.ioloop import IOLoop from tornado.ioloop import IOLoop
@ -100,7 +99,7 @@ class ShellCommand:
logging.info(msg) logging.info(msg)
return success return success
async def run_with_response(self, timeout=2.): async def run_with_response(self, timeout=2., retries=1):
result = [] result = []
def cb(data): def cb(data):
@ -109,7 +108,16 @@ class ShellCommand:
result.append(data.decode()) result.append(data.decode())
prev_cb = self.output_cb prev_cb = self.output_cb
self.output_cb = cb self.output_cb = cb
await self.run(timeout) while 1:
ret = await self.run(timeout)
if not ret or not result:
retries -= 1
if not retries:
return None
await gen.sleep(.5)
result.clear()
continue
break
self.output_cb = prev_cb self.output_cb = prev_cb
return "\n".join(result) return "\n".join(result)