-
Notifications
You must be signed in to change notification settings - Fork 133
feat(celery): finalize in-flight task on worker eviction so the chord proceeds #1203
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
+138
−2
Closed
Changes from 1 commit
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
4fe6fb1
feat(celery): finalize in-flight task on worker eviction so the chord…
ocervell cb0f0f4
fix(celery): clear stale eviction flag per task; harden handler + iso…
ocervell 10cce2e
chore: re-trigger CI
ocervell e32d1c2
test(eviction): re-raise shutdown flag until the task stops (fix CI r…
ocervell c33c83b
test(eviction): make monitor tests deterministic (no live subprocess)
ocervell 25e2dc8
chore: re-trigger CI
ocervell 2a3d816
test(eviction): assert clear-at-start via mock (robust to CI env)
ocervell File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| import threading | ||
| import time | ||
| import unittest | ||
| import unittest.mock | ||
|
|
||
| from secator.celery_signals import ( | ||
| clear_shutdown_flag, | ||
| is_worker_shutting_down, | ||
| worker_shutting_down_handler, | ||
| ) | ||
| from secator.runners import Command | ||
|
|
||
|
|
||
| class TestEvictionSelfFinalize(unittest.TestCase): | ||
| """Worker-eviction self-finalize: on shutdown (e.g. a K8s pod SIGTERM eviction) the in-flight | ||
| task's monitor stops it early and returns partial results, so the surrounding chord proceeds | ||
| instead of hanging until the broker visibility timeout redelivers the task.""" | ||
|
|
||
| def setUp(self): | ||
| clear_shutdown_flag() | ||
|
|
||
| def tearDown(self): | ||
| clear_shutdown_flag() | ||
|
ocervell marked this conversation as resolved.
|
||
|
|
||
| def test_shutdown_flag_lifecycle(self): | ||
| """worker_shutting_down_handler raises the flag; clear_shutdown_flag drops it.""" | ||
| self.assertFalse(is_worker_shutting_down()) | ||
| worker_shutting_down_handler() | ||
| self.assertTrue(is_worker_shutting_down()) | ||
| clear_shutdown_flag() | ||
| self.assertFalse(is_worker_shutting_down()) | ||
|
|
||
| def test_monitor_stops_running_command_on_shutdown(self): | ||
| """A long-running command stops early once the flag is raised (instead of running to | ||
| completion), and emits the eviction Warning — proving the monitor self-stop path.""" | ||
| holder = {} | ||
|
|
||
| def run(): | ||
| holder['cmd'] = Command.execute('sleep 30', name='evict_sleep', process=True, quiet=True) | ||
|
|
||
| t = threading.Thread(target=run, daemon=True) | ||
| # Poll fast so the test doesn't wait the full stat-update cadence. | ||
| with unittest.mock.patch('secator.runners.command.MONITOR_POLL_SECONDS', 1): | ||
| start = time.monotonic() | ||
| t.start() | ||
| time.sleep(2) # let the subprocess + monitor thread start | ||
| worker_shutting_down_handler() # simulate the eviction SIGTERM | ||
| t.join(timeout=20) | ||
| elapsed = time.monotonic() - start | ||
|
|
||
| self.assertFalse(t.is_alive(), 'command did not stop after the shutdown flag was raised') | ||
| self.assertLess(elapsed, 25, 'command did not stop early (ran toward the full 30s sleep)') | ||
|
|
||
| cmd = holder['cmd'] | ||
| signals = [ | ||
| item for item in (cmd.warnings + cmd.results) | ||
| if 'shutting down' in str(getattr(item, 'message', '')).lower() | ||
| ] | ||
| self.assertTrue(signals, 'no eviction warning emitted on shutdown') | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.