1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
7 // Dispatcher service for Crunch that runs containers locally.
20 "git.curoverse.com/arvados.git/sdk/go/arvados"
21 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
22 "git.curoverse.com/arvados.git/sdk/go/dispatch"
23 "github.com/sirupsen/logrus"
31 logrus.Fatalf("%q", err)
36 runningCmds map[string]*exec.Cmd
37 runningCmdsMutex sync.Mutex
38 waitGroup sync.WaitGroup
39 crunchRunCommand *string
43 logger := logrus.StandardLogger()
44 if os.Getenv("DEBUG") != "" {
45 logger.SetLevel(logrus.DebugLevel)
47 logger.Formatter = &logrus.JSONFormatter{
48 TimestampFormat: "2006-01-02T15:04:05.000000000Z07:00",
51 flags := flag.NewFlagSet("crunch-dispatch-local", flag.ExitOnError)
53 pollInterval := flags.Int(
56 "Interval in seconds to poll for queued containers")
58 crunchRunCommand = flags.String(
60 "/usr/bin/crunch-run",
61 "Crunch command to run container")
63 getVersion := flags.Bool(
66 "Print version information and exit.")
68 // Parse args; omit the first arg which is the command name
69 flags.Parse(os.Args[1:])
71 // Print version information if requested
73 fmt.Printf("crunch-dispatch-local %s\n", version)
77 logger.Printf("crunch-dispatch-local %s started", version)
79 runningCmds = make(map[string]*exec.Cmd)
81 arv, err := arvadosclient.MakeArvadosClient()
83 logger.Errorf("error making Arvados client: %v", err)
88 dispatcher := dispatch.Dispatcher{
92 PollPeriod: time.Duration(*pollInterval) * time.Second,
95 ctx, cancel := context.WithCancel(context.Background())
96 err = dispatcher.Run(ctx)
101 c := make(chan os.Signal, 1)
102 signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
104 logger.Printf("Received %s, shutting down", sig)
109 runningCmdsMutex.Lock()
110 // Finished dispatching; interrupt any crunch jobs that are still running
111 for _, cmd := range runningCmds {
112 cmd.Process.Signal(os.Interrupt)
114 runningCmdsMutex.Unlock()
116 // Wait for all running crunch jobs to complete / terminate
122 func startFunc(container arvados.Container, cmd *exec.Cmd) error {
126 var startCmd = startFunc
130 // If the container is Locked, start a new crunch-run process and wait until
131 // crunch-run completes. If the priority is set to zero, set an interrupt
132 // signal to the crunch-run process.
134 // If the container is in any other state, or is not Complete/Cancelled after
135 // crunch-run terminates, mark the container as Cancelled.
136 func run(dispatcher *dispatch.Dispatcher,
137 container arvados.Container,
138 status <-chan arvados.Container) {
140 uuid := container.UUID
142 if container.State == dispatch.Locked {
145 cmd := exec.Command(*crunchRunCommand, uuid)
147 cmd.Stderr = os.Stderr
148 cmd.Stdout = os.Stderr
150 dispatcher.Logger.Printf("starting container %v", uuid)
152 // Add this crunch job to the list of runningCmds only if we
153 // succeed in starting crunch-run.
155 runningCmdsMutex.Lock()
156 if err := startCmd(container, cmd); err != nil {
157 runningCmdsMutex.Unlock()
158 dispatcher.Logger.Warnf("error starting %q for %s: %s", *crunchRunCommand, uuid, err)
159 dispatcher.UpdateState(uuid, dispatch.Cancelled)
161 runningCmds[uuid] = cmd
162 runningCmdsMutex.Unlock()
164 // Need to wait for crunch-run to exit
165 done := make(chan struct{})
168 if _, err := cmd.Process.Wait(); err != nil {
169 dispatcher.Logger.Warnf("error while waiting for crunch job to finish for %v: %q", uuid, err)
171 dispatcher.Logger.Debugf("sending done")
181 // Interrupt the child process if priority changes to 0
182 if (c.State == dispatch.Locked || c.State == dispatch.Running) && c.Priority == 0 {
183 dispatcher.Logger.Printf("sending SIGINT to pid %d to cancel container %v", cmd.Process.Pid, uuid)
184 cmd.Process.Signal(os.Interrupt)
190 dispatcher.Logger.Printf("finished container run for %v", uuid)
192 // Remove the crunch job from runningCmds
193 runningCmdsMutex.Lock()
194 delete(runningCmds, uuid)
195 runningCmdsMutex.Unlock()
200 // If the container is not finalized, then change it to "Cancelled".
201 err := dispatcher.Arv.Get("containers", uuid, nil, &container)
203 dispatcher.Logger.Warnf("error getting final container state: %v", err)
205 if container.State == dispatch.Locked || container.State == dispatch.Running {
206 dispatcher.Logger.Warnf("after %q process termination, container state for %v is %q; updating it to %q",
207 *crunchRunCommand, uuid, container.State, dispatch.Cancelled)
208 dispatcher.UpdateState(uuid, dispatch.Cancelled)
211 // drain any subsequent status changes
215 dispatcher.Logger.Printf("finalized container %v", uuid)