Skip to content

fix(sla): defer SLA audit events until after transaction commit - #245

Merged
usmanimamu17-create merged 1 commit into
ApexChainx:mainfrom
Magrexy:fix/issue-236-audit-events-after-commit
Aug 18, 2026
Merged

fix(sla): defer SLA audit events until after transaction commit#245
usmanimamu17-create merged 1 commit into
ApexChainx:mainfrom
Magrexy:fix/issue-236-audit-events-after-commit

Conversation

@Magrexy

@Magrexy Magrexy commented Aug 18, 2026

Copy link
Copy Markdown
Contributor

Summary

Closes #236

Why

SLA audit events were logged inside compute_device_sla() before the caller's transaction committed. Since the audit log uses its own database session, these events were persisted immediately — even if the SLA computation failed to commit. This left orphaned audit records describing work that never occurred.

What was built

File Change
app/services/sla_service.py Removed record_sla_settlement_audit_events() calls from the success path of compute_device_sla() (both no-outages and with-outages branches). The exception-handler audit log for failures is kept since failures are independent of transaction state.
app/tasks/sla_tasks.py Added record_sla_settlement_audit_events() calls after _mark_success() — the point where the SLA computation is committed to the database.
tests/test_sla_audit_deferred.py 6 new tests: verifies compute_device_sla() never emits audit events, verifies the failure path still logs, and smoke-tests the record_sla_settlement_audit_events() helper.

Integration changes outside <module>/

No new env vars, feature flags, or imports introduced. Existing record_sla_settlement_audit_events() function signature is unchanged.

Acceptance criteria coverage

  • Audit events are only logged after the transaction commits (success path moved to sla_tasks.py after _mark_success())
  • No orphaned audit records when SLA computation fails (exception-path audit log kept inside compute_device_sla())
  • Tests verify compute_device_sla() never emits audit events directly (test_no_outages_does_not_emit_audit, test_with_outages_does_not_emit_audit)

Deliberately deferred

  • Audit log for the failure path in compute_device_sla() remains inside the function — this is intentional since it doesn't depend on transaction state

Test plan

  • pytest tests/test_sla_audit_deferred.py -v — 6/6 passing

Env vars / Notes

No new env vars. No migration required.

SLA audit events were logged inside compute_device_sla() before the
caller's transaction committed. If the commit failed, the audit log
would contain records for a computation that never persisted.

- Removed audit_event logging from compute_device_sla() for the
  success path (both no-outages and with-outages branches).
- Added audit logging to the Celery task caller in sla_tasks.py
  AFTER _mark_success() commits the transaction.
- Kept the exception-handler audit log inside compute_device_sla()
  since failures are independent of transaction state.
- Added tests verifying compute_device_sla() never emits audit events
  and that record_sla_settlement_audit_events still works correctly.

Closes ApexChainx#236
@Magrexy
Magrexy force-pushed the fix/issue-236-audit-events-after-commit branch from ffc384b to 0d3e0fe Compare August 18, 2026 15:57

@usmanimamu17-create usmanimamu17-create left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM. thanks for contributing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SLA service audit events logged before transaction commit

2 participants