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"
33 runningCmds map[string]*exec.Cmd
34 runningCmdsMutex sync.Mutex
35 waitGroup sync.WaitGroup
36 crunchRunCommand *string
40 flags := flag.NewFlagSet("crunch-dispatch-local", flag.ExitOnError)
42 pollInterval := flags.Int(
45 "Interval in seconds to poll for queued containers")
47 crunchRunCommand = flags.String(
49 "/usr/bin/crunch-run",
50 "Crunch command to run container")
52 // Parse args; omit the first arg which is the command name
53 flags.Parse(os.Args[1:])
55 runningCmds = make(map[string]*exec.Cmd)
57 arv, err := arvadosclient.MakeArvadosClient()
59 log.Printf("Error making Arvados client: %v", err)
64 dispatcher := dispatch.Dispatcher{
67 PollPeriod: time.Duration(*pollInterval) * time.Second,
70 ctx, cancel := context.WithCancel(context.Background())
71 err = dispatcher.Run(ctx)
76 c := make(chan os.Signal, 1)
77 signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
79 log.Printf("Received %s, shutting down", sig)
84 runningCmdsMutex.Lock()
85 // Finished dispatching; interrupt any crunch jobs that are still running
86 for _, cmd := range runningCmds {
87 cmd.Process.Signal(os.Interrupt)
89 runningCmdsMutex.Unlock()
91 // Wait for all running crunch jobs to complete / terminate
97 func startFunc(container arvados.Container, cmd *exec.Cmd) error {
101 var startCmd = startFunc
105 // If the container is Locked, start a new crunch-run process and wait until
106 // crunch-run completes. If the priority is set to zero, set an interrupt
107 // signal to the crunch-run process.
109 // If the container is in any other state, or is not Complete/Cancelled after
110 // crunch-run terminates, mark the container as Cancelled.
111 func run(dispatcher *dispatch.Dispatcher,
112 container arvados.Container,
113 status <-chan arvados.Container) {
115 uuid := container.UUID
117 if container.State == dispatch.Locked {
120 cmd := exec.Command(*crunchRunCommand, uuid)
122 cmd.Stderr = os.Stderr
123 cmd.Stdout = os.Stderr
125 log.Printf("Starting container %v", uuid)
127 // Add this crunch job to the list of runningCmds only if we
128 // succeed in starting crunch-run.
130 runningCmdsMutex.Lock()
131 if err := startCmd(container, cmd); err != nil {
132 runningCmdsMutex.Unlock()
133 log.Printf("Error starting %v for %v: %q", *crunchRunCommand, uuid, err)
134 dispatcher.UpdateState(uuid, dispatch.Cancelled)
136 runningCmds[uuid] = cmd
137 runningCmdsMutex.Unlock()
139 // Need to wait for crunch-run to exit
140 done := make(chan struct{})
143 if _, err := cmd.Process.Wait(); err != nil {
144 log.Printf("Error while waiting for crunch job to finish for %v: %q", uuid, err)
146 log.Printf("sending done")
156 // Interrupt the child process if priority changes to 0
157 if (c.State == dispatch.Locked || c.State == dispatch.Running) && c.Priority == 0 {
158 log.Printf("Sending SIGINT to pid %d to cancel container %v", cmd.Process.Pid, uuid)
159 cmd.Process.Signal(os.Interrupt)
165 log.Printf("Finished container run for %v", uuid)
167 // Remove the crunch job from runningCmds
168 runningCmdsMutex.Lock()
169 delete(runningCmds, uuid)
170 runningCmdsMutex.Unlock()
175 // If the container is not finalized, then change it to "Cancelled".
176 err := dispatcher.Arv.Get("containers", uuid, nil, &container)
178 log.Printf("Error getting final container state: %v", err)
180 if container.State == dispatch.Locked || container.State == dispatch.Running {
181 log.Printf("After %s process termination, container state for %v is %q. Updating it to %q",
182 *crunchRunCommand, container.State, uuid, dispatch.Cancelled)
183 dispatcher.UpdateState(uuid, dispatch.Cancelled)
186 // drain any subsequent status changes
190 log.Printf("Finalized container %v", uuid)