11#!/usr/bin/env python3
22# -*- coding: utf_8 -*-l
3+ import datetime
34import os
45import sys
56import psutil
910import requests
1011import subprocess
1112
13+ from mypylib import MyPyClass
1214from mytoncore .mytoncore import MyTonCore
1315from mytonctrl .utils import fix_git_config
1416from mytoninstaller .config import GetConfig
@@ -667,6 +669,32 @@ def gc_import(local, ton):
667669 local .add_log (f"Removed { removed } import files up to { node_seqno } seqno" , "debug" )
668670
669671
672+ def backup_mytoncore_logs (local : MyPyClass , ton : MyTonCore ):
673+ logs_path = os .path .join (ton .tempDir , 'old_logs' )
674+ os .makedirs (logs_path , exist_ok = True )
675+ for file in os .listdir (logs_path ):
676+ file_path = os .path .join (logs_path , file )
677+ if time .time () - os .path .getmtime (file_path ) < 3600 : # check that last file was created not less than an hour ago
678+ return
679+ now = datetime .datetime .now ().strftime ("%Y_%m_%d_%H_%M_%S" )
680+ log_backup_tmp_path = os .path .join (logs_path , 'mytoncore_log_' + now + '.log' )
681+ subprocess .run (["cp" , local .buffer .log_file_name , log_backup_tmp_path ])
682+ ton .clear_dir (logs_path )
683+
684+
685+ def check_mytoncore_db (local : MyPyClass , ton : MyTonCore ):
686+ try :
687+ local .read_db (local .buffer .db_path )
688+ backup_path = local .buffer .db_path + ".backup"
689+ if not os .path .isfile (backup_path ) or time .time () - os .path .getmtime (backup_path ) > 3600 * 6 :
690+ ton .create_self_db_backup ()
691+ return
692+ except Exception as e :
693+ print (f'Failed to read mytoncore db: { e } ' )
694+ local .add_log (f"Failed to read mytoncore db: { e } " , "error" )
695+ ton .CheckConfigFile (None , None ) # get mytoncore db from backup
696+
697+
670698def General (local ):
671699 local .add_log ("start General function" , "debug" )
672700 ton = MyTonCore (local )
@@ -677,6 +705,9 @@ def General(local):
677705 local .start_cycle (Statistics , sec = 10 , args = (local , ))
678706 local .start_cycle (Telemetry , sec = 60 , args = (local , ton , ))
679707 local .start_cycle (OverlayTelemetry , sec = 7200 , args = (local , ton , ))
708+ local .start_cycle (backup_mytoncore_logs , sec = 3600 * 4 , args = (local , ton , ))
709+ local .start_cycle (check_mytoncore_db , sec = 600 , args = (local , ton , ))
710+
680711 if local .db .get ("onlyNode" ): # mytoncore service works only for telemetry
681712 thr_sleep ()
682713 return
0 commit comments