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 (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hibiken/asynq/internal/base"
|
|
|
|
"github.com/hibiken/asynq/internal/rdb"
|
|
|
|
)
|
|
|
|
|
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 {
|
|
|
|
rdb *rdb.RDB
|
|
|
|
|
2020-02-02 14:22:48 +08:00
|
|
|
pinfo *base.ProcessInfo
|
2020-01-31 22:48:58 +08:00
|
|
|
|
|
|
|
// channel to communicate back to the long running "heartbeater" goroutine.
|
|
|
|
done chan struct{}
|
|
|
|
|
2020-02-16 13:59:06 +08:00
|
|
|
// channel to receive updates on process state.
|
|
|
|
stateCh <-chan string
|
|
|
|
|
|
|
|
// channel to recieve updates on workers count.
|
|
|
|
workerCh <-chan int
|
|
|
|
|
2020-01-31 22:48:58 +08:00
|
|
|
// interval between heartbeats.
|
|
|
|
interval time.Duration
|
|
|
|
}
|
|
|
|
|
2020-02-16 13:59:06 +08:00
|
|
|
func newHeartbeater(rdb *rdb.RDB, host string, pid, concurrency int, queues map[string]int, strict bool,
|
|
|
|
interval time.Duration, stateCh <-chan string, workerCh <-chan int) *heartbeater {
|
2020-01-31 22:48:58 +08:00
|
|
|
return &heartbeater{
|
|
|
|
rdb: rdb,
|
2020-02-16 13:59:06 +08:00
|
|
|
pinfo: base.NewProcessInfo(host, pid, concurrency, queues, strict),
|
2020-01-31 22:48:58 +08:00
|
|
|
done: make(chan struct{}),
|
2020-02-16 13:59:06 +08:00
|
|
|
stateCh: stateCh,
|
|
|
|
workerCh: workerCh,
|
2020-01-31 22:48:58 +08:00
|
|
|
interval: interval,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *heartbeater) terminate() {
|
|
|
|
logger.info("Heartbeater shutting down...")
|
|
|
|
// Signal the heartbeater goroutine to stop.
|
|
|
|
h.done <- struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *heartbeater) start() {
|
2020-02-16 13:59:06 +08:00
|
|
|
h.pinfo.Started = time.Now()
|
|
|
|
h.pinfo.State = "running"
|
2020-01-31 22:48:58 +08:00
|
|
|
go func() {
|
2020-02-02 14:22:48 +08:00
|
|
|
h.beat()
|
2020-02-16 13:59:06 +08:00
|
|
|
timer := time.NewTimer(h.interval)
|
2020-01-31 22:48:58 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-h.done:
|
2020-02-16 13:59:06 +08:00
|
|
|
h.rdb.ClearProcessInfo(h.pinfo)
|
2020-01-31 22:48:58 +08:00
|
|
|
logger.info("Heartbeater done")
|
|
|
|
return
|
2020-02-16 13:59:06 +08:00
|
|
|
case state := <-h.stateCh:
|
|
|
|
h.pinfo.State = state
|
|
|
|
case delta := <-h.workerCh:
|
|
|
|
h.pinfo.ActiveWorkerCount += delta
|
|
|
|
case <-timer.C:
|
2020-02-02 14:22:48 +08:00
|
|
|
h.beat()
|
2020-02-16 13:59:06 +08:00
|
|
|
timer.Reset(h.interval)
|
2020-01-31 22:48:58 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
2020-02-02 14:22:48 +08:00
|
|
|
|
|
|
|
func (h *heartbeater) beat() {
|
|
|
|
// 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.
|
|
|
|
err := h.rdb.WriteProcessInfo(h.pinfo, h.interval*2)
|
|
|
|
if err != nil {
|
|
|
|
logger.error("could not write heartbeat data: %v", err)
|
|
|
|
}
|
|
|
|
}
|