MCPcopy Create free account
hub / github.com/aptly-dev/aptly / RunTaskInBackground

Method RunTaskInBackground

task/list.go:201–231  ·  view source on GitHub ↗

RunTaskInBackground creates task and runs it in background. This will block until the necessary resources become available.

(name string, resources []string, process Process)

Source from the content-addressed store, hash-verified

199// RunTaskInBackground creates task and runs it in background. This will block until the necessary resources
200// become available.
201func (list *List) RunTaskInBackground(name string, resources []string, process Process) (Task, *ResourceConflictError) {
202 list.Lock()
203
204 list.idCounter++
205 wgTask := &sync.WaitGroup{}
206 task := NewTask(process, name, list.idCounter, resources, wgTask)
207
208 list.tasks = append(list.tasks, task)
209 list.wgTasks[task.ID] = wgTask
210
211 list.wg.Add(1)
212 task.wgTask.Add(1)
213
214 // Copy task while still holding the lock to avoid racing with consumer
215 // setting State=RUNNING after receiving from queue
216 taskCopy := *task
217
218 // add task to queue for processing if resources are available
219 // if not, task will be queued by the consumer once resources are available
220 tasks := list.usedResources.UsedBy(resources)
221 if len(tasks) == 0 {
222 list.usedResources.MarkInUse(task.Resources, task)
223 // queueing task might block if channel not ready, unlock list before queueing
224 list.Unlock()
225 list.queue <- task
226 } else {
227 list.Unlock()
228 }
229
230 return taskCopy, nil
231}
232
233// Clear removes finished tasks from list
234func (list *List) Clear() {

Callers 2

TestListMethod · 0.95
runTaskInBackgroundFunction · 0.80

Calls 4

NewTaskFunction · 0.85
UsedByMethod · 0.80
MarkInUseMethod · 0.80
AddMethod · 0.45

Tested by 1

TestListMethod · 0.76