Skip to content

Commit a7a625a

Browse files
committed
Refactor ScanJob creation logic to trigger Celery tasks upon job submission. Update job status to QUEUED and handle potential errors during task submission, ensuring robust error logging and maintaining job state.
1 parent d3dc869 commit a7a625a

1 file changed

Lines changed: 18 additions & 5 deletions

File tree

backend/core/views.py

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,13 +428,26 @@ def perform_create(self, serializer):
428428
# We pass additional kwargs which DRF merges into validated_data for the create method.
429429
scan_job = serializer.save(
430430
initiator=user,
431-
status=ScanJobStatus.PENDING,
432-
target_info=job_target_info, # Pass data from config to be saved on ScanJob model
433-
tool_settings=job_tool_settings # Pass data from config to be saved on ScanJob model
431+
status=ScanJobStatus.PENDING, # Initial status
432+
target_info=job_target_info,
433+
tool_settings=job_tool_settings
434434
)
435435

436-
# TODO: Celery task triggering would go here, using scan_job.id and its details
437-
# For example: execute_scan_job.delay(scan_job.id)
436+
# Trigger the Celery task
437+
try:
438+
task = execute_scan_job.delay(scan_job.id)
439+
scan_job.celery_task_id = task.id
440+
scan_job.status = ScanJobStatus.QUEUED # Update status to QUEUED
441+
scan_job.save(update_fields=['celery_task_id', 'status'])
442+
print(f"Scan job {scan_job.id} submitted to Celery with task ID {task.id}")
443+
except Exception as e:
444+
# Handle potential errors during Celery task submission
445+
print(f"Error submitting scan job {scan_job.id} to Celery: {e}")
446+
# Optionally, set scan_job status to FAILED here if Celery submission fails critically
447+
# scan_job.status = ScanJobStatus.FAILED
448+
# scan_job.save(update_fields=['status'])
449+
# Depending on requirements, you might want to re-raise the exception or handle it gracefully
450+
pass # For now, just log and continue, the job remains PENDING if Celery fails
438451

439452
class UserViewSet(viewsets.ReadOnlyModelViewSet):
440453
"""

0 commit comments

Comments
 (0)