Skip to content

Commit

Permalink
catch errors in watchdog
Browse files Browse the repository at this point in the history
  • Loading branch information
TShapinsky committed Aug 24, 2023
1 parent 60eb92e commit 1e3f2eb
Showing 1 changed file with 28 additions and 24 deletions.
52 changes: 28 additions & 24 deletions alfalfa_bacnet_bridge/alfalfa_watchdog.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,32 +51,36 @@ async def main_loop(host: str, alfalfa_site: str, command: str):
client = AlfalfaClient(host)
old_site_id = None
child_process:Popen = None


while True:
site_id = get_site_id(client, alfalfa_site)

if site_id != None and (site_id != old_site_id or not is_process_alive(child_process)):
logger.info(f"Found new site with ID: '{site_id}'")
status = client.status(site_id)
logger.info(f"Site status is: '{status}'")
if status == "running":
if is_process_alive(child_process):
logger.info(f"Killing old child process: '{child_process.pid}'")
child_process.kill()
elif child_process != None:
logger.info(f"Process '{child_process.pid}' died, restarting process")
child_process = Popen(["python", "-u", command, host, site_id])
logger.info(f"Spawned new child process: '{child_process.pid}'")
old_site_id = site_id

if site_id and is_process_alive(child_process) and client.status(site_id) != "running":
logger.info(f"Killing old child process: '{child_process.pid}'")
child_process.kill()

elif site_id == None:
logger.info(f"No site found with identifier: '{alfalfa_site}'")

try:
site_id = get_site_id(client, alfalfa_site)

if site_id != None and (site_id != old_site_id or not is_process_alive(child_process)):
logger.info(f"Found new site with ID: '{site_id}'")
status = client.status(site_id)
logger.info(f"Site status is: '{status}'")
if status == "running":
if is_process_alive(child_process):
logger.info(f"Killing old child process: '{child_process.pid}'")
child_process.kill()
elif child_process != None:
logger.info(f"Process '{child_process.pid}' died, restarting process")
child_process = Popen(["python", "-u", command, host, site_id])
logger.info(f"Spawned new child process: '{child_process.pid}'")
old_site_id = site_id

if site_id and is_process_alive(child_process) and client.status(site_id) != "running":
logger.info(f"Killing old child process: '{child_process.pid}'")
child_process.kill()

elif site_id == None:
logger.info(f"No site found with identifier: '{alfalfa_site}'")

except Exception as e:
print(e)

await asyncio.sleep(5)

if __name__ == "__main__":
Expand Down

0 comments on commit 1e3f2eb

Please sign in to comment.