-
Notifications
You must be signed in to change notification settings - Fork 603
/
termination_handler.go
92 lines (77 loc) · 2.82 KB
/
termination_handler.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
// Copyright 2014-2015 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file is distributed
// on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either
// express or implied. See the License for the specific language governing
// permissions and limitations under the License.
// sighandlers handle signals and behave appropriately.
// SIGTERM:
// Flush state to disk and exit
// SIGUSR1:
// Print a dump of goroutines to the logger and DON'T exit
package sighandlers
import (
"errors"
"os"
"os/signal"
"syscall"
"time"
"github.com/aws/amazon-ecs-agent/agent/engine"
"github.com/aws/amazon-ecs-agent/agent/logger"
"github.com/aws/amazon-ecs-agent/agent/sighandlers/exitcodes"
"github.com/aws/amazon-ecs-agent/agent/statemanager"
"github.com/aws/amazon-ecs-agent/agent/utils"
)
var log = logger.ForModule("TerminationHandler")
func StartTerminationHandler(saver statemanager.Saver, taskEngine engine.TaskEngine) {
signalChannel := make(chan os.Signal, 2)
signal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM)
sig := <-signalChannel
log.Debug("Received termination signal", "signal", sig.String())
err := FinalSave(saver, taskEngine)
if err != nil {
log.Crit("Error saving state before final shutdown", "err", err)
// Terminal because it's a sigterm; the user doesn't want it to restart
os.Exit(exitcodes.ExitTerminal)
}
os.Exit(exitcodes.ExitSuccess)
}
const engineDisableTimeout = 5 * time.Second
const finalSaveTimeout = 3 * time.Second
// FinalSave should be called immediately before exiting, and only before
// exiting, in order to flush tasks to disk. It waits a short timeout for state
// to settle if necessary. If unable to reach a steady-state and save within
// this short timeout, it returns an error
func FinalSave(saver statemanager.Saver, taskEngine engine.TaskEngine) error {
engineDisabled := make(chan error)
disableTimer := time.AfterFunc(engineDisableTimeout, func() {
engineDisabled <- errors.New("Timed out waiting for TaskEngine to settle")
})
go func() {
log.Debug("Shutting down task engine")
taskEngine.Disable()
disableTimer.Stop()
engineDisabled <- nil
}()
disableErr := <-engineDisabled
stateSaved := make(chan error)
saveTimer := time.AfterFunc(finalSaveTimeout, func() {
stateSaved <- errors.New("Timed out trying to save to disk")
})
go func() {
log.Debug("Saving state before shutting down")
stateSaved <- saver.ForceSave()
saveTimer.Stop()
}()
saveErr := <-stateSaved
if disableErr != nil || saveErr != nil {
return utils.NewMultiError(disableErr, saveErr)
}
return nil
}