From 2ea41adecab79f47ee70230923fdef5afb7c68cf Mon Sep 17 00:00:00 2001 From: lavadk Date: Wed, 7 Apr 2021 00:02:55 +0300 Subject: [PATCH] Periodic (not instant) saving of the channel state --- liker/setup/dependencies.py | 3 ++- liker/state/channel_state.py | 15 ++++++++++++--- liker/state/reaction_hashes.py | 7 ++++--- liker/state/space_state.py | 4 ++++ tengine | 2 +- 5 files changed, 23 insertions(+), 8 deletions(-) diff --git a/liker/setup/dependencies.py b/liker/setup/dependencies.py index d6b8c71..cc10308 100644 --- a/liker/setup/dependencies.py +++ b/liker/setup/dependencies.py @@ -15,7 +15,8 @@ def bind_app_dependencies(binder: Binder): binder.bind_to_constructor(App, lambda: App(update_funcs=[inject.instance(TelegramInboxHub).update, inject.instance(MarkupSynchronizer).update, - inject.instance(AbuseJanitor).update], + inject.instance(AbuseJanitor).update, + inject.instance(SpaceState).update], update_seconds=constants.UPDATE_SECONDS, restart_seconds=constants.RESTART_SECONDS)) binder.bind(Config, Config(config_path=constants.config_path(), diff --git a/liker/state/channel_state.py b/liker/state/channel_state.py index ca56371..9bc9e3b 100644 --- a/liker/state/channel_state.py +++ b/liker/state/channel_state.py @@ -1,5 +1,7 @@ from pathlib import Path -from tengine.state.preserver import Preserver +import inject +from tengine.state.timed_preserver import TimedPreserver +from tengine import Config from liker.state.reaction_hashes import ReactionHashes from liker.state.markup_queue import MarkupQueue @@ -7,10 +9,13 @@ from liker.state.comment_trail import CommentTrail -class ChannelState(Preserver): +class ChannelState(TimedPreserver): + config = inject.attr(Config) + def __init__(self, state_dir: Path, str_channel_id: str): state_path = state_dir / f'{str_channel_id}.json' - super().__init__(state_path) + save_period = self.config['channel_state_save_seconds'] + super().__init__(state_path, save_period=save_period) last_reactions_path = state_dir / f'{str_channel_id}_last_reactions.json' self.last_reactions = ReactionHashes(last_reactions_path) @@ -18,3 +23,7 @@ def __init__(self, state_dir: Path, str_channel_id: str): self.markup_queue = MarkupQueue(self.state) self.markup_trail = MarkupTrail(self.state) self.comment_trail = CommentTrail(self.state) + + def update(self): + super().update() + self.last_reactions.update() diff --git a/liker/state/reaction_hashes.py b/liker/state/reaction_hashes.py index 453e292..8e5b02e 100644 --- a/liker/state/reaction_hashes.py +++ b/liker/state/reaction_hashes.py @@ -1,13 +1,14 @@ import inject from tengine import Config -from tengine.state.preserver import * +from tengine.state.timed_preserver import * -class ReactionHashes(Preserver): +class ReactionHashes(TimedPreserver): config = inject.attr(Config) def __init__(self, state_file_path: Path): - super().__init__(state_file_path) + save_period = self.config['last_reactions_save_seconds'] + super().__init__(state_file_path, save_period=save_period) def has(self, reaction_hash: str) -> bool: if 'hashes' not in self.state: diff --git a/liker/state/space_state.py b/liker/state/space_state.py index af6d114..1255e4f 100644 --- a/liker/state/space_state.py +++ b/liker/state/space_state.py @@ -23,3 +23,7 @@ def ensure_channel_state(self, str_channel_id: str) -> ChannelState: self.channels[str_channel_id] = ChannelState(state_dir=self.state_dir, str_channel_id=str_channel_id) return self.channels[str_channel_id] + + def update(self): + for ch in self.channels.values(): + ch.update() diff --git a/tengine b/tengine index 436bc52..3ff8266 160000 --- a/tengine +++ b/tengine @@ -1 +1 @@ -Subproject commit 436bc52535144a50237286d51d308a491e81e897 +Subproject commit 3ff8266b7339e5d6d7efbe0a9f1986a3c31b6d75