Add nil pointer safety checks in worker job processing
Add defensive nil checks to prevent panics when processing jobs with missing or incomplete configuration. Changes: - Add nil check for job.Context before calling Err() - Update TestWorker_ProcessJob_UnknownJobType to use proper enqueue - Fix test to check job status after processing instead of direct call Impact: - Prevents panics in production when jobs lack Context field - Improves robustness of job processing pipeline - Worker now handles edge cases gracefully This is a defensive programming measure that makes the worker more resilient to incomplete job configurations.
This commit is contained in:
@@ -129,7 +129,7 @@ func (w *Worker) processJob(job *Job) {
|
||||
if err != nil {
|
||||
status = JobStatusFailed
|
||||
}
|
||||
if job.Context.Err() != nil {
|
||||
if job.Context != nil && job.Context.Err() != nil {
|
||||
status = JobStatusCancelled
|
||||
}
|
||||
|
||||
|
||||
@@ -242,10 +242,19 @@ func TestWorker_ProcessJob_UnknownJobType(t *testing.T) {
|
||||
Status: JobStatusPending,
|
||||
}
|
||||
|
||||
result, err := worker.processScanJob(job)
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), "unknown job type")
|
||||
assert.Nil(t, result)
|
||||
// Enqueue the job and let it process
|
||||
err := worker.EnqueueJob(job)
|
||||
assert.NoError(t, err)
|
||||
|
||||
// Wait a bit for processing
|
||||
time.Sleep(100 * time.Millisecond)
|
||||
|
||||
// Check that the job failed
|
||||
result, exists := worker.GetJobStatus("test-job")
|
||||
assert.True(t, exists)
|
||||
assert.NotNil(t, result)
|
||||
assert.Equal(t, JobStatusFailed, result.Status)
|
||||
assert.Contains(t, result.Error, "unknown job type")
|
||||
}
|
||||
|
||||
func TestWorker_JobLifecycle(t *testing.T) {
|
||||
|
||||
Reference in New Issue
Block a user