This repository has been archived by the owner on Aug 2, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscheduler.go
85 lines (72 loc) · 2.04 KB
/
scheduler.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
// Copyright (c) 2020 Davi Villalva.
// license can be found in the LICENSE file.
package dvtask
import (
"errors"
"time"
)
// Scheduler schedules tasks
type Scheduler struct {
startDate time.Time
deadline time.Time
workStart Hour
workEnd Hour
firstTask *Task
}
// NewScheduler creates a new Scheduler (may remove this in the future). To be tested.
func NewScheduler(startDate, deadline time.Time, workStart, workEnd Hour) *Scheduler {
return &Scheduler{
startDate: startDate,
deadline: deadline,
workStart: workStart,
workEnd: workEnd,
}
}
// ScheduledTaskAt returns the scheduled task at a specific time. To be tested.
func (s *Scheduler) ScheduledTaskAt(time time.Time) *Task {
for currentTask := s.firstTask; currentTask != nil; currentTask = currentTask.next {
if currentTask.ContainsTime(time) {
return currentTask
}
}
return nil
}
// ScheduledTasksInTimeInterval returns the first and last scheduled tasks within the given time interval.
// Returns nil, nil if no tasks are found within the time interval. To be tested.
func (s *Scheduler) ScheduledTasksInTimeInterval(start, end time.Time) (first, last *Task) {
for currentTask := s.firstTask; currentTask != nil; currentTask = currentTask.next {
if currentTask.IntersectsWithTimeInterval(start, end) {
if first != nil {
return
}
continue
}
if first == nil {
first = currentTask
}
last = currentTask
}
return
}
// Not ready yet. To be tested.
func (s *Scheduler) ScheduleTask(task *Task, fromTime time.Time) error {
if fromTime.Before(time.Now().Add(5 * time.Minute)) {
return errors.New("tasks must be scheduled at least five minutes from now")
}
taskDuration := task.Duration()
if s.firstTask == nil {
task.start = fromTime
task.end = fromTime.Add(taskDuration)
s.firstTask = task
return nil
}
for currentTask := s.firstTask; ; currentTask = currentTask.next {
if currentTask.priority < task.priority {
break
} else if currentTask.next == nil {
break
}
}
task.isScheduled = true
return errors.New("not implemented yet")
}