@@ -8,14 +8,14 @@ import (
8
8
)
9
9
10
10
type queue struct {
11
- active int32
12
- st * jobs.Stat
11
+ on int32
12
+ state * jobs.Stat
13
13
14
14
// job pipeline
15
15
concurPool chan interface {}
16
16
jobs chan * entry
17
17
18
- // active operations
18
+ // on operations
19
19
muw sync.Mutex
20
20
wg sync.WaitGroup
21
21
@@ -34,56 +34,50 @@ type entry struct {
34
34
}
35
35
36
36
// create new queue
37
- func newQueue (concurrency int ) * queue {
38
- q := & queue {st : & jobs.Stat {}, jobs : make (chan * entry )}
37
+ func newQueue (maxConcur int ) * queue {
38
+ q := & queue {state : & jobs.Stat {}, jobs : make (chan * entry )}
39
39
40
- if concurrency != 0 {
41
- q .concurPool = make (chan interface {}, concurrency )
42
- for i := 0 ; i < concurrency ; i ++ {
40
+ if maxConcur != 0 {
41
+ q .concurPool = make (chan interface {}, maxConcur )
42
+ for i := 0 ; i < maxConcur ; i ++ {
43
43
q .concurPool <- nil
44
44
}
45
45
}
46
46
47
47
return q
48
48
}
49
49
50
- // associate queue with new do pool
51
- func (q * queue ) configure (execPool chan jobs.Handler , err jobs.ErrorHandler ) error {
52
- q .execPool = execPool
53
- q .errHandler = err
54
-
55
- return nil
56
- }
57
-
58
50
// serve consumers
59
51
func (q * queue ) serve () {
60
52
q .wait = make (chan interface {})
61
- atomic .StoreInt32 (& q .active , 1 )
53
+ atomic .StoreInt32 (& q .on , 1 )
62
54
63
55
for {
64
56
e := q .consume ()
65
57
if e == nil {
58
+ q .wg .Wait ()
66
59
return
67
60
}
68
61
69
62
if q .concurPool != nil {
70
63
<- q .concurPool
71
64
}
72
65
73
- atomic .AddInt64 (& q .st .Active , 1 )
66
+ atomic .AddInt64 (& q .state .Active , 1 )
74
67
h := <- q .execPool
75
- go func (e * entry ) {
68
+
69
+ go func (h jobs.Handler , e * entry ) {
70
+ defer q .wg .Done ()
71
+
76
72
q .do (h , e )
77
- atomic .AddInt64 (& q .st .Active , ^ int64 (0 ))
73
+ atomic .AddInt64 (& q .state .Active , ^ int64 (0 ))
78
74
79
75
q .execPool <- h
80
76
81
77
if q .concurPool != nil {
82
78
q .concurPool <- nil
83
79
}
84
-
85
- q .wg .Done ()
86
- }(e )
80
+ }(h , e )
87
81
}
88
82
}
89
83
@@ -107,14 +101,14 @@ func (q *queue) do(h jobs.Handler, e *entry) {
107
101
err := h (e .id , e .job )
108
102
109
103
if err == nil {
110
- atomic .AddInt64 (& q .st .Queue , ^ int64 (0 ))
104
+ atomic .AddInt64 (& q .state .Queue , ^ int64 (0 ))
111
105
return
112
106
}
113
107
114
108
q .errHandler (e .id , e .job , err )
115
109
116
110
if ! e .job .Options .CanRetry (e .attempt ) {
117
- atomic .AddInt64 (& q .st .Queue , ^ int64 (0 ))
111
+ atomic .AddInt64 (& q .state .Queue , ^ int64 (0 ))
118
112
return
119
113
}
120
114
@@ -123,35 +117,35 @@ func (q *queue) do(h jobs.Handler, e *entry) {
123
117
124
118
// stop the queue consuming
125
119
func (q * queue ) stop () {
126
- if atomic .LoadInt32 (& q .active ) == 0 {
120
+ if atomic .LoadInt32 (& q .on ) == 0 {
127
121
return
128
122
}
129
123
130
- atomic .StoreInt32 (& q .active , 0 )
131
-
132
124
close (q .wait )
133
-
125
+
134
126
q .muw .Lock ()
135
127
q .wg .Wait ()
136
- q .muw .Unlock ()
128
+ q .muw .Unlock ()
129
+
130
+ atomic .StoreInt32 (& q .on , 0 )
137
131
}
138
132
139
133
// add job to the queue
140
134
func (q * queue ) push (id string , j * jobs.Job , attempt int , delay time.Duration ) {
141
135
if delay == 0 {
142
- atomic .AddInt64 (& q .st .Queue , 1 )
136
+ atomic .AddInt64 (& q .state .Queue , 1 )
143
137
go func () {
144
138
q .jobs <- & entry {id : id , job : j , attempt : attempt }
145
139
}()
146
140
147
141
return
148
142
}
149
143
150
- atomic .AddInt64 (& q .st .Delayed , 1 )
144
+ atomic .AddInt64 (& q .state .Delayed , 1 )
151
145
go func () {
152
146
time .Sleep (delay )
153
- atomic .AddInt64 (& q .st .Delayed , ^ int64 (0 ))
154
- atomic .AddInt64 (& q .st .Queue , 1 )
147
+ atomic .AddInt64 (& q .state .Delayed , ^ int64 (0 ))
148
+ atomic .AddInt64 (& q .state .Queue , 1 )
155
149
156
150
q .jobs <- & entry {id : id , job : j , attempt : attempt }
157
151
}()
@@ -160,8 +154,8 @@ func (q *queue) push(id string, j *jobs.Job, attempt int, delay time.Duration) {
160
154
func (q * queue ) stat () * jobs.Stat {
161
155
return & jobs.Stat {
162
156
InternalName : ":memory:" ,
163
- Queue : atomic .LoadInt64 (& q .st .Queue ),
164
- Active : atomic .LoadInt64 (& q .st .Active ),
165
- Delayed : atomic .LoadInt64 (& q .st .Delayed ),
157
+ Queue : atomic .LoadInt64 (& q .state .Queue ),
158
+ Active : atomic .LoadInt64 (& q .state .Active ),
159
+ Delayed : atomic .LoadInt64 (& q .state .Delayed ),
166
160
}
167
161
}
0 commit comments