Skip to content

Commit

Permalink
Store healthcheck indicator in db
Browse files Browse the repository at this point in the history
  • Loading branch information
richardr1126 committed Jan 16, 2025
1 parent ed9575e commit 8d3fd24
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 10 deletions.
19 changes: 9 additions & 10 deletions firehose/data_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
models.AppBskyFeedPost: models.ids.AppBskyFeedPost,
}

LAST_INDEXED_AT = None

def is_healthy():
if not LAST_INDEXED_AT:
return False
return (datetime.now() - LAST_INDEXED_AT) < timedelta(minutes=5)
with db.connection_context():
state = SubscriptionState.get_or_none()
if not state or not state.last_indexed_at:
return False
return (datetime.now() - state.last_indexed_at) < timedelta(minutes=5)

def _get_ops_by_type(commit: models.ComAtprotoSyncSubscribeRepos.Commit) -> defaultdict:
"""
Expand Down Expand Up @@ -172,11 +172,10 @@ def on_message_handler(message: firehose_models.MessageFrame) -> None:
client.update_params(models.ComAtprotoSyncSubscribeRepos.Params(cursor=commit.seq))
# Persist the new cursor in the database
with db.atomic():
SubscriptionState.update(cursor=commit.seq).where(SubscriptionState.service == name).execute()

# For health check
global LAST_INDEXED_AT
LAST_INDEXED_AT = datetime.now()
SubscriptionState.update(
cursor=commit.seq,
last_indexed_at=datetime.now()
).where(SubscriptionState.service == name).execute()


if not commit.blocks:
Expand Down
1 change: 1 addition & 0 deletions firehose/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Post(BaseModel):
class SubscriptionState(BaseModel):
service = peewee.CharField(unique=True)
cursor = peewee.BigIntegerField()
last_indexed_at = peewee.DateTimeField(null=True, default=None)

class SessionState(BaseModel):
service = peewee.CharField(unique=True)
Expand Down
1 change: 1 addition & 0 deletions scheduler/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Post(BaseModel):
class SubscriptionState(BaseModel):
service = peewee.CharField(unique=True)
cursor = peewee.BigIntegerField()
last_indexed_at = peewee.DateTimeField(null=True, default=None)

class SessionState(BaseModel):
service = peewee.CharField(unique=True)
Expand Down
1 change: 1 addition & 0 deletions web/database_ro.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ class Post(BaseModel):
class SubscriptionState(BaseModel):
service = peewee.CharField(unique=True)
cursor = peewee.BigIntegerField()
last_indexed_at = peewee.DateTimeField(null=True, default=None)

class SessionState(BaseModel):
service = peewee.CharField(unique=True)
Expand Down

0 comments on commit 8d3fd24

Please sign in to comment.