Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
Florian Schaeffeler committed Oct 31, 2019
1 parent 97d3ec0 commit 7ec217b
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 0 deletions.
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
retrying
Empty file added retryer/__init__.py
Empty file.
39 changes: 39 additions & 0 deletions retryer/retryer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import argparse
import os
import subprocess
import sys
from tenacity import retry, wait_random_exponential, stop_after_attempt, after_log

import logging

logging.basicConfig(stream=sys.stdout, level=logging.INFO)
logger = logging.getLogger(__name__)


@retry(
wait=wait_random_exponential(multiplier=1, max=30),
after=after_log(logger, logging.INFO),
)
def execute(cmd: list):
logger.debug(f"execute: {cmd}")
cp: subprocess.CompletedProcess = subprocess.run(
cmd, shell=True, check=True, stdout=subprocess.PIPE)
if cp.stdout:
sys.stdout.write(str(cp.stdout, 'utf-8'))
if cp.stderr:
sys.stderr.write(str(cp.stderr, 'utf-8'))

logger.debug(f"exitcode was: {cp.returncode}")
if cp.returncode != 0:
raise Exception(f"execution of [{cp.args}] failed")


if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Retryer")
parser.add_argument("cmd", type=str, nargs="+")
parser.add_argument(
"--retries", type=int, help="retries", default=3,
)

args = parser.parse_args()
sys.exit(execute.retry_with(stop=stop_after_attempt(args.retries))(args.cmd))
13 changes: 13 additions & 0 deletions retryer_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import pytest
from tenacity import stop_after_attempt

from retryer import retryer


@pytest.mark.parametrize("cmd", [
["echo", "hello" ,"world"],
["echo", "'test'"]
])
def test_retry_cmd_call_succeeds(cmd):
retryer.execute.retry_with(stop=stop_after_attempt(0))(cmd)
retryer.execute(cmd)

0 comments on commit 7ec217b

Please sign in to comment.