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-02-16 15:14:30 +08:00
|
|
|
"sync"
|
2020-01-31 22:48:58 +08:00
|
|
|
"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 {
|
2020-03-12 22:31:10 +08:00
|
|
|
logger Logger
|
2020-03-09 22:11:16 +08:00
|
|
|
rdb *rdb.RDB
|
2020-01-31 22:48:58 +08:00
|
|
|
|
2020-02-18 22:57:39 +08:00
|
|
|
ps *base.ProcessState
|
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-03-12 22:31:10 +08:00
|
|
|
func newHeartbeater(l Logger, rdb *rdb.RDB, ps *base.ProcessState, interval time.Duration) *heartbeater {
|
2020-01-31 22:48:58 +08:00
|
|
|
return &heartbeater{
|
2020-03-09 22:11:16 +08:00
|
|
|
logger: l,
|
2020-01-31 22:48:58 +08:00
|
|
|
rdb: rdb,
|
2020-02-18 22:57:39 +08:00
|
|
|
ps: ps,
|
2020-01-31 22:48:58 +08:00
|
|
|
done: make(chan struct{}),
|
|
|
|
interval: interval,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *heartbeater) terminate() {
|
2020-03-09 22:11:16 +08:00
|
|
|
h.logger.Info("Heartbeater shutting down...")
|
2020-01-31 22:48:58 +08:00
|
|
|
// Signal the heartbeater goroutine to stop.
|
|
|
|
h.done <- struct{}{}
|
|
|
|
}
|
|
|
|
|
2020-02-16 15:14:30 +08:00
|
|
|
func (h *heartbeater) start(wg *sync.WaitGroup) {
|
2020-02-18 22:57:39 +08:00
|
|
|
h.ps.SetStarted(time.Now())
|
|
|
|
h.ps.SetStatus(base.StatusRunning)
|
2020-02-16 15:14:30 +08:00
|
|
|
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-02-02 14:22:48 +08:00
|
|
|
h.beat()
|
2020-01-31 22:48:58 +08:00
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-h.done:
|
2020-02-21 23:18:22 +08:00
|
|
|
h.rdb.ClearProcessState(h.ps)
|
2020-03-09 22:11:16 +08:00
|
|
|
h.logger.Info("Heartbeater done")
|
2020-01-31 22:48:58 +08:00
|
|
|
return
|
2020-02-18 22:57:39 +08:00
|
|
|
case <-time.After(h.interval):
|
2020-02-02 14:22:48 +08:00
|
|
|
h.beat()
|
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.
|
2020-02-21 23:18:22 +08:00
|
|
|
err := h.rdb.WriteProcessState(h.ps, h.interval*2)
|
2020-02-02 14:22:48 +08:00
|
|
|
if err != nil {
|
2020-03-09 22:11:16 +08:00
|
|
|
h.logger.Error("could not write heartbeat data: %v", err)
|
2020-02-02 14:22:48 +08:00
|
|
|
}
|
|
|
|
}
|