Skip to content

Commit

Permalink
Stop all services on a panic
Browse files Browse the repository at this point in the history
  • Loading branch information
jspc committed Feb 26, 2023
1 parent 3399f48 commit 48ad542
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,19 @@ import (
)

var (
// supervisor is the thing that handles all of the
// running of services, including tracking whether or not
// they're even running
//
// Why is this a global variable?
// Because we need to be able to stop all services and drop to
// a shell if our init process panics, so we can suss out what
// happened.
//
// Without a global, this needs all kinds of software architecture
// changes
supervisor *Supervisor

sockAddr = "/run/vinit.sock"
svcDir = envOrDefault("SVC_DIR", "/etc/vinit/services")
certDir = "certs"
Expand Down Expand Up @@ -78,8 +91,6 @@ func main() {
}

func Setup() (grpcServer *grpc.Server, err error) {
var supervisor *Supervisor

supervisor, err = New(svcDir)
if err != nil {
if _, ok := err.(ConfigParseError); !ok {
Expand Down Expand Up @@ -147,6 +158,10 @@ func loadTLSCredentials() (credentials.TransportCredentials, error) {
func panicHandler() {
err := recover()
if err != nil {
if supervisor != nil {
supervisor.StopAll()
}

stack := string(debug.Stack())
errStr := err.(error).Error()

Expand Down

0 comments on commit 48ad542

Please sign in to comment.