Skip to content

Commit

Permalink
refactor!: change AppState to StateSnapshot to better express it
Browse files Browse the repository at this point in the history
  • Loading branch information
fubuloubu committed May 30, 2024
1 parent 21e24f9 commit 466f18a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 18 deletions.
4 changes: 2 additions & 2 deletions example.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
from ape_tokens import tokens # type: ignore[import]
from taskiq import Context, TaskiqDepends, TaskiqState

from silverback import AppState, CircuitBreaker, SilverbackApp
from silverback import CircuitBreaker, SilverbackApp, StateSnapshot

# Do this first to initialize your app
app = SilverbackApp()
Expand All @@ -17,7 +17,7 @@


@app.on_startup()
def app_startup(startup_state: AppState):
def app_startup(startup_state: StateSnapshot):
# NOTE: This is called just as the app is put into "run" state,
# and handled by the first available worker
# raise Exception # NOTE: Any exception raised on startup aborts immediately
Expand Down
4 changes: 2 additions & 2 deletions silverback/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
from .application import SilverbackApp
from .exceptions import CircuitBreaker, SilverbackException
from .state import AppState
from .state import StateSnapshot

__all__ = [
"AppState",
"StateSnapshot",
"CircuitBreaker",
"SilverbackApp",
"SilverbackException",
Expand Down
4 changes: 2 additions & 2 deletions silverback/runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from .application import SilverbackApp, SystemConfig, TaskData
from .exceptions import Halt, NoTasksAvailableError, NoWebsocketAvailableError, StartupFailure
from .recorder import BaseRecorder, TaskResult
from .state import AppDatastore, AppState
from .state import AppDatastore, StateSnapshot
from .subscriptions import SubscriptionType, Web3SubscriptionsManager
from .types import TaskType
from .utils import (
Expand Down Expand Up @@ -160,7 +160,7 @@ async def run(self):
self.state = startup_state

else: # use empty state
self.state = AppState(last_block_seen=-1, last_block_processed=-1)
self.state = StateSnapshot(last_block_seen=-1, last_block_processed=-1)

# Execute Silverback startup task before we init the rest
startup_taskdata_result = await run_taskiq_task_wait_result(
Expand Down
26 changes: 14 additions & 12 deletions silverback/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from .types import SilverbackID, UTCTimestamp, utc_now


class AppState(BaseModel):
class StateSnapshot(BaseModel):
# Last block number seen by runner
last_block_seen: int

Expand Down Expand Up @@ -38,7 +38,7 @@ class AppDatastore:
- `SILVERBACK_APP_NAME`: Any alphabetical string valid as a folder name
"""

async def init(self, app_id: SilverbackID) -> AppState | None:
async def init(self, app_id: SilverbackID) -> StateSnapshot | None:
data_folder = (
Path.cwd() / ".silverback-sessions" / app_id.name / app_id.ecosystem / app_id.network
)
Expand All @@ -47,16 +47,18 @@ async def init(self, app_id: SilverbackID) -> AppState | None:
self.state_backup_file = data_folder / "state.json"

return (
AppState.parse_file(self.state_backup_file) if self.state_backup_file.exists() else None
StateSnapshot.parse_file(self.state_backup_file)
if self.state_backup_file.exists()
else None
)

async def set_state(self, state: AppState):
async def save(self, snapshot: StateSnapshot):
if self.state_backup_file.exists():
old_state = AppState.parse_file(self.state_backup_file)
if old_state.last_block_seen > state.last_block_seen:
state.last_block_seen = old_state.last_block_seen
if old_state.last_block_processed > state.last_block_processed:
state.last_block_processed = old_state.last_block_processed

state.last_updated = utc_now()
self.state_backup_file.write_text(state.model_dump_json())
old_snapshot = AppState.parse_file(self.snapshot_backup_file)
if old_snapshot.last_block_seen > snapshot.last_block_seen:
snapshot.last_block_seen = old_snapshot.last_block_seen
if old_snapshot.last_block_processed > snapshot.last_block_processed:
snapshot.last_block_processed = old_snapshot.last_block_processed

snapshot.last_updated = utc_now()
self.state_backup_file.write_text(snapshot.model_dump_json())

0 comments on commit 466f18a

Please sign in to comment.