-
Notifications
You must be signed in to change notification settings - Fork 22
Expand file tree
/
Copy pathrun_neuron.py
More file actions
92 lines (75 loc) · 3.1 KB
/
run_neuron.py
File metadata and controls
92 lines (75 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
Thank you to Namoray of SN19 for their autoupdate implementation!
"""
import os
import sys
import subprocess
import time
import argparse
# self heal restart interval
RESTART_INTERVAL_HOURS = 3
def should_update_local(local_commit, remote_commit):
return local_commit != remote_commit
def run_auto_update_self_heal(neuron_type, auto_update, self_heal):
last_restart_time = time.time()
while True:
time.sleep(120)
if auto_update:
current_branch = subprocess.getoutput("git rev-parse --abbrev-ref HEAD")
local_commit = subprocess.getoutput("git rev-parse HEAD")
os.system("git fetch")
remote_commit = subprocess.getoutput(
f"git rev-parse origin/{current_branch}"
)
if should_update_local(local_commit, remote_commit):
print("Local repo is not up-to-date. Updating...")
reset_cmd = "git reset --hard " + remote_commit
process = subprocess.Popen(reset_cmd.split(), stdout=subprocess.PIPE)
output, error = process.communicate()
if error:
print("Error in updating:", error)
else:
print("Updated local repo to latest version: {}".format(remote_commit))
print("Running the autoupdate steps...")
# Trigger shell script. Make sure this file path starts from root
os.system(f"./setup.sh")
time.sleep(20)
print("Finished running the autoupdate steps 😎")
print("Restarting neuron")
os.system(f"./start_{neuron_type}.sh")
else:
print("Repo is up-to-date.")
if self_heal:
# Check if it's time to restart the PM2 process
if time.time() - last_restart_time >= RESTART_INTERVAL_HOURS * 3600:
os.system(f"./start_{neuron_type}.sh")
last_restart_time = time.time() # Reset the timer after the restart
if __name__ == "__main__":
parser = argparse.ArgumentParser(
description="Bittensor neuron run script with optional self-healing and auto-update."
)
parser.add_argument("--validator", action="store_true")
parser.add_argument("--miner", action="store_true")
parser.add_argument(
"--self-heal",
action="store_true",
help="Periodically restart the PM2 processes.",
)
parser.add_argument(
"--no-auto-update",
action="store_true",
help="Disable the automatic update of the local repository",
)
args = parser.parse_args()
if not (args.miner ^ args.validator):
print(
f"Usage: python {__file__}"
+ "--validator | --miner [--self-heal --no-auto-update]"
)
sys.exit(1)
neuron_type = "miner" if args.miner else "validator"
os.system(f"./start_{neuron_type}.sh")
if not args.no_auto_update or args.self_heal:
run_auto_update_self_heal(
neuron_type, auto_update=not args.no_auto_update, self_heal=args.self_heal
)