Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc083608

Browse files
committed
feat(agent): Handle signals and shutdown gracefully
This change allows the agent to handle common shutdown signals likeinterrupt, hangup and terminate and initiate a graceful shutdown.As long as terraform providers initiate graceful shutdowns via theaforementioned signals, things like SSH connections will be closedimmediately on shutdown instead of being left hanging/timing out due tothe agent being abruptly killed.Refs:#4677,#5901
1 parent896158c commitc083608

File tree

1 file changed

+30
-10
lines changed

1 file changed

+30
-10
lines changed

‎cli/agent.go

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"net/http/pprof"
88
"net/url"
99
"os"
10+
"os/signal"
1011
"path/filepath"
1112
"runtime"
1213
"time"
@@ -35,12 +36,10 @@ func workspaceAgent() *cobra.Command {
3536
Use:"agent",
3637
// This command isn't useful to manually execute.
3738
Hidden:true,
38-
RunE:func(cmd*cobra.Command,args []string)error {
39+
RunE:func(cmd*cobra.Command,_ []string)error {
3940
ctx,cancel:=context.WithCancel(cmd.Context())
4041
defercancel()
4142

42-
godumpHandler(ctx)
43-
4443
rawURL,err:=cmd.Flags().GetString(varAgentURL)
4544
iferr!=nil {
4645
returnxerrors.Errorf("CODER_AGENT_URL must be set: %w",err)
@@ -50,18 +49,18 @@ func workspaceAgent() *cobra.Command {
5049
returnxerrors.Errorf("parse %q: %w",rawURL,err)
5150
}
5251

53-
logWriter:=&lumberjack.Logger{
54-
Filename:filepath.Join(os.TempDir(),"coder-agent.log"),
55-
MaxSize:5,// MB
56-
}
57-
deferlogWriter.Close()
58-
logger:=slog.Make(sloghuman.Sink(cmd.ErrOrStderr()),sloghuman.Sink(logWriter)).Leveled(slog.LevelDebug)
59-
6052
isLinux:=runtime.GOOS=="linux"
6153

6254
// Spawn a reaper so that we don't accumulate a ton
6355
// of zombie processes.
6456
ifreaper.IsInitProcess()&&!noReap&&isLinux {
57+
logWriter:=&lumberjack.Logger{
58+
Filename:filepath.Join(os.TempDir(),"coder-agent-init.log"),
59+
MaxSize:5,// MB
60+
}
61+
deferlogWriter.Close()
62+
logger:=slog.Make(sloghuman.Sink(cmd.ErrOrStderr()),sloghuman.Sink(logWriter)).Leveled(slog.LevelDebug)
63+
6564
logger.Info(ctx,"spawning reaper process")
6665
// Do not start a reaper on the child process. It's important
6766
// to do this else we fork bomb ourselves.
@@ -76,6 +75,27 @@ func workspaceAgent() *cobra.Command {
7675
returnnil
7776
}
7877

78+
// Handle interrupt signals to allow for graceful shutdown,
79+
// note that calling stopNotify disables the signal handler
80+
// and the next interrupt will terminate the program.
81+
//
82+
// Note that we don't want to handle these signals in the
83+
// process that runs as PID 1, that's why we do this after
84+
// the reaper forked.
85+
ctx,stopNotify:=signal.NotifyContext(ctx,InterruptSignals...)
86+
deferstopNotify()
87+
88+
// dumpHandler does signal handling, so we call it after the
89+
// reaper.
90+
godumpHandler(ctx)
91+
92+
logWriter:=&lumberjack.Logger{
93+
Filename:filepath.Join(os.TempDir(),"coder-agent.log"),
94+
MaxSize:5,// MB
95+
}
96+
deferlogWriter.Close()
97+
logger:=slog.Make(sloghuman.Sink(cmd.ErrOrStderr()),sloghuman.Sink(logWriter)).Leveled(slog.LevelDebug)
98+
7999
version:=buildinfo.Version()
80100
logger.Info(ctx,"starting agent",
81101
slog.F("url",coderURL),

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp