-
Notifications
You must be signed in to change notification settings - Fork 105
Closed
Description
Description
Implement a Kafka consumer worker responsible for safely updating Photo documents with their assigned eventId. This worker consumes internal Kafka messages emitted by the Event Clustering Worker (Issue 3) and applies those decisions to the database.
This worker performs only database mutations and must not contain any clustering or decision-making logic.
Scope
- Consume internal Kafka messages related to event updates
- Assign
eventIdto photos in the database - Ensure updates are idempotent and retry-safe
- Isolate photo update logic from clustering logic
Kafka Topic
event-updates— internal topic carrying photo-to-event assignment data
Example message:
{
"eventId": "evt123",
"photoIds": ["photo1", "photo2"]
}Folder & Files
workers/
photoUpdateWorker.js
services/
photoUpdateService.js
File Responsibilities
workers/photoUpdateWorker.js
- Initialize Kafka consumer
- Subscribe to
event-updatestopic - Parse incoming messages
- Delegate database updates to the service layer
- Commit Kafka offsets only after successful updates
Main function:
startPhotoUpdateWorker()
services/photoUpdateService.js
Contains only database update logic and must be idempotent.
Key functions to implement:
assignEventToPhotos(photoIds, eventId)isPhotoAlreadyAssigned(photoId)
Uses:
models/Photo.js
Function Responsibilities & Call Flow
startPhotoUpdateWorker()
Entry point for this worker.
Flow:
- Consumes a message from the
event-updatestopic - Extracts
eventIdandphotoIds - Calls
assignEventToPhotos(photoIds, eventId) - Commits Kafka offset on success
assignEventToPhotos(photoIds, eventId)
Assigns the given eventId to each photo listed in photoIds.
What this function does:
- Iterates over each
photoId - For each photo:
- Calls
isPhotoAlreadyAssigned(photoId) - If not assigned, updates
photo.eventId = eventId
- Calls
- Ensures safe execution if the same message is processed multiple times
Calls:
isPhotoAlreadyAssigned(photoId)
isPhotoAlreadyAssigned(photoId)
Checks whether a photo already has an eventId assigned.
What this function does:
- Reads the Photo document from the database
- Returns
trueifeventIdalready exists - Returns
falseotherwise
Purpose:
- Prevents duplicate writes
- Ensures idempotency under retries
Acceptance Criteria
- Photos receive correct
eventId - Processing the same message multiple times is safe
- No duplicate or conflicting updates occur
- Worker can be restarted without data corruption
- Kafka offsets are committed only on success
Notes
- No clustering or event creation logic should exist here
- All updates must be idempotent
- Database writes must be safe under retries
- This issue depends on Event Clustering Worker Issue 49 - Event Clustering Worker with Geo-Temporal Logic (backend) #328