-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
49 lines (40 loc) · 1.93 KB
/
main.py
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
# Description: Main script for running the bot and updating the dataset at regular intervals.
import subprocess
import time
import sched
import threading
import logging
from db_manage.db_manager import db_manager
from chain_observer.utils.check_thread_status import check_thread_staus
from chain_observer.utils.sentry import init_sentry
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
def run_script(script_name):
"""Runs a specified Python script."""
try:
subprocess.run(['python', script_name], check=True)
except subprocess.CalledProcessError as e:
logging.error(f"Error executing script {script_name}: {e}")
def schedule_task(scheduler, task, interval, *args):
"""Schedules a task to run at regular intervals."""
threading.Thread(target=task, args=args).start()
scheduler.enter(interval, 1, schedule_task, (scheduler, task, interval) + args)
def update_coldkeys():
"""Executes find_validator_coldkey and find_owner_coldkey in sequence."""
if check_thread_staus() == 'not running':
db_manager.update_whole_owner_coldkeys()
db_manager.update_whole_validator_coldkeys()
if __name__ == "__main__":
init_sentry()
bot_interval = 12 # Interval in seconds for running the bot
update_dataset_interval = 86400 # Interval in seconds for updating the dataset (1 day)
initial_delay = 86400 # Delay in seconds before starting the dataset update (1 day)
scheduler = sched.scheduler(time.time, time.sleep)
scheduler.enter(0, 1, schedule_task, (scheduler, run_script, bot_interval, 'run.py'))
scheduler.enter(initial_delay, 1, schedule_task, (scheduler, update_coldkeys, update_dataset_interval))
try:
logging.info("Starting the scheduler.")
scheduler.run()
except KeyboardInterrupt:
logging.info("Scheduler terminated by user.")
except Exception as e:
logging.error(f"An error occurred: {e}")