From adaf7047065861339ec7349cfe0904e17a75ed44 Mon Sep 17 00:00:00 2001 From: yungwine Date: Tue, 27 Feb 2024 12:25:14 +0700 Subject: [PATCH 1/4] add offers garbage collector --- mytoncore/mytoncore.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index a92eb1bd..723bc685 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -2950,13 +2950,22 @@ def WriteBookmarkData(self, bookmark): bookmark["data"] = data #end define + def offers_gc(self, save_offers): + current_offers = self.GetOffers() + current_offers_hashes = [offer.get("hash") for offer in current_offers] + for offer in save_offers: + if offer not in current_offers_hashes: + save_offers.pop(offer) + return save_offers + def GetSaveOffers(self): bname = "saveOffers" - saveOffers = self.local.db.get(bname) - if saveOffers is None: - saveOffers = dict() - self.local.db[bname] = saveOffers - return saveOffers + save_offers = self.local.db.get(bname) + if save_offers is None: + save_offers = dict() + self.local.db[bname] = save_offers + self.offers_gc(save_offers) + return save_offers #end define def AddSaveOffer(self, offer): From 1cb220412201427259db7532977efa9e4eb460a1 Mon Sep 17 00:00:00 2001 From: yungwine Date: Tue, 27 Feb 2024 12:41:06 +0700 Subject: [PATCH 2/4] add deleting old files from temp dir --- mytoncore/mytoncore.py | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index 723bc685..ef50af45 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -1495,10 +1495,26 @@ def ElectionEntry(self, args=None): # Save vars to json file self.SaveElectionVarsToJsonFile(wallet=wallet, account=account, stake=stake, maxFactor=maxFactor, fullElectorAddr=fullElectorAddr, startWorkTime=startWorkTime, validatorsElectedFor=validatorsElectedFor, endWorkTime=endWorkTime, validatorKey=validatorKey, validatorPubkey_b64=validatorPubkey_b64, adnlAddr=adnl_addr, var1=var1, validatorSignature=validatorSignature, validatorPubkey=validatorPubkey) - self.local.add_log("ElectionEntry completed. Start work time: " + str(startWorkTime)) + + self.clear_tmp() + #end define + def clear_tmp(self): + start = time.time() + count = 0 + dir = self.tempDir + for f in os.listdir(dir): + ts = f.split('_')[0] + if ts.isdigit(): + ts = int(ts) + week = 60 * 60 * 24 * 7 + if ts < time.time() - week: + count += 1 + os.remove(os.path.join(dir, f)) + self.local.add_log(f"Removed {count} old files from tmp dir for {int(time.time() - start)} seconds", "info") + def GetValidatorKeyByTime(self, startWorkTime, endWorkTime): self.local.add_log("start GetValidatorKeyByTime function", "debug") # Check temp key From d19883ab73cc241b32724228e01388359721d907 Mon Sep 17 00:00:00 2001 From: yungwine Date: Tue, 27 Feb 2024 12:48:26 +0700 Subject: [PATCH 3/4] improves --- mytoncore/mytoncore.py | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index ef50af45..913c4570 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -1504,15 +1504,21 @@ def ElectionEntry(self, args=None): def clear_tmp(self): start = time.time() count = 0 + week_ago = 60 * 60 * 24 * 7 dir = self.tempDir for f in os.listdir(dir): - ts = f.split('_')[0] - if ts.isdigit(): - ts = int(ts) - week = 60 * 60 * 24 * 7 - if ts < time.time() - week: + prefix = f.split('_')[0] + if prefix.isdigit(): + ts = int(prefix) + if ts < time.time() - week_ago: count += 1 os.remove(os.path.join(dir, f)) + elif prefix == 'checkload': + ts = int(f.split('_')[1]) + if ts < time.time() - week_ago: + count += 1 + os.remove(os.path.join(dir, f)) + self.local.add_log(f"Removed {count} old files from tmp dir for {int(time.time() - start)} seconds", "info") def GetValidatorKeyByTime(self, startWorkTime, endWorkTime): From cbdd56cd20efb5b7e48e42929f4b043437230091 Mon Sep 17 00:00:00 2001 From: yungwine Date: Tue, 27 Feb 2024 21:20:34 +0700 Subject: [PATCH 4/4] improves --- mytoncore/mytoncore.py | 15 ++++----------- 1 file changed, 4 insertions(+), 11 deletions(-) diff --git a/mytoncore/mytoncore.py b/mytoncore/mytoncore.py index 913c4570..5a72fc5b 100644 --- a/mytoncore/mytoncore.py +++ b/mytoncore/mytoncore.py @@ -1507,17 +1507,10 @@ def clear_tmp(self): week_ago = 60 * 60 * 24 * 7 dir = self.tempDir for f in os.listdir(dir): - prefix = f.split('_')[0] - if prefix.isdigit(): - ts = int(prefix) - if ts < time.time() - week_ago: - count += 1 - os.remove(os.path.join(dir, f)) - elif prefix == 'checkload': - ts = int(f.split('_')[1]) - if ts < time.time() - week_ago: - count += 1 - os.remove(os.path.join(dir, f)) + ts = os.path.getmtime(os.path.join(dir, f)) + if ts < time.time() - week_ago: + count += 1 + os.remove(os.path.join(dir, f)) self.local.add_log(f"Removed {count} old files from tmp dir for {int(time.time() - start)} seconds", "info")