Repository navigation
fix: create the sync event before queueing its job - #1114
Open
rohan-chaturvedi wants to merge 1 commit into
Open
rohan-chaturvedi wants to merge 1 commit into
rohan-chaturvedi wants to merge 1 commit into
Conversation
trigger_sync_tasks queued the job, then created its event. A worker could start the job first. It then found no event and failed at the end, leaving the sync "in progress", or updated an older event of the sync and left the new one "queued". Create the event first, and queue the job with the event's ID. The job updates the event with its own ID, and still records the sync's result if the event is missing.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
🔍 Overview
Sync jobs could start before their sync event existed.
trigger_sync_tasksqueued the job withsync_func.delay(), and only then created theEnvironmentSyncEventwith the job's ID. When a worker picked the job up first,handle_sync_eventlooked up the sync's latest event and:AttributeError: 'NoneType' object has no attribute 'completed_at', and the sync stayed "in progress";This affects every provider. It was seen while testing #1044, with a GitLab sync created through the UI.
💡 Proposed Changes
trigger_sync_taskscreates the event first, then queues the job with the event's ID (delay(env_sync, job_id=...), which RQ's@jobdecorator supports). If queueing fails, the event fails too ("Failed to queue the sync job"), as does the sync.handle_sync_eventupdates the event of the running job (rq.get_current_job()), so it never updates another run's event. Outside a worker, e.g. when called directly, it uses the sync's latest event, as before.AttributeError.handle_sync_eventworks out the status and result once, and applies them to the sync and its event. What is recorded doesn't change: the same statuses and messages for success, failure, exceptions, timeouts and missing credentials.🖼️ Screenshots or Demo
N/A
📝 Release Notes
❓ Open Questions
N/A
🧪 Testing
backend/tests/tasks/test_sync_events.py(11 tests, with an in-memory event store):delay()returns updates its own new event;main. The other 4 cover existing behaviour.main: for a sync with earlier runs, the job overwrote the previous event and the new one stayed "queued". For a new sync, the job failed withAttributeErrorand the sync stayed "in progress".test_file_read_permission_error, which fails whenever tests run as root.🎯 Reviewer Focus
backend/api/tasks/syncing.py:trigger_sync_tasks,_get_sync_eventandhandle_sync_event.➕ Additional Context
backend/api/tasks/syncing.py, and they merge cleanly.cancel_sync_tasksdoesn't stop a job that is already running, so overlapping runs of a sync are still possible.✨ How to Test the Changes Locally
cd backend && pytest tests/tasks/test_sync_events.pypython manage.py shell, patchEnvironmentSyncEvent.objects.createto sleep 3 s before creating the event, then calltrigger_sync_tasksfor a sync. Onmain, the new event stays "queued", or for a new sync the job fails. On this branch, it completes.💚 Did You...
Generated by Claude Code