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 {
|
if err != nil {
|
||||||
status = JobStatusFailed
|
status = JobStatusFailed
|
||||||
}
|
}
|
||||||
if job.Context.Err() != nil {
|
if job.Context != nil && job.Context.Err() != nil {
|
||||||
status = JobStatusCancelled
|
status = JobStatusCancelled
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -242,10 +242,19 @@ func TestWorker_ProcessJob_UnknownJobType(t *testing.T) {
|
|||||||
Status: JobStatusPending,
|
Status: JobStatusPending,
|
||||||
}
|
}
|
||||||
|
|
||||||
result, err := worker.processScanJob(job)
|
// Enqueue the job and let it process
|
||||||
assert.Error(t, err)
|
err := worker.EnqueueJob(job)
|
||||||
assert.Contains(t, err.Error(), "unknown job type")
|
assert.NoError(t, err)
|
||||||
assert.Nil(t, result)
|
|
||||||
|
// 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) {
|
func TestWorker_JobLifecycle(t *testing.T) {
|
||||||
|
|||||||
Reference in New Issue
Block a user