from datetime import datetime
import pytz
import subprocess
import subprocess

times = [
    {"from": 50000, "to": 60100},
    {"from": 90000, "to": 90100},
    {"from": 120000, "to": 120100},
    {"from": 150000, "to": 150100},
    {"from": 180000, "to": 180100},
    {"from": 210000, "to": 210100},
]

def is_now_in_time_period(now):
    for item in times:
        if now >= item["from"] and now < item["to"]:
            return True
    return False

def main():
    AT = pytz.timezone('Asia/Tehran')
    now = datetime.now(AT)
    current_time = int(now.strftime("%H%M%S"))
    print(str(current_time) + " : check for restart loyalty_app !")
    if is_now_in_time_period(current_time):
        print(str(current_time) + " : restart loyalty_app")
        bashCommand = "docker restart loyalty_app"
        process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
        output, error = process.communicate()

        bashCommand = "docker exec -u 0 loyalty_app service supervisor start"
        process = subprocess.Popen(bashCommand.split(), stdout=subprocess.PIPE)
        output, error = process.communicate()

if __name__ == "__main__":
   main()
