mirror of
https://github.com/hibiken/asynq.git
synced 2024-11-10 11:31:58 +08:00
Add heartbeat goroutine to write heartbeat data to redis for active
worker
This commit is contained in:
parent
80f477212d
commit
2e0af11ca0
25
processor.go
25
processor.go
@ -78,18 +78,37 @@ func (p *processor) exec() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
t := &Task{Type: msg.Type, Payload: msg.Payload}
|
task := &Task{Type: msg.Type, Payload: msg.Payload}
|
||||||
p.sema <- struct{}{} // acquire token
|
p.sema <- struct{}{} // acquire token
|
||||||
go func(task *Task) {
|
go func(task *Task) {
|
||||||
|
quit := make(chan struct{}) // channel to signal heartbeat goroutine
|
||||||
defer func() {
|
defer func() {
|
||||||
|
quit <- struct{}{}
|
||||||
if err := p.rdb.srem(inProgress, msg); err != nil {
|
if err := p.rdb.srem(inProgress, msg); err != nil {
|
||||||
log.Printf("[SERVER ERROR] SREM failed: %v\n", err)
|
log.Printf("[SERVER ERROR] SREM failed: %v\n", err)
|
||||||
}
|
}
|
||||||
|
if err := p.rdb.clearHeartbeat(msg.ID); err != nil {
|
||||||
|
log.Printf("[SERVER ERROR] DEL heartbeat failed: %v\n", err)
|
||||||
|
}
|
||||||
<-p.sema // release token
|
<-p.sema // release token
|
||||||
}()
|
}()
|
||||||
err := p.handler(task)
|
// start "heartbeat" goroutine
|
||||||
|
go func() {
|
||||||
|
ticker := time.NewTicker(5 * time.Second)
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-quit:
|
||||||
|
return
|
||||||
|
case t := <-ticker.C:
|
||||||
|
if err := p.rdb.heartbeat(msg.ID, t); err != nil {
|
||||||
|
log.Printf("[ERROR] heartbeat failed for %v at %v: %v", msg.ID, t, err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}()
|
||||||
|
err := p.handler(task) // TODO(hibiken): maybe also handle panic?
|
||||||
if err != nil {
|
if err != nil {
|
||||||
retryTask(p.rdb, msg, err)
|
retryTask(p.rdb, msg, err)
|
||||||
}
|
}
|
||||||
}(t)
|
}(task)
|
||||||
}
|
}
|
||||||
|
22
rdb.go
22
rdb.go
@ -9,16 +9,18 @@ import (
|
|||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/go-redis/redis/v7"
|
"github.com/go-redis/redis/v7"
|
||||||
|
"github.com/google/uuid"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Redis keys
|
// Redis keys
|
||||||
const (
|
const (
|
||||||
queuePrefix = "asynq:queues:" // LIST
|
queuePrefix = "asynq:queues:" // LIST - asynq:queues:<qname>
|
||||||
allQueues = "asynq:queues" // SET
|
allQueues = "asynq:queues" // SET
|
||||||
scheduled = "asynq:scheduled" // ZSET
|
scheduled = "asynq:scheduled" // ZSET
|
||||||
retry = "asynq:retry" // ZSET
|
retry = "asynq:retry" // ZSET
|
||||||
dead = "asynq:dead" // ZSET
|
dead = "asynq:dead" // ZSET
|
||||||
inProgress = "asynq:in_progress" // SET
|
inProgress = "asynq:in_progress" // SET
|
||||||
|
heartbeatPrefix = "asynq:heartbeat:" // STRING - asynq:heartbeat:<taskID>
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
@ -145,6 +147,24 @@ func (r *rdb) move(from string, msg *taskMessage) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (r *rdb) heartbeat(id uuid.UUID, timestamp time.Time) error {
|
||||||
|
key := heartbeatPrefix + id.String()
|
||||||
|
err := r.client.Set(key, timestamp, 0).Err() // zero expiration means no expiration
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("command SET %s %v failed: %v", key, timestamp, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func (r *rdb) clearHeartbeat(id uuid.UUID) error {
|
||||||
|
key := heartbeatPrefix + id.String()
|
||||||
|
err := r.client.Del(key).Err()
|
||||||
|
if err != nil {
|
||||||
|
return fmt.Errorf("command DEL %s failed: %v", key, err)
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
const maxDeadTask = 100
|
const maxDeadTask = 100
|
||||||
const deadExpirationInDays = 90
|
const deadExpirationInDays = 90
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user