mirror of
https://github.com/hibiken/asynq.git
synced 2024-12-27 00:02:19 +08:00
Make Launcher thread safe
This commit is contained in:
parent
e238d3835d
commit
eb6a5032f6
8
asynq.go
8
asynq.go
@ -15,6 +15,7 @@ import (
|
|||||||
"math"
|
"math"
|
||||||
"math/rand"
|
"math/rand"
|
||||||
"strconv"
|
"strconv"
|
||||||
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v7"
|
"github.com/go-redis/redis/v7"
|
||||||
@ -105,8 +106,9 @@ func (c *Client) enqueue(msg *taskMessage, executeAt time.Time) error {
|
|||||||
type Launcher struct {
|
type Launcher struct {
|
||||||
rdb *redis.Client
|
rdb *redis.Client
|
||||||
|
|
||||||
// running indicates whether the workes are currently running.
|
// running indicates whether manager and poller are both running.
|
||||||
running bool
|
running bool
|
||||||
|
mu sync.Mutex
|
||||||
|
|
||||||
poller *poller
|
poller *poller
|
||||||
|
|
||||||
@ -135,6 +137,8 @@ type TaskHandler func(*Task) error
|
|||||||
|
|
||||||
// Start starts the manager and poller.
|
// Start starts the manager and poller.
|
||||||
func (l *Launcher) Start(handler TaskHandler) {
|
func (l *Launcher) Start(handler TaskHandler) {
|
||||||
|
l.mu.Lock()
|
||||||
|
defer l.mu.Unlock()
|
||||||
if l.running {
|
if l.running {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -147,6 +151,8 @@ func (l *Launcher) Start(handler TaskHandler) {
|
|||||||
|
|
||||||
// Stop stops both manager and poller.
|
// Stop stops both manager and poller.
|
||||||
func (l *Launcher) Stop() {
|
func (l *Launcher) Stop() {
|
||||||
|
l.mu.Lock()
|
||||||
|
defer l.mu.Unlock()
|
||||||
if !l.running {
|
if !l.running {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user