consumer is processing the queue
()
| 41 | |
| 42 | // consumer is processing the queue |
| 43 | func (list *List) consumer() { |
| 44 | for { |
| 45 | select { |
| 46 | case task := <-list.queue: |
| 47 | // Set task state to RUNNING before processing |
| 48 | list.Lock() |
| 49 | task.State = RUNNING |
| 50 | list.Unlock() |
| 51 | |
| 52 | go func() { |
| 53 | retValue, err := task.process(aptly.Progress(task.output), task.detail) |
| 54 | |
| 55 | // Update task completion state and cleanup with list lock held |
| 56 | list.Lock() |
| 57 | { |
| 58 | if err != nil { |
| 59 | task.output.Printf("Task failed with error: %v", err) |
| 60 | task.State = FAILED |
| 61 | task.err = err |
| 62 | task.processReturnValue = retValue |
| 63 | } else { |
| 64 | task.output.Print("Task succeeded") |
| 65 | task.State = SUCCEEDED |
| 66 | task.err = nil |
| 67 | task.processReturnValue = retValue |
| 68 | } |
| 69 | |
| 70 | list.usedResources.Free(task.Resources) |
| 71 | |
| 72 | task.wgTask.Done() |
| 73 | list.wg.Done() |
| 74 | |
| 75 | unlocked := false |
| 76 | for _, t := range list.tasks { |
| 77 | if t.State == IDLE { |
| 78 | // check resources |
| 79 | blockingTasks := list.usedResources.UsedBy(t.Resources) |
| 80 | if len(blockingTasks) == 0 { |
| 81 | list.usedResources.MarkInUse(t.Resources, t) |
| 82 | // unlock list since queueing may block |
| 83 | list.Unlock() |
| 84 | unlocked = true |
| 85 | list.queue <- t |
| 86 | break |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | if !unlocked { |
| 91 | list.Unlock() |
| 92 | } |
| 93 | } |
| 94 | }() |
| 95 | |
| 96 | case <-list.queueDone: |
| 97 | return |
| 98 | } |
| 99 | } |
| 100 | } |