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.
21 "git.curoverse.com/arvados.git/sdk/go/arvados"
22 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
23 "git.curoverse.com/arvados.git/sdk/go/dispatch"
36 runningCmds map[string]*exec.Cmd
37 runningCmdsMutex sync.Mutex
38 waitGroup sync.WaitGroup
39 crunchRunCommand *string
43 flags := flag.NewFlagSet("crunch-dispatch-local", flag.ExitOnError)
45 pollInterval := flags.Int(
48 "Interval in seconds to poll for queued containers")
50 crunchRunCommand = flags.String(
52 "/usr/bin/crunch-run",
53 "Crunch command to run container")
55 getVersion := flags.Bool(
58 "Print version information and exit.")
60 // Parse args; omit the first arg which is the command name
61 flags.Parse(os.Args[1:])
63 // Print version information if requested
65 fmt.Printf("crunch-dispatch-local %s\n", version)
69 log.Printf("crunch-dispatch-local %s started", version)
71 runningCmds = make(map[string]*exec.Cmd)
73 arv, err := arvadosclient.MakeArvadosClient()
75 log.Printf("Error making Arvados client: %v", err)
80 dispatcher := dispatch.Dispatcher{
83 PollPeriod: time.Duration(*pollInterval) * time.Second,
86 ctx, cancel := context.WithCancel(context.Background())
87 err = dispatcher.Run(ctx)
92 c := make(chan os.Signal, 1)
93 signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
95 log.Printf("Received %s, shutting down", sig)
100 runningCmdsMutex.Lock()
101 // Finished dispatching; interrupt any crunch jobs that are still running
102 for _, cmd := range runningCmds {
103 cmd.Process.Signal(os.Interrupt)
105 runningCmdsMutex.Unlock()
107 // Wait for all running crunch jobs to complete / terminate
113 func startFunc(container arvados.Container, cmd *exec.Cmd) error {
117 var startCmd = startFunc
121 // If the container is Locked, start a new crunch-run process and wait until
122 // crunch-run completes. If the priority is set to zero, set an interrupt
123 // signal to the crunch-run process.
125 // If the container is in any other state, or is not Complete/Cancelled after
126 // crunch-run terminates, mark the container as Cancelled.
127 func run(dispatcher *dispatch.Dispatcher,
128 container arvados.Container,
129 status <-chan arvados.Container) {
131 uuid := container.UUID
133 if container.State == dispatch.Locked {
136 cmd := exec.Command(*crunchRunCommand, uuid)
138 cmd.Stderr = os.Stderr
139 cmd.Stdout = os.Stderr
141 log.Printf("Starting container %v", uuid)
143 // Add this crunch job to the list of runningCmds only if we
144 // succeed in starting crunch-run.
146 runningCmdsMutex.Lock()
147 if err := startCmd(container, cmd); err != nil {
148 runningCmdsMutex.Unlock()
149 log.Printf("Error starting %v for %v: %q", *crunchRunCommand, uuid, err)
150 dispatcher.UpdateState(uuid, dispatch.Cancelled)
152 runningCmds[uuid] = cmd
153 runningCmdsMutex.Unlock()
155 // Need to wait for crunch-run to exit
156 done := make(chan struct{})
159 if _, err := cmd.Process.Wait(); err != nil {
160 log.Printf("Error while waiting for crunch job to finish for %v: %q", uuid, err)
162 log.Printf("sending done")
172 // Interrupt the child process if priority changes to 0
173 if (c.State == dispatch.Locked || c.State == dispatch.Running) && c.Priority == 0 {
174 log.Printf("Sending SIGINT to pid %d to cancel container %v", cmd.Process.Pid, uuid)
175 cmd.Process.Signal(os.Interrupt)
181 log.Printf("Finished container run for %v", uuid)
183 // Remove the crunch job from runningCmds
184 runningCmdsMutex.Lock()
185 delete(runningCmds, uuid)
186 runningCmdsMutex.Unlock()
191 // If the container is not finalized, then change it to "Cancelled".
192 err := dispatcher.Arv.Get("containers", uuid, nil, &container)
194 log.Printf("Error getting final container state: %v", err)
196 if container.State == dispatch.Locked || container.State == dispatch.Running {
197 log.Printf("After %s process termination, container state for %v is %q. Updating it to %q",
198 *crunchRunCommand, container.State, uuid, dispatch.Cancelled)
199 dispatcher.UpdateState(uuid, dispatch.Cancelled)
202 // drain any subsequent status changes
206 log.Printf("Finalized container %v", uuid)