-
Notifications
You must be signed in to change notification settings - Fork 1
/
tasks.go
139 lines (116 loc) · 2.62 KB
/
tasks.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
package tasks
import (
"fmt"
"math/rand"
"sync/atomic"
"time"
)
// Task a task to run
type Task struct {
Task string
}
// NewTask make a new task
func NewTask(task string) *Task {
t := Task{Task: task}
return &t
}
// TaskList a list of tasks to run
type TaskList struct {
Tasks []Task
Offset int
}
// NewTaskList make a new task list
func NewTaskList() TaskList {
tl := TaskList{}
tl.Offset = 0
return tl
}
// Add add tasks to a task list
func (tl *TaskList) Add(tasks ...string) {
for _, v := range tasks {
task := Task{}
task.Task = v
tl.Tasks = append(tl.Tasks, task)
}
}
// Shuffle shuffle the task lines for a task list
func (tl TaskList) Shuffle() {
rand.Seed(time.Now().UnixNano())
rand.Shuffle(len(tl.Tasks), func(i, j int) { tl.Tasks[i], tl.Tasks[j] = tl.Tasks[j], tl.Tasks[i] })
}
// TaskListSet a set of task lists
type TaskListSet struct {
TaskLists []*TaskList
Offset int64
}
// NewTaskListSet make a new task list set
func NewTaskListSet() TaskListSet {
tls := TaskListSet{}
tls.TaskLists = make([]*TaskList, 0)
// tls.Sequence = 1
return tls
}
// OffsetReset reset sequence
func (tls *TaskListSet) OffsetReset() {
for _, tl := range tls.TaskLists {
tl.Offset = 0
}
atomic.StoreInt64(&tls.Offset, 0)
}
// AddTaskList add a task list ot the taskSet
func (tls *TaskListSet) AddTaskList(taskList TaskList) {
tls.TaskLists = append(tls.TaskLists, &taskList)
}
// Max get maximum task list size
func (tls *TaskListSet) Max() (max int) {
for _, v := range tls.TaskLists {
if len(v.Tasks) > max {
max = len(v.Tasks)
}
}
return
}
// NextAll get next item slice for all tasks item lists
func (tls TaskListSet) NextAll() (tasks []Task, err error) {
for i := range tls.TaskLists {
var task Task
task, err = tls.Next(i)
if err != nil {
return
}
tasks = append(tasks, task)
}
return
}
// Next treat task list as a circle that loops back to zero
func (tls *TaskListSet) Next(list int) (task Task, err error) {
var taskList *TaskList
if list <= len(tls.TaskLists)-1 {
taskList = tls.TaskLists[list]
} else {
err = fmt.Errorf("list %d out of bounds for %d lists", list, len(tls.TaskLists)-1)
return
}
task = taskList.Tasks[taskList.Offset]
newOffset := taskList.Offset
if newOffset >= len(taskList.Tasks)-1 {
taskList.Offset = 0
} else {
taskList.Offset++
}
return
}
// Next treat task list as a circle that loops back to zero
func (tl *TaskList) Next() (task Task, atEnd bool, err error) {
task = tl.Tasks[tl.Offset]
newOffset := tl.Offset
if newOffset >= len(tl.Tasks)-1 {
tl.Offset = 0
} else {
tl.Offset++
}
if tl.Offset == len(tl.Tasks) {
atEnd = true
}
return
}