2
0
mirror of https://github.com/hibiken/asynq.git synced 2024-12-27 16:13:40 +08:00
asynq/heartbeat.go

166 lines
4.0 KiB
Go
Raw Normal View History

2020-01-31 22:48:58 +08:00
// Copyright 2020 Kentaro Hibino. All rights reserved.
// Use of this source code is governed by a MIT license
// that can be found in the LICENSE file.
package asynq
import (
2020-05-19 11:47:35 +08:00
"os"
2020-02-16 15:14:30 +08:00
"sync"
2020-01-31 22:48:58 +08:00
"time"
"github.com/google/uuid"
2020-01-31 22:48:58 +08:00
"github.com/hibiken/asynq/internal/base"
2020-05-06 13:10:11 +08:00
"github.com/hibiken/asynq/internal/log"
2020-01-31 22:48:58 +08:00
)
2020-02-02 14:22:48 +08:00
// heartbeater is responsible for writing process info to redis periodically to
2020-01-31 22:48:58 +08:00
// indicate that the background worker process is up.
type heartbeater struct {
2020-05-06 13:10:11 +08:00
logger *log.Logger
2020-04-18 22:55:10 +08:00
broker base.Broker
2020-01-31 22:48:58 +08:00
// channel to communicate back to the long running "heartbeater" goroutine.
done chan struct{}
// interval between heartbeats.
interval time.Duration
2020-05-19 11:47:35 +08:00
// following fields are initialized at construction time and are immutable.
host string
pid int
serverID string
concurrency int
queues map[string]int
strictPriority bool
// following fields are mutable and should be accessed only by the
// heartbeater goroutine. In other words, confine these variables
// to this goroutine only.
started time.Time
workers map[string]workerStat
// status is shared with other goroutine but is concurrency safe.
status *base.ServerStatus
// channels to receive updates on active workers.
starting <-chan *base.TaskMessage
finished <-chan *base.TaskMessage
2020-01-31 22:48:58 +08:00
}
type heartbeaterParams struct {
2020-05-19 11:47:35 +08:00
logger *log.Logger
broker base.Broker
interval time.Duration
concurrency int
queues map[string]int
strictPriority bool
status *base.ServerStatus
starting <-chan *base.TaskMessage
finished <-chan *base.TaskMessage
}
func newHeartbeater(params heartbeaterParams) *heartbeater {
2020-05-19 11:47:35 +08:00
host, err := os.Hostname()
if err != nil {
host = "unknown-host"
}
2020-01-31 22:48:58 +08:00
return &heartbeater{
logger: params.logger,
broker: params.broker,
2020-01-31 22:48:58 +08:00
done: make(chan struct{}),
interval: params.interval,
2020-05-19 11:47:35 +08:00
host: host,
pid: os.Getpid(),
serverID: uuid.New().String(),
2020-05-19 11:47:35 +08:00
concurrency: params.concurrency,
queues: params.queues,
strictPriority: params.strictPriority,
status: params.status,
workers: make(map[string]workerStat),
starting: params.starting,
finished: params.finished,
2020-01-31 22:48:58 +08:00
}
}
func (h *heartbeater) terminate() {
h.logger.Debug("Heartbeater shutting down...")
2020-01-31 22:48:58 +08:00
// Signal the heartbeater goroutine to stop.
h.done <- struct{}{}
}
2020-05-19 11:47:35 +08:00
// A workerStat records the message a worker is working on
// and the time the worker has started processing the message.
type workerStat struct {
started time.Time
msg *base.TaskMessage
}
2020-02-16 15:14:30 +08:00
func (h *heartbeater) start(wg *sync.WaitGroup) {
wg.Add(1)
2020-01-31 22:48:58 +08:00
go func() {
2020-02-16 15:14:30 +08:00
defer wg.Done()
2020-05-19 11:47:35 +08:00
h.started = time.Now()
2020-02-02 14:22:48 +08:00
h.beat()
2020-05-19 11:47:35 +08:00
timer := time.NewTimer(h.interval)
2020-01-31 22:48:58 +08:00
for {
select {
case <-h.done:
2020-05-19 11:47:35 +08:00
h.broker.ClearServerState(h.host, h.pid, h.serverID)
h.logger.Debug("Heartbeater done")
2020-05-19 11:47:35 +08:00
timer.Stop()
2020-01-31 22:48:58 +08:00
return
2020-05-19 11:47:35 +08:00
case <-timer.C:
2020-02-02 14:22:48 +08:00
h.beat()
2020-05-19 11:47:35 +08:00
timer.Reset(h.interval)
case msg := <-h.starting:
h.workers[msg.ID.String()] = workerStat{time.Now(), msg}
case msg := <-h.finished:
delete(h.workers, msg.ID.String())
2020-01-31 22:48:58 +08:00
}
}
}()
}
2020-02-02 14:22:48 +08:00
func (h *heartbeater) beat() {
2020-05-19 11:47:35 +08:00
info := base.ServerInfo{
Host: h.host,
PID: h.pid,
ServerID: h.serverID,
Concurrency: h.concurrency,
Queues: h.queues,
StrictPriority: h.strictPriority,
Status: h.status.String(),
Started: h.started,
ActiveWorkerCount: len(h.workers),
}
var ws []*base.WorkerInfo
for id, stat := range h.workers {
ws = append(ws, &base.WorkerInfo{
Host: h.host,
PID: h.pid,
ID: id,
Type: stat.msg.Type,
Queue: stat.msg.Queue,
Payload: stat.msg.Payload,
Started: stat.started,
})
}
2020-02-02 14:22:48 +08:00
// Note: Set TTL to be long enough so that it won't expire before we write again
// and short enough to expire quickly once the process is shut down or killed.
2020-05-19 11:47:35 +08:00
if err := h.broker.WriteServerState(&info, ws, h.interval*2); err != nil {
h.logger.Errorf("could not write server state data: %v", err)
2020-02-02 14:22:48 +08:00
}
}