11100: Adjust delete_at in before_validation hook instead of validation. Permit chang...
[arvados.git] / services / crunch-dispatch-slurm / crunch-dispatch-slurm.go
index 617b076da281a982b81f24ae9b7b7fe4d3897aee..d84d461c30184e12b45cd87611580d03004a343b 100644 (file)
@@ -11,6 +11,7 @@ import (
        "math"
        "os"
        "os/exec"
+       "regexp"
        "strings"
        "time"
 
@@ -122,9 +123,30 @@ func doMain() error {
                log.Printf("Error notifying init daemon: %v", err)
        }
 
+       go checkSqueueForOrphans(dispatcher, sqCheck)
+
        return dispatcher.Run(context.Background())
 }
 
+var containerUuidPattern = regexp.MustCompile(`^[a-z0-9]{5}-dz642-[a-z0-9]{15}$`)
+
+// Check the next squeue report, and invoke TrackContainer for all the
+// containers in the report. This gives us a chance to cancel slurm
+// jobs started by a previous dispatch process that never released
+// their slurm allocations even though their container states are
+// Cancelled or Complete. See https://dev.arvados.org/issues/10979
+func checkSqueueForOrphans(dispatcher *dispatch.Dispatcher, sqCheck *SqueueChecker) {
+       for _, uuid := range sqCheck.All() {
+               if !containerUuidPattern.MatchString(uuid) {
+                       continue
+               }
+               err := dispatcher.TrackContainer(uuid)
+               if err != nil {
+                       log.Printf("checkSqueueForOrphans: TrackContainer(%s): %s", uuid, err)
+               }
+       }
+}
+
 // sbatchCmd
 func sbatchFunc(container arvados.Container) *exec.Cmd {
        memPerCPU := math.Ceil(float64(container.RuntimeConstraints.RAM) / (float64(container.RuntimeConstraints.VCPUs) * 1048576))