#!/usr/bin/env python3

import json
import subprocess
import time
import logging

logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')

def check_process_status():
    status = 'READY'
    output = subprocess.check_output(['ps', 'ax', '-ocomm']).decode("utf-8").strip()

    if 'orted' in output:
        status = 'BUSY'
    
    if 'sshd' not in output:
        status = 'ERROR'

    return status


def update_worker_status():
    while True:
        status = check_process_status()

        data = {"status": status, "timestamp": int(time.time())}
        with open(
                "/competition/worker_node_status.json",
                "w+",
        ) as statusfile:
            statusfile.write(json.dumps(data))
        time.sleep(1)


if __name__ == "__main__":
    update_worker_status()