feat: Add Celery task queue for event-driven test processing #963
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.



Summary
This PR introduces an optional Celery task queue system as an alternative to the current cron-based test processing. This is part of the architectural improvements outlined in the Sample Platform Reliability Assessment.
Why This Change?
The current cron-based polling approach has several limitations:
The Celery task queue provides:
Changes Overview
New Files (5)
celery_app.pymod_ci/tasks.pyinstall/celery-worker.serviceinstall/celery-beat.servicetests/test_ci/test_tasks.pyModified Files (4)
requirements.txtcelery[redis]==5.3.6andredis==5.0.1config_sample.pymod_ci/controllers.pytrigger_test_tasks()function, modifiedadd_test_entry()to return test IDsinstall/installation.mdArchitecture
Task Definitions
start_test_task(test_id, bot_token)check_expired_instances_task()process_pending_tests_task()Queue Configuration
defaulttest_executionmaintenanceFeature Flag
The
USE_CELERY_TASKSconfiguration option controls whether Celery is active:False(default): Cron-based processing continues as beforeTrue: Webhooks trigger Celery tasks immediately after test creationThis allows for a gradual, zero-risk migration from cron to Celery.
How to Deploy
Prerequisites
Install Redis on the server:
Install Python dependencies:
Configuration
Add the following to your
config.py:Install Systemd Services
Recommended Rollout Strategy
Stage 1: Install & Monitor (Week 1-2)
/var/www/sample-platform/logs/celery/Stage 2: Celery Primary (Week 3-4)
Stage 3: Celery Only (Week 5+)
sudo crontab -eand comment out the line)How to Test
Unit Tests
Expected output: 6 tests pass
Manual Testing
Verify Redis is running:
redis-cli ping # Should return PONGVerify Celery worker is running:
Verify Celery beat is running:
Monitor queue depth:
View logs:
tail -f /var/www/sample-platform/logs/celery/*.logTest task execution manually (Python shell):
Optional: Flower Dashboard
For web-based monitoring:
Then access
http://localhost:5555for a real-time task dashboard.How to Rollback
If issues occur, rollback is simple:
Files Changed in Detail
celery_app.py(New)make_celery(app)mod_ci/tasks.py(New)start_test_task: Wraps existingstart_test()with Celery retry logiccheck_expired_instances_task: Periodic cleanup taskprocess_pending_tests_task: Finds pending tests and queues themmod_ci/controllers.py(Modified)add_test_entry(): Now returns list of created test IDs (wasNone)trigger_test_tasks(): New function to optionally queue Celery taskstrigger_test_tasks()after test creationdb.flush()calls to get test IDs before commitconfig_sample.py(Modified)Added configuration options:
install/celery-worker.service(New)www-datauserinstall/celery-beat.service(New)www-datauserinstall/installation.md(Modified)tests/test_ci/test_tasks.py(New)6 unit tests covering:
trigger_test_tasks()with Celery disabled (default)trigger_test_tasks()with empty test ID listtrigger_test_tasks()handling import errors gracefullyadd_test_entry()returning test IDsadd_test_entry()with invalid commit hashadd_test_entry()with database commit failureBackward Compatibility
USE_CELERY_TASKS = False(default) means cron continues as beforeRelated Issues
Test Plan
USE_CELERY_TASKS = FalseUSE_CELERY_TASKS = Trueon staging🤖 Generated with Claude Code