Skip to content

Commit

Permalink
postalpool: concurrently spin up workers
Browse files Browse the repository at this point in the history
  • Loading branch information
adamdecaf committed Jan 29, 2025
1 parent 7e1a353 commit 2374e24
Showing 1 changed file with 22 additions and 11 deletions.
33 changes: 22 additions & 11 deletions internal/postalpool/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/moov-io/base/telemetry"
"go.opentelemetry.io/otel/attribute"
"go.opentelemetry.io/otel/trace"
"golang.org/x/sync/errgroup"
)

type Service struct {
Expand All @@ -38,28 +39,38 @@ func NewService(logger log.Logger, conf Config) (*Service, error) {
processes: make([]*exec.Cmd, conf.Instances),
}

var g errgroup.Group
endpoints := make([]string, conf.Instances)
for i := 0; i < conf.Instances; i++ {
port := conf.StartingPort + i
idx := i

cmd := exec.Command(binPath)
cmd.Env = append(cmd.Env, fmt.Sprintf("PORT=%d", port))
port := conf.StartingPort + idx
endpoints[idx] = fmt.Sprintf("http://localhost:%d", port)

err := cmd.Start()
if err != nil {
ps.Shutdown()
g.Go(func() error {
cmd := exec.Command(binPath)
cmd.Env = append(cmd.Env, fmt.Sprintf("PORT=%d", port))

return nil, fmt.Errorf("failed to start postal instance %d: %w", i, err)
}
err := cmd.Start()
if err != nil {
ps.Shutdown()

return fmt.Errorf("failed to start postal instance %d: %w", i, err)
}

ps.processes[i] = cmd
ps.processes[idx] = cmd

endpoints[i] = fmt.Sprintf("http://localhost:%d", port)
return nil
})
}
err := g.Wait()
if err != nil {
return nil, fmt.Errorf("problem starting postalpool workers: %w", err)
}

ps.client = NewClient(conf, endpoints)

err := ps.client.healthcheck(ctx)
err = ps.client.healthcheck(ctx)
if err != nil {
return nil, fmt.Errorf("problem with postalpool healthcheck: %w", err)
}
Expand Down

0 comments on commit 2374e24

Please sign in to comment.