15954: Rename booter to supervisor, tidy up.
[arvados.git] / lib / boot / service.go
index 0ebf647adffb448aa7943fdf9d9585eb74554e61..018e9f8bb06fe096a8c680fe7f68b36d809b16c6 100644 (file)
@@ -6,41 +6,52 @@ package boot
 
 import (
        "context"
+       "errors"
        "path/filepath"
 
        "git.arvados.org/arvados.git/sdk/go/arvados"
 )
 
+// Run a service using the arvados-server binary.
+//
+// In future this will bring up the service in the current process,
+// but for now (at least until the subcommand handlers get a shutdown
+// mechanism) it starts a child process using the arvados-server
+// binary, which the supervisor is assumed to have installed in
+// {super.tempdir}/bin/.
 type runServiceCommand struct {
-       name    string
-       svc     arvados.Service
-       depends []bootTask
+       name    string           // arvados-server subcommand, e.g., "controller"
+       svc     arvados.Service  // cluster.Services.* entry with the desired InternalURLs
+       depends []supervisedTask // wait for these tasks before starting
 }
 
 func (runner runServiceCommand) String() string {
        return runner.name
 }
 
-func (runner runServiceCommand) Run(ctx context.Context, fail func(error), boot *Booter) error {
-       boot.wait(ctx, runner.depends...)
-       binfile := filepath.Join(boot.tempdir, "bin", "arvados-server")
-       err := boot.RunProgram(ctx, boot.tempdir, nil, nil, binfile, "-version")
+func (runner runServiceCommand) Run(ctx context.Context, fail func(error), super *Supervisor) error {
+       binfile := filepath.Join(super.tempdir, "bin", "arvados-server")
+       err := super.RunProgram(ctx, super.tempdir, nil, nil, binfile, "-version")
        if err != nil {
                return err
        }
-       go func() {
-               var u arvados.URL
-               for u = range runner.svc.InternalURLs {
-               }
-               fail(boot.RunProgram(ctx, boot.tempdir, nil, []string{"ARVADOS_SERVICE_INTERNAL_URL=" + u.String()}, binfile, runner.name, "-config", boot.configfile))
-       }()
+       super.wait(ctx, runner.depends...)
+       for u := range runner.svc.InternalURLs {
+               u := u
+               super.waitShutdown.Add(1)
+               go func() {
+                       defer super.waitShutdown.Done()
+                       fail(super.RunProgram(ctx, super.tempdir, nil, []string{"ARVADOS_SERVICE_INTERNAL_URL=" + u.String()}, binfile, runner.name, "-config", super.configfile))
+               }()
+       }
        return nil
 }
 
+// Run a Go service that isn't bundled in arvados-server.
 type runGoProgram struct {
-       src     string
-       svc     arvados.Service
-       depends []bootTask
+       src     string           // source dir, e.g., "services/keepproxy"
+       svc     arvados.Service  // cluster.Services.* entry with the desired InternalURLs
+       depends []supervisedTask // wait for these tasks before starting
 }
 
 func (runner runGoProgram) String() string {
@@ -48,9 +59,12 @@ func (runner runGoProgram) String() string {
        return basename
 }
 
-func (runner runGoProgram) Run(ctx context.Context, fail func(error), boot *Booter) error {
-       boot.wait(ctx, runner.depends...)
-       binfile, err := boot.installGoProgram(ctx, runner.src)
+func (runner runGoProgram) Run(ctx context.Context, fail func(error), super *Supervisor) error {
+       if len(runner.svc.InternalURLs) == 0 {
+               return errors.New("bug: runGoProgram needs non-empty svc.InternalURLs")
+       }
+
+       binfile, err := super.installGoProgram(ctx, runner.src)
        if err != nil {
                return err
        }
@@ -58,26 +72,18 @@ func (runner runGoProgram) Run(ctx context.Context, fail func(error), boot *Boot
                return ctx.Err()
        }
 
-       err = boot.RunProgram(ctx, boot.tempdir, nil, nil, binfile, "-version")
+       err = super.RunProgram(ctx, super.tempdir, nil, nil, binfile, "-version")
        if err != nil {
                return err
        }
-       if len(runner.svc.InternalURLs) > 0 {
-               // Run one for each URL
-               for u := range runner.svc.InternalURLs {
-                       u := u
-                       boot.waitShutdown.Add(1)
-                       go func() {
-                               defer boot.waitShutdown.Done()
-                               fail(boot.RunProgram(ctx, boot.tempdir, nil, []string{"ARVADOS_SERVICE_INTERNAL_URL=" + u.String()}, binfile))
-                       }()
-               }
-       } else {
-               // Just run one
-               boot.waitShutdown.Add(1)
+
+       super.wait(ctx, runner.depends...)
+       for u := range runner.svc.InternalURLs {
+               u := u
+               super.waitShutdown.Add(1)
                go func() {
-                       defer boot.waitShutdown.Done()
-                       fail(boot.RunProgram(ctx, boot.tempdir, nil, nil, binfile))
+                       defer super.waitShutdown.Done()
+                       fail(super.RunProgram(ctx, super.tempdir, nil, []string{"ARVADOS_SERVICE_INTERNAL_URL=" + u.String()}, binfile))
                }()
        }
        return nil