Skip to content

Commit

Permalink
Merge pull request #29 from pyrocat101/svcctlPort
Browse files Browse the repository at this point in the history
make SVCCTL_PORT available to services
  • Loading branch information
dzbarsky authored Sep 20, 2024
2 parents 13a9b01 + 1acd12d commit 865c7ad
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions cmd/svcinit/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,20 @@ func main() {
ports, err := assignPorts(unversionedSpecs)
must(err)

serviceSpecs, err := augmentServiceSpecs(unversionedSpecs, ports)
listener, err := net.Listen("tcp", "127.0.0.1:0")
must(err)

svcctlPort := listener.Addr().(*net.TCPAddr).Port
svcctlPortStr := strconv.Itoa(svcctlPort)
os.Setenv("SVCCTL_PORT", svcctlPortStr)

if testLabel == "" {
err = os.WriteFile("/tmp/svcctl_port", []byte(svcctlPortStr), 0600)
must(err)
defer os.Remove("/tmp/svcctl_port")
}

serviceSpecs, err := augmentServiceSpecs(unversionedSpecs, ports, svcctlPortStr)
must(err)

ctx, cancelFunc := context.WithCancel(context.Background())
Expand All @@ -112,10 +125,7 @@ func main() {
r, err := runner.New(ctx, serviceSpecs)
must(err)

servicesErrCh := make(chan error, len(serviceSpecs))

listener, err := net.Listen("tcp", "127.0.0.1:0")
must(err)
servicesErrCh := make(chan error, len(unversionedSpecs))

go func() {
defer listener.Close()
Expand All @@ -125,16 +135,6 @@ func main() {
}
}()

port := listener.Addr().(*net.TCPAddr).Port
portString := strconv.Itoa(port)
os.Setenv("SVCCTL_PORT", portString)

if testLabel == "" {
err = os.WriteFile("/tmp/svcctl_port", []byte(portString), 0600)
must(err)
defer os.Remove("/tmp/svcctl_port")
}

signalCh := make(chan os.Signal, 1)
signal.Notify(signalCh, syscall.SIGINT, syscall.SIGTERM)
go func() {
Expand Down Expand Up @@ -281,7 +281,7 @@ func main() {
unversionedSpecs, err := readServiceSpecs(serviceSpecsPath)
must(err)

serviceSpecs, err := augmentServiceSpecs(unversionedSpecs, ports)
serviceSpecs, err := augmentServiceSpecs(unversionedSpecs, ports, svcctlPortStr)
must(err)

// TODO(zbarsky): what is the right behavior here when services are crashing in ibazel mode?
Expand Down Expand Up @@ -387,6 +387,7 @@ func assignPorts(
func augmentServiceSpecs(
serviceSpecs map[string]svclib.ServiceSpec,
ports svclib.Ports,
svcctlPort string,
) (
map[string]svclib.VersionedServiceSpec, error,
) {
Expand Down Expand Up @@ -446,6 +447,7 @@ func augmentServiceSpecs(
s.Env[k] = strings.ReplaceAll(v, "$${PORT}", port)
}
}
s.Env["SVCCTL_PORT"] = svcctlPort

versionedServiceSpecs[label] = s
}
Expand Down

0 comments on commit 865c7ad

Please sign in to comment.