Skip to content

Commit

Permalink
Pass worker config to handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
joodie committed Jan 28, 2025
1 parent fd2ef2b commit 2334362
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 20 deletions.
45 changes: 39 additions & 6 deletions src/nl/surf/eduhub/validator/service/jobs/worker.clj
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)))))
10 changes: 4 additions & 6 deletions src/nl/surf/eduhub/validator/service/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
(ns nl.surf.eduhub.validator.service.main
(:gen-class)
(:require [environ.core :refer [env]]
[goose.worker :as w]
[clojure.tools.logging :as log]
[nl.jomco.resources :refer [mk-system closeable with-resources wait-until-interrupted Resource]]
[nl.jomco.resources :refer [mk-system with-resources wait-until-interrupted Resource]]
[nl.surf.eduhub.validator.service.jobs.worker :as jobs.worker]
[nl.surf.eduhub.validator.service.redis-check :refer [check-redis-connection]]
[nl.surf.eduhub.validator.service.api :as api]
[nl.surf.eduhub.validator.service.config :as config]
Expand All @@ -34,9 +34,8 @@
(.stop server)))

(defn run-system
[{:keys [server-port goose-worker-opts] :as config}]
(mk-system [worker (-> (w/start goose-worker-opts)
(closeable w/stop))
[{:keys [server-port] :as config}]
(mk-system [worker (jobs.worker/mk-worker config)
web-app (api/compose-app config true)
jetty (run-jetty web-app
{:port server-port
Expand All @@ -45,7 +44,6 @@
:web-app web-app
:jetty jetty}))


(defn -main [& _]
(let [config (config/validate-and-load-config env)]
(try (check-redis-connection config)
Expand Down
16 changes: 9 additions & 7 deletions src/nl/surf/eduhub/validator/service/validate.clj
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,15 @@
"Performs a synchronous validation via the eduhub-validator"
[endpoint-id {:keys [gateway-url gateway-basic-auth ooapi-version] :as _config}]
{:pre [gateway-url]}
(let [url (str gateway-url (if (.endsWith gateway-url "/") "" "/") "courses")
opts {:headers {"x-route" (str "endpoint=" endpoint-id)
"accept" (str "application/json; version=" ooapi-version)
"x-envelope-response" "true"}
:basic-auth gateway-basic-auth
:throw false}]
(http/get url opts)))
(let [url (str gateway-url (if (.endsWith gateway-url "/") "" "/") "courses")
opts {:headers {"x-route" (str "endpoint=" endpoint-id)
"accept" (str "application/json; version=" ooapi-version)
"x-envelope-response" "true"}
:basic-auth gateway-basic-auth
:throw false}
response (http/get url opts)]
(log/info (str (:status response) " :get " url opts))
response))

;; Uses the ooapi validator to validate an endpoint.
;; Returns the generated HTML report.
Expand Down
4 changes: 3 additions & 1 deletion test/nl/surf/eduhub/validator/service/jobs/client_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
[goose.client :as c]
[nl.jomco.http-status-codes :as http-status]
[nl.surf.eduhub.validator.service.jobs.status :as status]
[nl.surf.eduhub.validator.service.jobs.worker :as worker]
[nl.surf.eduhub.validator.service.api :as api]
[nl.surf.eduhub.validator.service.config :as config]
[nl.surf.eduhub.validator.service.config-test :as config-test]
Expand Down Expand Up @@ -72,7 +73,8 @@
(with-redefs [http/request (fn wrap-vcr [req] (vcr req))]
;; run worker
(let [[fname & args] (pop-queue! jobs-atom)]
(apply (resolve fname) (assoc-in (vec args) [2 :config] test-config)))
(binding [worker/*config* test-config]
(apply (resolve fname) args)))

(let [body (-> (make-status-call uuid)
(test-helper/validate-timestamp :pending-at)
Expand Down

0 comments on commit 2334362

Please sign in to comment.