Skip to content

Commit

Permalink
Merge pull request #30 from CameronRP/add-schedule-retry
Browse files Browse the repository at this point in the history
Add retry for running commands
  • Loading branch information
CameronRP authored Nov 8, 2024
2 parents 9d271de + de1c9cd commit 0ed1ec0
Showing 1 changed file with 18 additions and 7 deletions.
25 changes: 18 additions & 7 deletions salt/schedule-commands/schedule-commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@
import subprocess
import sys
import shlex
import time

import salt.client

from salt_listener import SaltListener

COMMAND_FILE = "/opt/ops-tools/salt/commands.txt"
COMMAND_FILE = "/opt/ops-tools/salt/schedule-commands/commands.txt"


def main():
Expand All @@ -29,12 +30,22 @@ def main():
print("listening for minion ping events")
for minion_id in minion_ids:
command = getMinionCommand(minion_id)
while command != None:
print(f"'{minion_id}' connected, running '{command}'")
result = subprocess.run(command.split(), capture_output=True, text=True)
print(result.stdout)
print(result.stderr)

if command != None:
command_success = False
for i in range(0, 5): # Try 5 times to run command. If it fails, just skip it.
print(f"'{minion_id}' connected, running '{command}'")
result = subprocess.run(command.split(), capture_output=True, text=True)
print(result.stdout)
print(result.stderr)
if result.returncode == 0:
print("Command ran successfully")
break
else:
if i == 4:
print("Command failed")
else:
print(f"Command failed, waiting 10 seconds and trying {4-i} more times")
time.sleep(10)

def getMinionCommand(minion_id):
with open(COMMAND_FILE, "r") as file:
Expand Down

0 comments on commit 0ed1ec0

Please sign in to comment.