Skip to content

Commit f5267fb

Browse files
authored
Merge pull request #484 from yungwine/elections
add logs backup, db backup check, ensure adding validator temp key
2 parents ac7fa24 + 820a195 commit f5267fb

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

mytoncore/functions.py

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#!/usr/bin/env python3
22
# -*- coding: utf_8 -*-l
3+
import datetime
34
import os
45
import sys
56
import psutil
@@ -9,6 +10,7 @@
910
import requests
1011
import subprocess
1112

13+
from mypylib import MyPyClass
1214
from mytoncore.mytoncore import MyTonCore
1315
from mytonctrl.utils import fix_git_config
1416
from 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+
670698
def 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

mytoncore/mytoncore.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1456,6 +1456,7 @@ def ElectionEntry(self, args=None):
14561456

14571457
# Create keys
14581458
validatorKey = self.GetValidatorKeyByTime(startWorkTime, endWorkTime)
1459+
self.AddKeyToTemp(validatorKey, endWorkTime) # add one more time to ensure it is in temp keys
14591460
validatorPubkey_b64 = self.GetPubKeyBase64(validatorKey)
14601461

14611462
# Attach ADNL addr to validator

0 commit comments

Comments
 (0)