Get retrieves and removes the next non-cancelled task from the queue, adding it to the running tasks. Blocks until a task is available or the queue is closed.
()
| 53 | // Get retrieves and removes the next non-cancelled task from the queue, adding it to the running tasks. |
| 54 | // Blocks until a task is available or the queue is closed. |
| 55 | func (tq *TaskQueue[T]) Get() (*Task[T], error) { |
| 56 | tq.mu.Lock() |
| 57 | defer tq.mu.Unlock() |
| 58 | |
| 59 | for tq.tasks.Len() == 0 && !tq.closed { |
| 60 | tq.cond.Wait() |
| 61 | } |
| 62 | |
| 63 | if tq.closed && tq.tasks.Len() == 0 { |
| 64 | return nil, fmt.Errorf("queue is closed and empty") |
| 65 | } |
| 66 | |
| 67 | for tq.tasks.Len() > 0 { |
| 68 | element := tq.tasks.Front() |
| 69 | task := element.Value.(*Task[T]) |
| 70 | |
| 71 | tq.tasks.Remove(element) |
| 72 | task.element = nil |
| 73 | |
| 74 | if !task.Cancelled() { |
| 75 | tq.runningTaskMap[task.ID] = task |
| 76 | return task, nil |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | if !tq.closed { |
| 81 | return tq.Get() |
| 82 | } |
| 83 | |
| 84 | return nil, fmt.Errorf("queue is closed and empty") |
| 85 | } |
| 86 | |
| 87 | // Done stops(cancels) and removes the task from the running tasks. |
| 88 | func (tq *TaskQueue[T]) Done(taskID string) { |