-
Notifications
You must be signed in to change notification settings - Fork 491
/
worker.go
101 lines (87 loc) · 2.31 KB
/
worker.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// Copyright 2021 Canonical Ltd.
// Licensed under the AGPLv3, see LICENCE file for details.
package caasunitterminationworker
import (
"os"
"os/signal"
"syscall"
"github.com/juju/clock"
"github.com/juju/errors"
"github.com/juju/names/v5"
"github.com/juju/worker/v3"
"gopkg.in/tomb.v2"
"github.com/juju/juju/agent"
"github.com/juju/juju/api/agent/caasapplication"
)
// TerminationSignal is SIGTERM which is sent by most container runtimes when
// a container should terminate gracefully.
const TerminationSignal = syscall.SIGTERM
type terminationWorker struct {
tomb tomb.Tomb
agent agent.Agent
state State
unitTerminator UnitTerminator
logger Logger
clock clock.Clock
}
type Config struct {
Agent agent.Agent
State State
UnitTerminator UnitTerminator
Logger Logger
Clock clock.Clock
}
type State interface {
UnitTerminating(tag names.UnitTag) (caasapplication.UnitTermination, error)
}
type UnitTerminator interface {
Terminate() error
}
// NewWorker returns a worker that waits for a
// TerminationSignal signal, and then exits
// with worker.ErrTerminateAgent.
func NewWorker(config Config) worker.Worker {
w := terminationWorker{
agent: config.Agent,
state: config.State,
unitTerminator: config.UnitTerminator,
logger: config.Logger,
clock: config.Clock,
}
c := make(chan os.Signal, 1)
signal.Notify(c, TerminationSignal)
w.tomb.Go(func() error {
defer signal.Stop(c)
return w.loop(c)
})
return &w
}
func (w *terminationWorker) Kill() {
w.tomb.Kill(nil)
}
func (w *terminationWorker) Wait() error {
return w.tomb.Wait()
}
func (w *terminationWorker) loop(c <-chan os.Signal) (err error) {
select {
case <-c:
w.logger.Infof("terminating due to SIGTERM")
term, err := w.state.UnitTerminating(w.agent.CurrentConfig().Tag().(names.UnitTag))
if err != nil {
w.logger.Errorf("error while terminating unit: %v", err)
return err
}
if !term.WillRestart {
// Lifecycle watcher will handle termination of the unit.
return nil
}
err = w.unitTerminator.Terminate()
if err != nil {
w.logger.Errorf("error while terminating unit: %v", err)
return errors.Annotatef(err, "failed to terminate unit agent worker")
}
return nil
case <-w.tomb.Dying():
return tomb.ErrDying
}
}