Skip to content

Commit

Permalink
Periodic (not instant) saving of the channel state
Browse files Browse the repository at this point in the history
  • Loading branch information
lavadk committed Apr 6, 2021
1 parent 4948835 commit 2ea41ad
Show file tree
Hide file tree
Showing 5 changed files with 23 additions and 8 deletions.
3 changes: 2 additions & 1 deletion liker/setup/dependencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
15 changes: 12 additions & 3 deletions liker/state/channel_state.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,29 @@
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
from liker.state.markup_trail import MarkupTrail
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)

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()
7 changes: 4 additions & 3 deletions liker/state/reaction_hashes.py
Original file line number Diff line number Diff line change
@@ -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:
Expand Down
4 changes: 4 additions & 0 deletions liker/state/space_state.py
Original file line number Diff line number Diff line change
Expand Up @@ -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()
2 changes: 1 addition & 1 deletion tengine

0 comments on commit 2ea41ad

Please sign in to comment.