summary refs log tree commit diff
diff options
context:
space:
mode:
authorArun Isaac2024-10-27 02:50:26 +0000
committerArun Isaac2024-11-06 00:37:10 +0000
commit2f1fb017a9330fe5c11719a297140ec6896c3c6c (patch)
tree45b99414f726500b47a849a189de9f9558eef808
parent8c3153b1161b2b6c74ff6316e7ada76128c7fefa (diff)
downloadravanan-2f1fb017a9330fe5c11719a297140ec6896c3c6c.tar.gz
ravanan-2f1fb017a9330fe5c11719a297140ec6896c3c6c.tar.lz
ravanan-2f1fb017a9330fe5c11719a297140ec6896c3c6c.zip
slurm-api: Deduplicate API error checks.
* ravanan/slurm-api.scm (check-api-error): New function.
(submit-job, job-state): Use check-api-error.
-rw-r--r--ravanan/slurm-api.scm50
1 files changed, 25 insertions, 25 deletions
diff --git a/ravanan/slurm-api.scm b/ravanan/slurm-api.scm
index 880eadb..8e7abd7 100644
--- a/ravanan/slurm-api.scm
+++ b/ravanan/slurm-api.scm
@@ -45,6 +45,12 @@ additional @var{headers}."
                                      #:streaming? #t)))
     (json->scm body)))
 
+(define (check-api-error json)
+  "Check @var{json} API response for errors, and raise an exception if any."
+  (match (json-ref json "errors")
+    (#() json)
+    (errors (error "Slurm API error" errors))))
+
 (define (slurm-http-get api-endpoint jwt path)
   "Make a HTTP GET request to @var{path} on a slurm @var{api-endpoint}
 authenticating using @var{jwt}."
@@ -90,16 +96,13 @@ FAQ}) to request for the job."
             (if nice
                 `(("nice" . ,nice))
                 '())))
-  
-  (let ((response (slurm-http-post api-endpoint
-                                   jwt
-                                   "/slurm/v0.0.41/job/submit"
-                                   `(("jobs" . #(,job-spec))))))
-    (match (json-ref response "errors")
-      (#()
-       (json-ref response "job_id"))
-      (#(errors ...)
-       (error "Slurm API error" errors)))))
+
+  (json-ref (check-api-error
+             (slurm-http-post api-endpoint
+                              jwt
+                              "/slurm/v0.0.41/job/submit"
+                              `(("jobs" . #(,job-spec)))))
+            "job_id"))
 
 (define* (job-state job-id #:key api-endpoint jwt)
   "Query the state of slurm @var{job-id} via @var{api-endpoint}
@@ -107,18 +110,15 @@ authenticating using @var{jwt}. Return value is one of the symbols
 @code{pending}, @code{failed} and @code{completed}."
   ;; TODO: What if job has been "archived"? Then, look up archived
   ;; jobs too.
-  (let ((response (slurm-http-get api-endpoint
-                                  jwt
-                                  (string-append "/slurm/v0.0.41/job/"
-                                                 (number->string job-id)))))
-    (match (json-ref response "errors")
-      (#()
-       (match (json-ref (find (lambda (job)
-                                (= (json-ref job "job_id")
-                                   job-id))
-                              (vector->list (json-ref response "jobs")))
-                        "job_state")
-         (#(job-state)
-          (string->symbol (string-downcase job-state)))))
-      (#(errors ...)
-       (error "Slurm API error" errors)))))
+  (let ((response (check-api-error
+                   (slurm-http-get api-endpoint
+                                   jwt
+                                   (string-append "/slurm/v0.0.41/job/"
+                                                  (number->string job-id))))))
+    (match (json-ref (find (lambda (job)
+                             (= (json-ref job "job_id")
+                                job-id))
+                           (vector->list (json-ref response "jobs")))
+                     "job_state")
+      (#(job-state)
+       (string->symbol (string-downcase job-state))))))