forked from akash-network/provider
-
Notifications
You must be signed in to change notification settings - Fork 0
/
watchdog.go
84 lines (67 loc) · 1.84 KB
/
watchdog.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package manifest
import (
"context"
"time"
"github.com/boz/go-lifecycle"
"github.com/tendermint/tendermint/libs/log"
"github.com/akash-network/node/util/runner"
dtypes "github.com/akash-network/node/x/deployment/types/v1beta2"
types "github.com/akash-network/node/x/market/types/v1beta2"
"github.com/akash-network/provider/session"
)
type watchdog struct {
leaseID types.LeaseID
timeout time.Duration
lc lifecycle.Lifecycle
sess session.Session
ctx context.Context
log log.Logger
}
func newWatchdog(sess session.Session, parent <-chan struct{}, done chan<- dtypes.DeploymentID, leaseID types.LeaseID, timeout time.Duration) *watchdog {
ctx, cancel := context.WithCancel(context.Background())
result := &watchdog{
leaseID: leaseID,
timeout: timeout,
lc: lifecycle.New(),
sess: sess,
ctx: ctx,
log: sess.Log().With("leaseID", leaseID),
}
go func() {
result.lc.WatchChannel(parent)
cancel()
}()
go func() {
<-result.lc.Done()
done <- leaseID.DeploymentID()
}()
go result.run()
return result
}
func (wd *watchdog) stop() {
wd.lc.ShutdownAsync(nil)
}
func (wd *watchdog) run() {
defer wd.lc.ShutdownCompleted()
var runch <-chan runner.Result
var err error
wd.log.Debug("watchdog start")
select {
case <-time.After(wd.timeout):
// Close the bid, since if this point is reached then a manifest has not been received
wd.log.Info("watchdog closing bid")
runch = runner.Do(func() runner.Result {
return runner.NewResult(nil, wd.sess.Client().Tx().Broadcast(wd.ctx, &types.MsgCloseBid{
BidID: types.MakeBidID(wd.leaseID.OrderID(), wd.sess.Provider().Address()),
}))
})
case err = <-wd.lc.ShutdownRequest():
}
wd.lc.ShutdownInitiated(err)
if runch != nil {
result := <-runch
if err := result.Error(); err != nil {
wd.log.Error("failed closing bid", "err", err)
}
}
}