-
-
Notifications
You must be signed in to change notification settings - Fork 637
fix: set job to FAILED on exception in job_pipeline #3661
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
base: develop
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,7 +19,7 @@ | |
| ) | ||
| from api_app.choices import Classification | ||
| from api_app.models import Job | ||
| from intel_owl.tasks import check_stuck_analysis, remove_old_jobs | ||
| from intel_owl.tasks import check_stuck_analysis, job_pipeline, remove_old_jobs | ||
|
|
||
| from . import CustomTestCase, get_logger | ||
| from .mock_utils import MockUpResponse, if_mock_connections, patch, skip | ||
|
|
@@ -58,6 +58,44 @@ def test_check_stuck_analysis(self): | |
| _job.delete() | ||
| an.delete() | ||
|
|
||
| def test_job_pipeline_exception_sets_job_to_failed(self): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Suggestion: Add a second test case for the Investigation status update path The |
||
| """ | ||
| Regression test for GitHub issue #3653. | ||
| When job.execute() raises an exception inside job_pipeline, | ||
| the Job object must be marked as FAILED (not left stuck in RUNNING), | ||
| finished_analysis_time must be set, and the error must be recorded. | ||
| """ | ||
| an = Analyzable.objects.create( | ||
| name="8.8.8.8", | ||
| classification=Classification.IP, | ||
| ) | ||
| _job = Job.objects.create( | ||
| user=self.user, | ||
| status=Job.STATUSES.PENDING.value, | ||
| analyzable=an, | ||
| ) | ||
| error_message = "Simulated broker failure" | ||
| with ( | ||
| patch.object( | ||
| _job.__class__, | ||
| "execute", | ||
| side_effect=Exception(error_message), | ||
| ), | ||
| patch("api_app.websocket.JobConsumer.serialize_and_send_job"), | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Missing assertion: WebSocket notification was actually sent You're patching with (
patch.object(
_job.__class__,
"execute",
side_effect=Exception(error_message),
),
patch("api_app.websocket.JobConsumer.serialize_and_send_job") as mock_ws,
):
job_pipeline(_job.pk)
mock_ws.assert_called_once() |
||
| patch( | ||
| "api_app.models.Job.objects.get", | ||
| return_value=_job, | ||
| ), | ||
| ): | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Unnecessary mock — this can be removed The |
||
| job_pipeline(_job.pk) | ||
|
|
||
| _job.refresh_from_db() | ||
| self.assertEqual(_job.status, Job.STATUSES.FAILED.value) | ||
| self.assertIsNotNone(_job.finished_analysis_time) | ||
| self.assertIn(error_message, _job.errors) | ||
| _job.delete() | ||
| an.delete() | ||
|
|
||
| def test_remove_old_jobs(self): | ||
| import datetime | ||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Suggestion: Wrap cleanup in its own try/except
If any of these cleanup lines raise (e.g.,
job.get_root()hits a treebeardMultipleObjectsReturnededge case, or the WebSocket layer has a config issue), the original exceptionegets swallowed and replaced with the cleanup exception. The original error would be lost.Consider:
This ensures the original error is always logged regardless of cleanup failures.