Skip to content

Commit c53b473

Browse files
authored
chore(simple): support stop flag (#20)
Signed-off-by: Bo-Yi Wu <[email protected]>
1 parent 2a43c22 commit c53b473

File tree

1 file changed

+7
-3
lines changed

1 file changed

+7
-3
lines changed

simple/simple.go

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"errors"
66
"sync"
7+
"sync/atomic"
78
"time"
89

910
"github.com/appleboy/queue"
@@ -25,6 +26,7 @@ type Worker struct {
2526
stop chan struct{}
2627
logger queue.Logger
2728
stopOnce sync.Once
29+
stopFlag int32
2830
}
2931

3032
// BeforeRun run script before start worker
@@ -102,6 +104,10 @@ func (s *Worker) Run() error {
102104

103105
// Shutdown worker
104106
func (s *Worker) Shutdown() error {
107+
if !atomic.CompareAndSwapInt32(&s.stopFlag, 0, 1) {
108+
return queue.ErrQueueShutdown
109+
}
110+
105111
s.stopOnce.Do(func() {
106112
close(s.stop)
107113
close(s.taskQueue)
@@ -121,10 +127,8 @@ func (s *Worker) Usage() int {
121127

122128
// Queue send notification to queue
123129
func (s *Worker) Queue(job queue.QueuedMessage) error {
124-
select {
125-
case <-s.stop:
130+
if atomic.LoadInt32(&s.stopFlag) == 1 {
126131
return queue.ErrQueueShutdown
127-
default:
128132
}
129133

130134
select {

0 commit comments

Comments
 (0)