-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
146 lines (120 loc) · 2.92 KB
/
queue.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
140
141
142
143
144
145
146
package taskqueue
import (
"fmt"
"log"
"os"
"os/signal"
"sync"
"syscall"
"time"
)
// Queue - A queue for enqueueing tasks to be processed
type Queue struct {
// Debug Settings
tLog *log.Logger // logging
tDebug bool // enable debugging
// Channals
tQueueChan chan Task // Task Channal
tReadyChan chan chan Task // Ready Task Channals
// Goroutine synchronization
tDispatcherSync sync.WaitGroup // Work Dispatcher synchronization
tWorkersSync sync.WaitGroup // Workers synchronization
// Queue Workers
tWorkers []*Worker
// Quit Queue
tQuit chan bool
}
// NewQueue - Creates a new Queue
func NewQueue(nW int) *Queue {
// workers
w := make([]*Worker, nW, nW)
// workers synchronization
ws := sync.WaitGroup{}
// Ready Task Channals
rc := make(chan chan Task, nW)
// create n Workers
for i := 0; i < nW; i++ {
w[i] = NewWorker(rc, ws)
}
// return Queue
return &Queue{
// Channals
tQueueChan: make(chan Task),
tReadyChan: rc,
// Queue Workers
tWorkers: w,
// Goroutine synchronization
tDispatcherSync: sync.WaitGroup{},
tWorkersSync: ws,
// Quit Queue
tQuit: make(chan bool),
}
}
// dispatch - Dispatch workers to process tasks
func (q *Queue) dispatch() {
q.tDispatcherSync.Add(1)
for {
select {
case Task := <-q.tQueueChan: // We got something in on our queue
workerChannel := <-q.tReadyChan // Check out an available worker
workerChannel <- Task // Send the request to the channel
case <-q.tQuit:
for i := 0; i < len(q.tWorkers); i++ {
q.tWorkers[i].Stop()
}
q.tWorkersSync.Wait()
q.tDispatcherSync.Done()
return
}
}
}
// Start - Starts the worker and dispatcher go routines
func (q *Queue) Start() {
for i := 0; i < len(q.tWorkers); i++ {
q.tWorkers[i].Start() // start workers
}
go q.dispatch() // queue dispach
}
// Stop - Stopes Queue
func (q *Queue) Stop() {
q.tQuit <- true // pass quit flag
q.tDispatcherSync.Wait() // wait
}
// Enqueue - Fire-and-forget task are executed only once.
func (q *Queue) Enqueue(Task Task) {
q.tQueueChan <- Task
}
// Schedule - Delayed task are executed only once too, but not immediately, after a certain time interval.
func (q *Queue) Schedule(Task Task, duration_string string) {
dur, _ := time.ParseDuration(duration_string)
go func() {
t := time.NewTicker(dur)
defer t.Stop()
<-t.C
q.tQueueChan <- Task
}()
}
// Recurring - Recurring task are executed every x duration
func (q *Queue) Recurring(Task Task, duration_string string) {
dur, _ := time.ParseDuration(duration_string)
go func() {
// Signals to stop timer
sigs := make(chan os.Signal, 1)
sigdone := make(chan bool, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM)
t := time.NewTicker(dur)
go func() {
sig := <-sigs
fmt.Println(sig)
t.Stop() // stop timer
sigdone <- true
}()
// main loop
for {
go func() {
q.tQueueChan <- Task // run task
}()
<-t.C
}
}()
}