@@ -79,16 +79,6 @@ func NewQueue(opts ...Option) (*Queue, error) {
79
79
return q , nil
80
80
}
81
81
82
- // Capacity for queue max size
83
- func (q * Queue ) Capacity () int {
84
- return q .worker .Capacity ()
85
- }
86
-
87
- // Usage for count of queue usage
88
- func (q * Queue ) Usage () int {
89
- return q .worker .Usage ()
90
- }
91
-
92
82
// Start to enable all worker
93
83
func (q * Queue ) Start () {
94
84
go q .start ()
@@ -123,24 +113,24 @@ func (q *Queue) BusyWorkers() int {
123
113
return int (q .metric .BusyWorkers ())
124
114
}
125
115
126
- // Wait all process
127
- func (q * Queue ) Wait () {
128
- q . routineGroup . Wait ( )
116
+ // BusyWorkers returns the numbers of success tasks.
117
+ func (q * Queue ) SuccessTasks () int {
118
+ return int ( q . metric . SuccessTasks () )
129
119
}
130
120
131
- func ( q * Queue ) handleQueue ( timeout time. Duration , job QueuedMessage ) error {
132
- if atomic . LoadInt32 ( & q . stopFlag ) == 1 {
133
- return ErrQueueShutdown
134
- }
121
+ // BusyWorkers returns the numbers of failure tasks.
122
+ func ( q * Queue ) FailureTasks () int {
123
+ return int ( q . metric . FailureTasks ())
124
+ }
135
125
136
- data := Job {
137
- Timeout : timeout ,
138
- Payload : job . Bytes (),
139
- }
126
+ // BusyWorkers returns the numbers of submitted tasks.
127
+ func ( q * Queue ) SubmittedTasks () int {
128
+ return int ( q . metric . SubmittedTasks ())
129
+ }
140
130
141
- return q . worker . Queue ( Job {
142
- Payload : data . Encode (),
143
- } )
131
+ // Wait all process
132
+ func ( q * Queue ) Wait () {
133
+ q . routineGroup . Wait ( )
144
134
}
145
135
146
136
// Queue to queue all job
@@ -153,19 +143,25 @@ func (q *Queue) QueueWithTimeout(timeout time.Duration, job QueuedMessage) error
153
143
return q .handleQueue (timeout , job )
154
144
}
155
145
156
- func (q * Queue ) handleQueueTask (timeout time.Duration , task TaskFunc ) error {
146
+ func (q * Queue ) handleQueue (timeout time.Duration , job QueuedMessage ) error {
157
147
if atomic .LoadInt32 (& q .stopFlag ) == 1 {
158
148
return ErrQueueShutdown
159
149
}
160
150
161
151
data := Job {
162
152
Timeout : timeout ,
153
+ Payload : job .Bytes (),
163
154
}
164
155
165
- return q .worker .Queue (Job {
166
- Task : task ,
156
+ if err := q .worker .Queue (Job {
167
157
Payload : data .Encode (),
168
- })
158
+ }); err != nil {
159
+ return err
160
+ }
161
+
162
+ q .metric .IncSubmittedTask ()
163
+
164
+ return nil
169
165
}
170
166
171
167
// QueueTask to queue job task
@@ -178,18 +174,48 @@ func (q *Queue) QueueTaskWithTimeout(timeout time.Duration, task TaskFunc) error
178
174
return q .handleQueueTask (timeout , task )
179
175
}
180
176
177
+ func (q * Queue ) handleQueueTask (timeout time.Duration , task TaskFunc ) error {
178
+ if atomic .LoadInt32 (& q .stopFlag ) == 1 {
179
+ return ErrQueueShutdown
180
+ }
181
+
182
+ data := Job {
183
+ Timeout : timeout ,
184
+ }
185
+
186
+ if err := q .worker .Queue (Job {
187
+ Task : task ,
188
+ Payload : data .Encode (),
189
+ }); err != nil {
190
+ return err
191
+ }
192
+
193
+ q .metric .IncSubmittedTask ()
194
+
195
+ return nil
196
+ }
197
+
181
198
func (q * Queue ) work (task QueuedMessage ) {
199
+ var err error
182
200
// to handle panic cases from inside the worker
183
201
// in such case, we start a new goroutine
184
202
defer func () {
185
203
q .metric .DecBusyWorker ()
186
- if err := recover (); err != nil {
204
+ e := recover ()
205
+ if e != nil {
187
206
q .logger .Errorf ("panic error: %v" , err )
188
207
}
189
208
q .schedule ()
209
+
210
+ // increase success or failure number
211
+ if err == nil && e == nil {
212
+ q .metric .IncSuccessTask ()
213
+ } else {
214
+ q .metric .IncFailureTask ()
215
+ }
190
216
}()
191
217
192
- if err : = q .worker .Run (task ); err != nil {
218
+ if err = q .worker .Run (task ); err != nil {
193
219
q .logger .Errorf ("runtime error: %s" , err .Error ())
194
220
}
195
221
}
0 commit comments