Skip to content

Make prediction inserts atomic with auto_commit control and unified service #129

@extreme4all-ai

Description

@extreme4all-ai

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions