fix: Try to run automation every 5 mins

This commit is contained in:
Nathan Woodburn 2024-03-15 14:59:07 +11:00
parent 171e891555
commit afd7ae2947
Signed by: nathanwoodburn
GPG Key ID: 203B000478AD0EF1

View File

@ -3,7 +3,7 @@ import account
import requests import requests
import threading import threading
import os import os
import datetime import time
APIKEY = os.environ.get("hsd_api") APIKEY = os.environ.get("hsd_api")
ip = os.getenv("hsd_ip") ip = os.getenv("hsd_ip")
@ -37,45 +37,47 @@ functions = {
} }
} }
started = 0 started = False
# Main entry point only lets the main function run every 5 mins # Main entry point only lets the main function run every 5 mins
def automation(params, authentication): def automation(params, authentication):
global started global started
now = datetime.datetime.now().timestamp()
# Add 5 mins if started:
now = now - 300 return {"Status": "Auto Renews running"}
if now < started: started = True
return {"Status": "Waiting before checking for new actions"}
started = datetime.datetime.now().timestamp()
threading.Thread(target=automations_background, args=(authentication,)).start() threading.Thread(target=automations_background, args=(authentication,)).start()
return {"Status": "Checking for actions"} return {"Status": "Started Auto Renews"}
# Background function to run the automations # Background function to run the automations
def automations_background(authentication): def automations_background(authentication):
print("Running automations") while True:
# Get account details print("Running automations")
account_name = account.check_account(authentication) # Get account details
password = ":".join(authentication.split(":")[1:]) account_name = account.check_account(authentication)
password = ":".join(authentication.split(":")[1:])
if account_name == False: if account_name == False:
return { return {
"error": { "error": {
"message": "Invalid account" "message": "Invalid account"
}
} }
}
try: try:
# Try to select and login to the wallet # Try to select and login to the wallet
response = account.hsw.rpc_selectWallet(account_name) response = account.hsw.rpc_selectWallet(account_name)
if response['error'] is not None: if response['error'] is not None:
return return
response = account.hsw.rpc_walletPassphrase(password,10) response = account.hsw.rpc_walletPassphrase(password,10)
if response['error'] is not None: if response['error'] is not None:
return return
# Try to send the batch of all renew, reveal and redeem actions # Try to send the batch of all renew, reveal and redeem actions
requests.post(f"http://x:{APIKEY}@{ip}:12039",json={"method": "sendbatch","params": [[["RENEW"]]]}) requests.post(f"http://x:{APIKEY}@{ip}:12039",json={"method": "sendbatch","params": [[["RENEW"]]]})
requests.post(f"http://x:{APIKEY}@{ip}:12039",json={"method": "sendbatch","params": [[["REVEAL"]]]}) requests.post(f"http://x:{APIKEY}@{ip}:12039",json={"method": "sendbatch","params": [[["REVEAL"]]]})
requests.post(f"http://x:{APIKEY}@{ip}:12039",json={"method": "sendbatch","params": [[["REDEEM"]]]}) requests.post(f"http://x:{APIKEY}@{ip}:12039",json={"method": "sendbatch","params": [[["REDEEM"]]]})
except Exception as e: except Exception as e:
print(e) print(e)
# Sleep for 5 mins before running again
time.sleep(300)