InitTasks initializes the global Tasks variable.
()
| 86 | |
| 87 | // InitTasks initializes the global Tasks variable. |
| 88 | func InitTasks() { |
| 89 | path := filepath.Join(x.WorkerConfig.TmpDir, "tasks.buf") |
| 90 | log, err := z.NewTreePersistent(path) |
| 91 | x.Check(err) |
| 92 | |
| 93 | // #nosec G404: weak RNG |
| 94 | Tasks = &tasks{ |
| 95 | queue: make(chan taskRequest, 16), |
| 96 | log: log, |
| 97 | logMu: new(sync.Mutex), |
| 98 | rng: rand.New(rand.NewSource(time.Now().UnixNano())), |
| 99 | } |
| 100 | |
| 101 | // Mark all pending tasks as failed. |
| 102 | Tasks.logMu.Lock() |
| 103 | Tasks.log.IterateKV(func(id, val uint64) uint64 { |
| 104 | meta := TaskMeta(val) |
| 105 | if status := meta.Status(); status == TaskStatusQueued || status == TaskStatusRunning { |
| 106 | return uint64(newTaskMeta(meta.Kind(), TaskStatusFailed)) |
| 107 | } |
| 108 | return 0 |
| 109 | }) |
| 110 | Tasks.logMu.Unlock() |
| 111 | |
| 112 | // Start the task runner. |
| 113 | go Tasks.worker() |
| 114 | } |
| 115 | |
| 116 | // tasks is a persistent task queue. |
| 117 | type tasks struct { |