3 // Dispatcher service for Crunch that runs containers locally.
7 "git.curoverse.com/arvados.git/sdk/go/arvados"
8 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
9 "git.curoverse.com/arvados.git/sdk/go/dispatch"
25 runningCmds map[string]*exec.Cmd
26 runningCmdsMutex sync.Mutex
27 waitGroup sync.WaitGroup
28 crunchRunCommand *string
32 flags := flag.NewFlagSet("crunch-dispatch-local", flag.ExitOnError)
34 pollInterval := flags.Int(
37 "Interval in seconds to poll for queued containers")
39 crunchRunCommand = flags.String(
41 "/usr/bin/crunch-run",
42 "Crunch command to run container")
44 // Parse args; omit the first arg which is the command name
45 flags.Parse(os.Args[1:])
47 runningCmds = make(map[string]*exec.Cmd)
49 arv, err := arvadosclient.MakeArvadosClient()
51 log.Printf("Error making Arvados client: %v", err)
56 dispatcher := dispatch.Dispatcher{
59 PollInterval: time.Duration(*pollInterval) * time.Second,
60 DoneProcessing: make(chan struct{})}
62 err = dispatcher.RunDispatcher()
67 runningCmdsMutex.Lock()
68 // Finished dispatching; interrupt any crunch jobs that are still running
69 for _, cmd := range runningCmds {
70 cmd.Process.Signal(os.Interrupt)
72 runningCmdsMutex.Unlock()
74 // Wait for all running crunch jobs to complete / terminate
80 func startFunc(container arvados.Container, cmd *exec.Cmd) error {
84 var startCmd = startFunc
88 // If the container is Locked, start a new crunch-run process and wait until
89 // crunch-run completes. If the priority is set to zero, set an interrupt
90 // signal to the crunch-run process.
92 // If the container is in any other state, or is not Complete/Cancelled after
93 // crunch-run terminates, mark the container as Cancelled.
94 func run(dispatcher *dispatch.Dispatcher,
95 container arvados.Container,
96 status chan arvados.Container) {
98 uuid := container.UUID
100 if container.State == dispatch.Locked {
103 cmd := exec.Command(*crunchRunCommand, uuid)
105 cmd.Stderr = os.Stderr
106 cmd.Stdout = os.Stderr
108 log.Printf("Starting container %v", uuid)
110 // Add this crunch job to the list of runningCmds only if we
111 // succeed in starting crunch-run.
113 runningCmdsMutex.Lock()
114 if err := startCmd(container, cmd); err != nil {
115 runningCmdsMutex.Unlock()
116 log.Printf("Error starting %v for %v: %q", *crunchRunCommand, uuid, err)
117 dispatcher.UpdateState(uuid, dispatch.Cancelled)
119 runningCmds[uuid] = cmd
120 runningCmdsMutex.Unlock()
122 // Need to wait for crunch-run to exit
123 done := make(chan struct{})
126 if _, err := cmd.Process.Wait(); err != nil {
127 log.Printf("Error while waiting for crunch job to finish for %v: %q", uuid, err)
129 log.Printf("sending done")
139 // Interrupt the child process if priority changes to 0
140 if (c.State == dispatch.Locked || c.State == dispatch.Running) && c.Priority == 0 {
141 log.Printf("Sending SIGINT to pid %d to cancel container %v", cmd.Process.Pid, uuid)
142 cmd.Process.Signal(os.Interrupt)
148 log.Printf("Finished container run for %v", uuid)
150 // Remove the crunch job from runningCmds
151 runningCmdsMutex.Lock()
152 delete(runningCmds, uuid)
153 runningCmdsMutex.Unlock()
158 // If the container is not finalized, then change it to "Cancelled".
159 err := dispatcher.Arv.Get("containers", uuid, nil, &container)
161 log.Printf("Error getting final container state: %v", err)
163 if container.LockedByUUID == dispatcher.Auth.UUID &&
164 (container.State == dispatch.Locked || container.State == dispatch.Running) {
165 log.Printf("After %s process termination, container state for %v is %q. Updating it to %q",
166 *crunchRunCommand, container.State, uuid, dispatch.Cancelled)
167 dispatcher.UpdateState(uuid, dispatch.Cancelled)
170 // drain any subsequent status changes
174 log.Printf("Finalized container %v", uuid)