-
Notifications
You must be signed in to change notification settings - Fork 8
/
task.go
executable file
·83 lines (68 loc) · 1.25 KB
/
task.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
package main
import (
"labix.org/v2/mgo"
"time"
"fmt"
)
type Task struct {
Id int "_id"
Start int
Finish int
Done bool
Timestamp time.Time
}
// Marks a task as done.
func (t *Task) finish() {
t.Done = true
}
// TODO: Add a check that rate is valid
func (j *Job) createTask(session *mgo.Session, rate int) *Task {
var t Task
SECONDS := 240
total := 0
task_max := 0
if len(j.Tasks) > 0 {
for _, task := range j.Tasks {
if time.Since(task.Timestamp).Minutes() > 15 && task.Done == false {
// This is an old task, we need to resend it for reprocessing.
fmt.Println("Found an old task.")
return &task
}
if task.Id >= task_max {
task_max = task.Id + 1
}
}
} else {
task_max = 1
}
if len(j.Tasks) < task_max {
jump := j.Start + (SECONDS * rate)
if jump > j.Finish {
total = j.Finish
} else {
total = jump
}
if j.Start < j.Finish {
t = Task{
Id: task_max,
Start: j.Start,
Finish: total,
Done: false,
Timestamp: time.Now(),
}
// fmt.Println(t)
j.Tasks = append(j.Tasks, t)
j.Start = total
} else {
t = Task{}
}
j.update(session)
} else {
for _, task := range j.Tasks {
if task.Id == task_max {
return &task
}
}
}
return &t
}