From de7cd8220c364dd816f4136a9d4c5a4d6618359d Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Thu, 8 Mar 2018 23:06:12 -0300 Subject: Better routing and mutex --- src/clj/queue_api/db/core.clj | 9 +++++---- src/clj/queue_api/routes/services.clj | 31 +++++++++++++++++-------------- 2 files changed, 22 insertions(+), 18 deletions(-) (limited to 'src/clj') diff --git a/src/clj/queue_api/db/core.clj b/src/clj/queue_api/db/core.clj index adbda25..0ad9b81 100644 --- a/src/clj/queue_api/db/core.clj +++ b/src/clj/queue_api/db/core.clj @@ -156,7 +156,8 @@ [^String id] (let [a (d/entity @conn [:agent/id id])] (if-not (nil? a) - (let [jid (:job/id (fittest-job id))] - (end-job a) - (start-job id jid) - jid)))) \ No newline at end of file + (locking (Object.) + (let [jid (:job/id (fittest-job id))] + (end-job a) + (start-job id jid) + jid))))) \ No newline at end of file diff --git a/src/clj/queue_api/routes/services.clj b/src/clj/queue_api/routes/services.clj index 9b8cc91..01937c1 100644 --- a/src/clj/queue_api/routes/services.clj +++ b/src/clj/queue_api/routes/services.clj @@ -8,9 +8,9 @@ (s/defschema new-agent - {:id s/Str - :name s/Str - :primary_skillset [s/Str] + {:id s/Str + :name s/Str + :primary_skillset [s/Str] :secondary_skillset [s/Str]}) (s/defschema new-job @@ -55,21 +55,24 @@ (if (nil? (db/agent (:id body))) (do (db/add-agent body) {:status 201}) - (bad-request {:message "Agent id already exists"})))} - :post {:summary "Get summary of an agent" - :parameters {:body-params agent-id} + (bad-request {:message "Agent id already exists"})))}})) + + (context "/agent/:id" [] + (resource + {:tags ["agent"] + :get {:summary "Get summary of an agent" + :parameters {:path-params {:id s/Str}} :responses {http-status/ok {:schema agent-sum :description "Fetched correctly"} http-status/not-found {:schema error-message :description "Agent not found"}} - :handler (fn [{body :body-params}] - (let [a (:agent_id body)] - (if-not (nil? (db/agent a)) - (let [jobs (db/sum-agent a)] - (ok (map (fn [x] - {:type (first x) - :jobs (last x)}) jobs))) - (not-found {:message "Agent does not exist"}))))}})) + :handler (fn [{{:keys [id]} :path-params}] + (if-not (nil? (db/agent id)) + (let [jobs (db/sum-agent id)] + (ok (map (fn [x] + {:type (first x) + :jobs (last x)}) jobs))) + (not-found {:message "Agent does not exist"})))}})) (context "/job" [] -- cgit v1.2.3