Summary
worker_ml/core.py:31-36 inserts into prediction and prediction_latest tables via two separate repository calls that each commit independently. If the second commit fails, the prediction table is updated but prediction_latest is stale — data inconsistency.
Proposed Change
Two parts:
1. Add auto_commit parameter to database component repository methods
Add an auto_commit: bool = True parameter to repository insert/update methods so callers can control commit timing:
async def insert(self, session, predictions, auto_commit=True):
...
if auto_commit:
await session.commit()
This enables callers to wrap multiple repo operations in a single transaction.
2. Provide a unified prediction service in the database component
Add a PredictionService (or similar) in components/bot_detector/database/prediction/ that inserts into both prediction and prediction_latest tables atomically. This keeps the transaction control in the component layer instead of the base:
async def insert_predictions(self, session, predictions):
await self.prediction_repo.insert(session, predictions, auto_commit=False)
await self.prediction_latest_repo.insert(session, predictions, auto_commit=False)
await session.commit()
This way bases just call one method and get atomicity for free.
Summary
worker_ml/core.py:31-36inserts intopredictionandprediction_latesttables via two separate repository calls that each commit independently. If the second commit fails, thepredictiontable is updated butprediction_latestis stale — data inconsistency.Proposed Change
Two parts:
1. Add
auto_commitparameter to database component repository methodsAdd an
auto_commit: bool = Trueparameter to repository insert/update methods so callers can control commit timing:This enables callers to wrap multiple repo operations in a single transaction.
2. Provide a unified prediction service in the database component
Add a
PredictionService(or similar) incomponents/bot_detector/database/prediction/that inserts into bothpredictionandprediction_latesttables atomically. This keeps the transaction control in the component layer instead of the base:This way bases just call one method and get atomicity for free.