-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2631 from danswer-ai/hotfix/v0.6-heartbeat
Hotfix/v0.6 heartbeat
- Loading branch information
Showing
10 changed files
with
324 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import abc | ||
from typing import Any | ||
|
||
from sqlalchemy import func | ||
from sqlalchemy.orm import Session | ||
|
||
from danswer.db.index_attempt import get_index_attempt | ||
from danswer.utils.logger import setup_logger | ||
|
||
logger = setup_logger() | ||
|
||
|
||
class Heartbeat(abc.ABC): | ||
"""Useful for any long-running work that goes through a bunch of items | ||
and needs to occasionally give updates on progress. | ||
e.g. chunking, embedding, updating vespa, etc.""" | ||
|
||
@abc.abstractmethod | ||
def heartbeat(self, metadata: Any = None) -> None: | ||
raise NotImplementedError | ||
|
||
|
||
class IndexingHeartbeat(Heartbeat): | ||
def __init__(self, index_attempt_id: int, db_session: Session, freq: int): | ||
self.cnt = 0 | ||
|
||
self.index_attempt_id = index_attempt_id | ||
self.db_session = db_session | ||
self.freq = freq | ||
|
||
def heartbeat(self, metadata: Any = None) -> None: | ||
self.cnt += 1 | ||
if self.cnt % self.freq == 0: | ||
index_attempt = get_index_attempt( | ||
db_session=self.db_session, index_attempt_id=self.index_attempt_id | ||
) | ||
if index_attempt: | ||
index_attempt.time_updated = func.now() | ||
self.db_session.commit() | ||
else: | ||
logger.error("Index attempt not found, this should not happen!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from typing import Any | ||
|
||
import pytest | ||
|
||
from danswer.indexing.indexing_heartbeat import Heartbeat | ||
|
||
|
||
class MockHeartbeat(Heartbeat): | ||
def __init__(self) -> None: | ||
self.call_count = 0 | ||
|
||
def heartbeat(self, metadata: Any = None) -> None: | ||
self.call_count += 1 | ||
|
||
|
||
@pytest.fixture | ||
def mock_heartbeat() -> MockHeartbeat: | ||
return MockHeartbeat() |
Oops, something went wrong.