6 "git.curoverse.com/arvados.git/sdk/go/arvadosclient"
25 arv arvadosclient.ArvadosClient
26 runningCmds map[string]*exec.Cmd
27 runningCmdsMutex sync.Mutex
28 waitGroup sync.WaitGroup
29 doneProcessing chan bool
30 sigChan chan os.Signal
34 flags := flag.NewFlagSet("crunch-dispatch-slurm", flag.ExitOnError)
36 pollInterval := flags.Int(
39 "Interval in seconds to poll for queued containers")
41 priorityPollInterval := flags.Int(
42 "container-priority-poll-interval",
44 "Interval in seconds to check priority of a dispatched container")
46 crunchRunCommand := flags.String(
48 "/usr/bin/crunch-run",
49 "Crunch command to run container")
51 finishCommand := flags.String(
53 "/usr/bin/crunch-finish-slurm.sh",
54 "Command to run from strigger when job is finished")
56 // Parse args; omit the first arg which is the command name
57 flags.Parse(os.Args[1:])
60 arv, err = arvadosclient.MakeArvadosClient()
65 // Channel to terminate
66 doneProcessing = make(chan bool)
69 sigChan = make(chan os.Signal, 1)
70 signal.Notify(sigChan, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT)
71 go func(sig <-chan os.Signal) {
72 for sig := range sig {
73 log.Printf("Caught signal: %v", sig)
74 doneProcessing <- true
78 // Run all queued containers
79 runQueuedContainers(*pollInterval, *priorityPollInterval, *crunchRunCommand, *finishCommand)
81 // Wait for all running crunch jobs to complete / terminate
87 // Poll for queued containers using pollInterval.
88 // Invoke dispatchSlurm for each ticker cycle, which will run all the queued containers.
90 // Any errors encountered are logged but the program would continue to run (not exit).
91 // This is because, once one or more crunch jobs are running,
92 // we would need to wait for them complete.
93 func runQueuedContainers(pollInterval, priorityPollInterval int, crunchRunCommand, finishCommand string) {
94 ticker := time.NewTicker(time.Duration(pollInterval) * time.Second)
99 dispatchSlurm(priorityPollInterval, crunchRunCommand, finishCommand)
100 case <-doneProcessing:
108 type Container struct {
109 UUID string `json:"uuid"`
110 State string `json:"state"`
111 Priority int `json:"priority"`
114 // ContainerList is a list of the containers from api
115 type ContainerList struct {
116 Items []Container `json:"items"`
119 // Get the list of queued containers from API server and invoke run for each container.
120 func dispatchSlurm(priorityPollInterval int, crunchRunCommand, finishCommand string) {
121 params := arvadosclient.Dict{
122 "filters": [][]string{[]string{"state", "=", "Queued"}},
125 var containers ContainerList
126 err := arv.List("containers", params, &containers)
128 log.Printf("Error getting list of queued containers: %q", err)
132 for i := 0; i < len(containers.Items); i++ {
133 log.Printf("About to submit queued container %v", containers.Items[i].UUID)
135 go run(containers.Items[i], crunchRunCommand, finishCommand, priorityPollInterval)
140 func sbatchFunc(uuid string) *exec.Cmd {
141 return exec.Command("sbatch", "--job-name="+uuid, "--share", "--parsable")
144 var sbatchCmd = sbatchFunc
147 func striggerFunc(jobid, containerUUID, finishCommand, apiHost, apiToken, apiInsecure string) *exec.Cmd {
148 return exec.Command("strigger", "--set", "--jobid="+jobid, "--fini",
149 fmt.Sprintf("--program=%s %s %s %s %s", finishCommand, apiHost, apiToken, apiInsecure, containerUUID))
152 var striggerCmd = striggerFunc
154 // Submit job to slurm using sbatch.
155 func submit(container Container, crunchRunCommand string) (jobid string, submitErr error) {
158 // Mark record as complete if anything errors out.
160 if submitErr != nil {
161 // This really should be an "Error" state, see #8018
162 updateErr := arv.Update("containers", container.UUID,
164 "container": arvadosclient.Dict{"state": "Complete"}},
166 if updateErr != nil {
167 log.Printf("Error updating container state to 'Complete' for %v: %q", container.UUID, updateErr)
172 // Create the command and attach to stdin/stdout
173 cmd := sbatchCmd(container.UUID)
174 stdinWriter, stdinerr := cmd.StdinPipe()
176 submitErr = fmt.Errorf("Error creating stdin pipe %v: %q", container.UUID, stdinerr)
180 stdoutReader, stdoutErr := cmd.StdoutPipe()
181 if stdoutErr != nil {
182 submitErr = fmt.Errorf("Error creating stdout pipe %v: %q", container.UUID, stdoutErr)
186 stderrReader, stderrErr := cmd.StderrPipe()
187 if stderrErr != nil {
188 submitErr = fmt.Errorf("Error creating stderr pipe %v: %q", container.UUID, stderrErr)
194 submitErr = fmt.Errorf("Error starting %v: %v", cmd.Args, err)
198 stdoutChan := make(chan []byte)
200 b, _ := ioutil.ReadAll(stdoutReader)
205 stderrChan := make(chan []byte)
207 b, _ := ioutil.ReadAll(stderrReader)
212 // Send a tiny script on stdin to execute the crunch-run command
213 // slurm actually enforces that this must be a #! script
214 fmt.Fprintf(stdinWriter, "#!/bin/sh\nexec '%s' '%s'\n", crunchRunCommand, container.UUID)
219 stdoutMsg := <-stdoutChan
220 stderrmsg := <-stderrChan
223 submitErr = fmt.Errorf("Container submission failed %v: %v %v", cmd.Args, err, stderrmsg)
227 // If everything worked out, got the jobid on stdout
228 jobid = string(stdoutMsg)
233 // finalizeRecordOnFinish uses 'strigger' command to register a script that will run on
234 // the slurm controller when the job finishes.
235 func finalizeRecordOnFinish(jobid, containerUUID, finishCommand, apiHost, apiToken, apiInsecure string) {
236 cmd := striggerCmd(jobid, containerUUID, finishCommand, apiHost, apiToken, apiInsecure)
237 cmd.Stdout = os.Stdout
238 cmd.Stderr = os.Stderr
241 log.Printf("While setting up strigger: %v", err)
245 // Run a queued container.
246 // Set container state to locked (TBD)
247 // Submit job to slurm to execute crunch-run command for the container
248 // If the container priority becomes zero while crunch job is still running, cancel the job.
249 func run(container Container, crunchRunCommand, finishCommand string, priorityPollInterval int) {
251 jobid, err := submit(container, crunchRunCommand)
253 log.Printf("Error queuing container run: %v", err)
261 finalizeRecordOnFinish(jobid, container.UUID, finishCommand, arv.ApiServer, arv.ApiToken, insecure)
263 // Update container status to Running, this is a temporary workaround
264 // to avoid resubmitting queued containers because record locking isn't
266 err = arv.Update("containers", container.UUID,
268 "container": arvadosclient.Dict{"state": "Running"}},
271 log.Printf("Error updating container state to 'Running' for %v: %q", container.UUID, err)
274 log.Printf("Submitted container run for %v", container.UUID)
276 containerUUID := container.UUID
278 // A goroutine to terminate the runner if container priority becomes zero
279 priorityTicker := time.NewTicker(time.Duration(priorityPollInterval) * time.Second)
281 for _ = range priorityTicker.C {
282 var container Container
283 err := arv.Get("containers", containerUUID, nil, &container)
285 log.Printf("Error getting container info for %v: %q", container.UUID, err)
287 if container.Priority == 0 {
288 log.Printf("Canceling container %v", container.UUID)
289 priorityTicker.Stop()
290 cancelcmd := exec.Command("scancel", "--name="+container.UUID)
293 if container.State == "Complete" {
294 priorityTicker.Stop()