@@ -79,16 +79,6 @@ func NewQueue(opts ...Option) (*Queue, error) {
7979 return q , nil
8080}
8181
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-
9282// Start to enable all worker
9383func (q * Queue ) Start () {
9484 go q .start ()
@@ -123,24 +113,24 @@ func (q *Queue) BusyWorkers() int {
123113 return int (q .metric .BusyWorkers ())
124114}
125115
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 () )
129119}
130120
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+ }
135125
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+ }
140130
141- return q . worker . Queue ( Job {
142- Payload : data . Encode (),
143- } )
131+ // Wait all process
132+ func ( q * Queue ) Wait () {
133+ q . routineGroup . Wait ( )
144134}
145135
146136// Queue to queue all job
@@ -153,19 +143,25 @@ func (q *Queue) QueueWithTimeout(timeout time.Duration, job QueuedMessage) error
153143 return q .handleQueue (timeout , job )
154144}
155145
156- func (q * Queue ) handleQueueTask (timeout time.Duration , task TaskFunc ) error {
146+ func (q * Queue ) handleQueue (timeout time.Duration , job QueuedMessage ) error {
157147 if atomic .LoadInt32 (& q .stopFlag ) == 1 {
158148 return ErrQueueShutdown
159149 }
160150
161151 data := Job {
162152 Timeout : timeout ,
153+ Payload : job .Bytes (),
163154 }
164155
165- return q .worker .Queue (Job {
166- Task : task ,
156+ if err := q .worker .Queue (Job {
167157 Payload : data .Encode (),
168- })
158+ }); err != nil {
159+ return err
160+ }
161+
162+ q .metric .IncSubmittedTask ()
163+
164+ return nil
169165}
170166
171167// QueueTask to queue job task
@@ -178,18 +174,48 @@ func (q *Queue) QueueTaskWithTimeout(timeout time.Duration, task TaskFunc) error
178174 return q .handleQueueTask (timeout , task )
179175}
180176
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+
181198func (q * Queue ) work (task QueuedMessage ) {
199+ var err error
182200 // to handle panic cases from inside the worker
183201 // in such case, we start a new goroutine
184202 defer func () {
185203 q .metric .DecBusyWorker ()
186- if err := recover (); err != nil {
204+ e := recover ()
205+ if e != nil {
187206 q .logger .Errorf ("panic error: %v" , err )
188207 }
189208 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+ }
190216 }()
191217
192- if err : = q .worker .Run (task ); err != nil {
218+ if err = q .worker .Run (task ); err != nil {
193219 q .logger .Errorf ("runtime error: %s" , err .Error ())
194220 }
195221}
0 commit comments