10666: Added version number to go sdk and go tools & services
[arvados.git] / services / crunch-dispatch-local / crunch-dispatch-local.go
index 936a9088ed0c3d3affe6c3e0f9555d9e230d0c99..c1d6d562a2c963209be94a590fcd14bc8eea272b 100644 (file)
@@ -1,17 +1,27 @@
+// Copyright (C) The Arvados Authors. All rights reserved.
+//
+// SPDX-License-Identifier: AGPL-3.0
+
 package main
 
 // Dispatcher service for Crunch that runs containers locally.
 
 import (
+       "context"
        "flag"
-       "git.curoverse.com/arvados.git/sdk/go/arvados"
-       "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
-       "git.curoverse.com/arvados.git/sdk/go/dispatch"
+       "fmt"
        "log"
        "os"
        "os/exec"
+       "os/signal"
        "sync"
+       "syscall"
        "time"
+
+       "git.curoverse.com/arvados.git/sdk/go/arvados"
+       "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
+       "git.curoverse.com/arvados.git/sdk/go/dispatch"
+       arvadosVersion "git.curoverse.com/arvados.git/sdk/go/version"
 )
 
 func main() {
@@ -41,9 +51,22 @@ func doMain() error {
                "/usr/bin/crunch-run",
                "Crunch command to run container")
 
+       getVersion := flags.Bool(
+               "version",
+               false,
+               "Print version information and exit.")
+
        // Parse args; omit the first arg which is the command name
        flags.Parse(os.Args[1:])
 
+       // Print version information if requested
+       if *getVersion {
+               fmt.Printf("Version: %s\n", arvadosVersion.GetVersion())
+               os.Exit(0)
+       }
+
+       log.Printf("crunch-dispatch-local %q started", arvadosVersion.GetVersion())
+
        runningCmds = make(map[string]*exec.Cmd)
 
        arv, err := arvadosclient.MakeArvadosClient()
@@ -54,16 +77,25 @@ func doMain() error {
        arv.Retries = 25
 
        dispatcher := dispatch.Dispatcher{
-               Arv:            arv,
-               RunContainer:   run,
-               PollInterval:   time.Duration(*pollInterval) * time.Second,
-               DoneProcessing: make(chan struct{})}
+               Arv:          arv,
+               RunContainer: run,
+               PollPeriod:   time.Duration(*pollInterval) * time.Second,
+       }
 
-       err = dispatcher.RunDispatcher()
+       ctx, cancel := context.WithCancel(context.Background())
+       err = dispatcher.Run(ctx)
        if err != nil {
                return err
        }
 
+       c := make(chan os.Signal, 1)
+       signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
+       sig := <-c
+       log.Printf("Received %s, shutting down", sig)
+       signal.Stop(c)
+
+       cancel()
+
        runningCmdsMutex.Lock()
        // Finished dispatching; interrupt any crunch jobs that are still running
        for _, cmd := range runningCmds {
@@ -93,7 +125,7 @@ var startCmd = startFunc
 // crunch-run terminates, mark the container as Cancelled.
 func run(dispatcher *dispatch.Dispatcher,
        container arvados.Container,
-       status chan arvados.Container) {
+       status <-chan arvados.Container) {
 
        uuid := container.UUID
 
@@ -160,15 +192,14 @@ func run(dispatcher *dispatch.Dispatcher,
        if err != nil {
                log.Printf("Error getting final container state: %v", err)
        }
-       if container.LockedByUUID == dispatcher.Auth.UUID &&
-               (container.State == dispatch.Locked || container.State == dispatch.Running) {
+       if container.State == dispatch.Locked || container.State == dispatch.Running {
                log.Printf("After %s process termination, container state for %v is %q.  Updating it to %q",
                        *crunchRunCommand, container.State, uuid, dispatch.Cancelled)
                dispatcher.UpdateState(uuid, dispatch.Cancelled)
        }
 
        // drain any subsequent status changes
-       for _ = range status {
+       for range status {
        }
 
        log.Printf("Finalized container %v", uuid)