-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
55 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,53 @@ | ||
(ns nl.surf.eduhub.validator.service.jobs.worker | ||
"Functions called called by a worker thread running in the background." | ||
(:require [clojure.tools.logging :as log] | ||
[nl.surf.eduhub.validator.service.jobs.status :as status] | ||
[nl.surf.eduhub.validator.service.validate :as validate])) | ||
[nl.surf.eduhub.validator.service.validate :as validate] | ||
[nl.jomco.resources :refer [closeable]] | ||
[goose.worker])) | ||
|
||
;; A worker thread running in the background | ||
;; Called by the workers. Runs the validate-endpoint function | ||
;;;; middleware / config code | ||
|
||
(def ^:dynamic *config* | ||
"The current system configuration as set by `wrap-worker-config`." | ||
nil) | ||
|
||
(defn wrap-worker-config | ||
[config] | ||
(fn [next] | ||
(fn [opts job] | ||
(binding [*config* config] | ||
(next opts job))))) | ||
|
||
(defn mk-worker | ||
"Configure and start a goose worker resource. This will start | ||
multiple background threads and can be stopped by calling | ||
`nl.jomco.resources/close` | ||
Ensures that worker functions are called with `*config*` bound to | ||
the system configuration that was used to start the worker | ||
resource." | ||
[{:keys [goose-worker-opts] :as config}] | ||
(-> goose-worker-opts | ||
(assoc :middlewares (wrap-worker-config config)) | ||
goose.worker/start | ||
(closeable goose.worker/stop))) | ||
|
||
;;;; Actual background job functions | ||
|
||
;; Runs the validate-endpoint function | ||
;; and updates the values in the job status. | ||
;; opts should contain: basic-auth ooapi-version base-url profile | ||
(defn validate-endpoint [endpoint-id uuid {:keys [config] :as opts}] | ||
(let [{:keys [redis-conn expiry-seconds]} config] | ||
|
||
(defn validate-endpoint | ||
[endpoint-id uuid opts] | ||
(let [{:keys [redis-conn expiry-seconds]} *config*] | ||
(assert redis-conn) | ||
(try | ||
(let [html (validate/validate-endpoint endpoint-id opts)] | ||
;; assuming everything went ok, save html in status, update status and set expiry to value configured in ENV | ||
(status/set-status-fields redis-conn uuid "finished" {"html-report" html} expiry-seconds)) | ||
(catch Throwable ex | ||
(catch Exception ex | ||
;; otherwise set status to error, include error message and also set expiry | ||
(log/error ex "Validate endpoint threw an exception") | ||
(status/set-status-fields redis-conn uuid "failed" {"error" (str ex)} expiry-seconds))))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters