Skip to content

[Engine] Implement context-aware online re-scheduling for constraint changes (activity and user scope)#173

Open
noamarg wants to merge 1 commit intomainfrom
feature/noamarg/172/online-rescheduling
Open

[Engine] Implement context-aware online re-scheduling for constraint changes (activity and user scope)#173
noamarg wants to merge 1 commit intomainfrom
feature/noamarg/172/online-rescheduling

Conversation

@noamarg
Copy link
Copy Markdown
Collaborator

@noamarg noamarg commented Apr 5, 2026

Description

This PR implements a context-aware online re-scheduling pipeline that reacts in real time to constraint changes. The system now understands constraint scope (activity vs. user), operation type (create/update/delete), and performs safe, minimal-disruption reassignment of affected activities. Key improvements include event-driven architecture, scope-aware matching logic, non-destructive reassignment with fallback staging, and comprehensive test coverage.

Related Issues

Fixes #172

Changes Made

Domain Messages (new)

  • Added ConstraintScope enum (Activity, User) to distinguish constraint change contexts
  • Added ConstraintChangeOperation enum (Created, Updated, Deleted) to track mutation type
  • Extended HandleConstraintChangeRequest record with scope, operation, activity ID, and user ID parameters for full event context

Online Matching Strategy (refactored)

  • Replaced repository injection with AppDbContext for unified query execution and query filter control (IgnoreQueryFilters)
  • Implemented scope-based routing: routes activity-scope and user-scope changes to dedicated handlers
  • Added HandleActivityConstraintChangeAsync(): revalidates single activity assignment and reschedules if invalid
  • Added HandleUserConstraintChangeAsync(): discovers all affected assigned activities in period and reschedules invalid ones
  • Implemented staged fallback strategy via TryRescheduleAssignmentAsync():
    • Stage A: try alternative resource in same slot
    • Stage B: fallback to different slot/resource only if Stage A fails
  • Extracted helper methods for cleaner logic:
    • BuildCandidatesAsync(): filters available slot-resource pairs
    • SelectCandidateAsync(): applies preference-weighted ranking
    • GetExcludedSlotsAsync(): resolves constraint violations
    • IsAssignmentValidAsync(): checks assignment viability
    • ResolveSchedulingPeriodIdAsync(): handles optional period ID parameter
  • Non-destructive re-match: preserves current assignment until valid replacement confirmed
  • Enhanced logging with scope and operation context

Constraint Services

  • ActivityConstraintService: publish HandleConstraintChangeRequest on create/update/delete via IMessagePublisher
  • UserConstraintService: publish HandleConstraintChangeRequest on create/update/delete via IMessagePublisher
  • Added ResolveSchedulingPeriodIdAsync() helper to ActivityConstraintService for activity → scheduling period lookup
  • Made constraint validation static where applicable

Test Coverage

  • Updated ActivityConstraintServiceTests:
    • Mocked new dependencies: IActivityRepository, ISubjectRepository, IMessagePublisher
    • Updated constructor calls
    • Added assertion to verify message publishing on constraint creation
  • Updated UserConstraintServiceTests:
    • Mocked IMessagePublisher dependency
    • Updated constructor calls
    • Added assertion to verify message publishing on constraint creation

Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing completed
  • All existing tests pass

Test Instructions

  1. Run unit tests: dotnet test tests/Chronos.Tests.MainApi and dotnet test tests/Chronos.Tests.Engine
  2. Verify ActivityConstraintService publishes event on create/update/delete via test assertions
  3. Verify UserConstraintService publishes event on create/update/delete via test assertions
  4. Integration test: Create activity constraint → verify online scheduling consumer receives scope/operation context
  5. Integration test: Create user constraint → verify affected activities are validated and rescheduled if invalid
  6. Integration test: Verify fallback strategy (same-slot alternatives before new-slot fallback)
  7. Integration test: Verify non-destructive behavior (assignment preserved when no valid alternative exists)

Checklist

  • My code follows the project's code style guidelines
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings or errors
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

Database Changes

  • No database changes

Configuration Changes

  • No configuration changes required

Deployment Notes

Ensure RabbitMQ connectivity is available on deployed environments for message publication. Verify that IMessagePublisher is properly registered in dependency injection before deployment.

Additional Context

This feature completes the online scheduling pipeline by ensuring constraint changes are consistently propagated and intelligently acted upon. The staged fallback strategy minimizes unnecessary schedule disruption, while non-destructive re-matching and explicit unresolved reporting provide operational safety and transparency.

@noamarg noamarg self-assigned this Apr 5, 2026
@noamarg noamarg added the enhancement New feature or request label Apr 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Engine] Context-aware online re-scheduling pipeline for constraint changes (activity and user scope)

1 participant