-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: pass through signals to inner container (#83)
- Loading branch information
Showing
10 changed files
with
247 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,44 @@ | ||
package main | ||
|
||
import ( | ||
"fmt" | ||
"context" | ||
"os" | ||
"os/signal" | ||
"runtime" | ||
"syscall" | ||
|
||
"cdr.dev/slog" | ||
"cdr.dev/slog/sloggers/slogjson" | ||
"github.com/coder/envbox/cli" | ||
) | ||
|
||
func main() { | ||
_, err := cli.Root().ExecuteC() | ||
ch := make(chan func() error, 1) | ||
sigs := make(chan os.Signal, 1) | ||
signal.Notify(sigs, syscall.SIGTERM, syscall.SIGINT, syscall.SIGWINCH) | ||
go func() { | ||
log := slog.Make(slogjson.Sink(os.Stderr)) | ||
ctx := context.Background() | ||
log.Info(ctx, "waiting for signal") | ||
<-sigs | ||
log.Info(ctx, "got signal") | ||
select { | ||
case fn := <-ch: | ||
log.Info(ctx, "running shutdown function") | ||
err := fn() | ||
if err != nil { | ||
log.Error(ctx, "shutdown function failed", slog.Error(err)) | ||
os.Exit(1) | ||
} | ||
default: | ||
log.Info(ctx, "no shutdown function") | ||
} | ||
log.Info(ctx, "exiting") | ||
os.Exit(0) | ||
}() | ||
_, err := cli.Root(ch).ExecuteC() | ||
if err != nil { | ||
_, _ = fmt.Fprintln(os.Stderr, err.Error()) | ||
os.Exit(1) | ||
} | ||
|
||
// We exit the main thread while keepin all the other procs goin strong. | ||
runtime.Goexit() | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.