3 // Dispatcher service for Crunch that runs containers locally.
16 "git.curoverse.com/arvados.git/sdk/go/arvados"
17 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
18 "git.curoverse.com/arvados.git/sdk/go/dispatch"
29 runningCmds map[string]*exec.Cmd
30 runningCmdsMutex sync.Mutex
31 waitGroup sync.WaitGroup
32 crunchRunCommand *string
36 flags := flag.NewFlagSet("crunch-dispatch-local", flag.ExitOnError)
38 pollInterval := flags.Int(
41 "Interval in seconds to poll for queued containers")
43 crunchRunCommand = flags.String(
45 "/usr/bin/crunch-run",
46 "Crunch command to run container")
48 // Parse args; omit the first arg which is the command name
49 flags.Parse(os.Args[1:])
51 runningCmds = make(map[string]*exec.Cmd)
53 arv, err := arvadosclient.MakeArvadosClient()
55 log.Printf("Error making Arvados client: %v", err)
60 dispatcher := dispatch.Dispatcher{
63 PollPeriod: time.Duration(*pollInterval) * time.Second,
66 ctx, cancel := context.WithCancel(context.Background())
67 err = dispatcher.Run(ctx)
72 c := make(chan os.Signal, 1)
73 signal.Notify(c, os.Interrupt, syscall.SIGTERM, syscall.SIGQUIT)
75 log.Printf("Received %s, shutting down", sig)
80 runningCmdsMutex.Lock()
81 // Finished dispatching; interrupt any crunch jobs that are still running
82 for _, cmd := range runningCmds {
83 cmd.Process.Signal(os.Interrupt)
85 runningCmdsMutex.Unlock()
87 // Wait for all running crunch jobs to complete / terminate
93 func startFunc(container arvados.Container, cmd *exec.Cmd) error {
97 var startCmd = startFunc
101 // If the container is Locked, start a new crunch-run process and wait until
102 // crunch-run completes. If the priority is set to zero, set an interrupt
103 // signal to the crunch-run process.
105 // If the container is in any other state, or is not Complete/Cancelled after
106 // crunch-run terminates, mark the container as Cancelled.
107 func run(dispatcher *dispatch.Dispatcher,
108 container arvados.Container,
109 status <-chan arvados.Container) {
111 uuid := container.UUID
113 if container.State == dispatch.Locked {
116 cmd := exec.Command(*crunchRunCommand, uuid)
118 cmd.Stderr = os.Stderr
119 cmd.Stdout = os.Stderr
121 log.Printf("Starting container %v", uuid)
123 // Add this crunch job to the list of runningCmds only if we
124 // succeed in starting crunch-run.
126 runningCmdsMutex.Lock()
127 if err := startCmd(container, cmd); err != nil {
128 runningCmdsMutex.Unlock()
129 log.Printf("Error starting %v for %v: %q", *crunchRunCommand, uuid, err)
130 dispatcher.UpdateState(uuid, dispatch.Cancelled)
132 runningCmds[uuid] = cmd
133 runningCmdsMutex.Unlock()
135 // Need to wait for crunch-run to exit
136 done := make(chan struct{})
139 if _, err := cmd.Process.Wait(); err != nil {
140 log.Printf("Error while waiting for crunch job to finish for %v: %q", uuid, err)
142 log.Printf("sending done")
152 // Interrupt the child process if priority changes to 0
153 if (c.State == dispatch.Locked || c.State == dispatch.Running) && c.Priority == 0 {
154 log.Printf("Sending SIGINT to pid %d to cancel container %v", cmd.Process.Pid, uuid)
155 cmd.Process.Signal(os.Interrupt)
161 log.Printf("Finished container run for %v", uuid)
163 // Remove the crunch job from runningCmds
164 runningCmdsMutex.Lock()
165 delete(runningCmds, uuid)
166 runningCmdsMutex.Unlock()
171 // If the container is not finalized, then change it to "Cancelled".
172 err := dispatcher.Arv.Get("containers", uuid, nil, &container)
174 log.Printf("Error getting final container state: %v", err)
176 if container.State == dispatch.Locked || container.State == dispatch.Running {
177 log.Printf("After %s process termination, container state for %v is %q. Updating it to %q",
178 *crunchRunCommand, container.State, uuid, dispatch.Cancelled)
179 dispatcher.UpdateState(uuid, dispatch.Cancelled)
182 // drain any subsequent status changes
186 log.Printf("Finalized container %v", uuid)