forked from aws/aws-lambda-runtime-interface-emulator
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathwatchdog.go
More file actions
102 lines (87 loc) · 2.36 KB
/
watchdog.go
File metadata and controls
102 lines (87 loc) · 2.36 KB
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
102
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0
package core
import (
"fmt"
log "github.com/sirupsen/logrus"
"go.amzn.com/lambda/appctx"
"go.amzn.com/lambda/fatalerror"
"sync"
)
type WaitableProcess interface {
// Wait blocks until process exits and returns error in case of non-zero exit code
Wait() error
// Pid returnes process ID
Pid() int
// Name returnes process executable name (for logging)
Name() string
}
// Watchdog watches started goroutines.
type Watchdog struct {
cancelOnce sync.Once
initFlow InitFlowSynchronization
invokeFlow InvokeFlowSynchronization
exitPidChan chan<- int
appCtx appctx.ApplicationContext
mutedMutex sync.Mutex
muted bool
}
func (w *Watchdog) Mute() {
w.mutedMutex.Lock()
defer w.mutedMutex.Unlock()
w.muted = true
}
func (w *Watchdog) Unmute() {
w.mutedMutex.Lock()
defer w.mutedMutex.Unlock()
w.muted = false
}
func (w *Watchdog) Muted() bool {
w.mutedMutex.Lock()
defer w.mutedMutex.Unlock()
return w.muted
}
// GoWait waits for process to complete in separate goroutine and handles the process termination
// Returns PID of the process
func (w *Watchdog) GoWait(p WaitableProcess, errorType fatalerror.ErrorType) int {
pid := p.Pid()
name := p.Name()
appCtx := w.appCtx
go func() {
err := p.Wait()
if !w.Muted() {
appctx.StoreFirstFatalError(appCtx, errorType)
if err == nil {
err = fmt.Errorf("exit code 0")
}
log.Warnf("Process %d(%s) exited: %s", pid, name, err)
}
w.CancelFlows(err)
w.exitPidChan <- pid
}()
return pid
}
// CancelFlows cancels init and invoke flows with error.
func (w *Watchdog) CancelFlows(err error) {
// The following block protects us from overwriting the error
// which was first used to cancel flows.
w.cancelOnce.Do(func() {
log.Debugf("Canceling flows: %s", err)
w.initFlow.CancelWithError(err)
w.invokeFlow.CancelWithError(err)
})
}
// Clear watchdog state
func (w *Watchdog) Clear() {
w.cancelOnce = sync.Once{}
}
// NewWatchdog returns new instance of a Watchdog.
func NewWatchdog(initFlow InitFlowSynchronization, invokeFlow InvokeFlowSynchronization, exitPidChan chan<- int, appCtx appctx.ApplicationContext) *Watchdog {
return &Watchdog{
initFlow: initFlow,
invokeFlow: invokeFlow,
exitPidChan: exitPidChan,
appCtx: appCtx,
mutedMutex: sync.Mutex{},
}
}