aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGabriel A. Giovanini <mail@gabrielgio.me>2018-02-18 21:26:07 -0300
committerGabriel A. Giovanini <mail@gabrielgio.me>2018-02-18 21:26:07 -0300
commitff69f08e64ad890255fb0f9e957be4e00b1c404f (patch)
treea2de43cc577752b6ca43bb8be0b017469dac9d89
parent9dd6aeb32d21ed8673bdd490341302fd7f37d2fc (diff)
downloadqueue-api-ff69f08e64ad890255fb0f9e957be4e00b1c404f.tar.gz
queue-api-ff69f08e64ad890255fb0f9e957be4e00b1c404f.tar.bz2
queue-api-ff69f08e64ad890255fb0f9e957be4e00b1c404f.zip
Improves code clarity
-rw-r--r--src/clj/queue_api/db/core.clj43
-rw-r--r--src/clj/queue_api/routes/services.clj6
2 files changed, 29 insertions, 20 deletions
diff --git a/src/clj/queue_api/db/core.clj b/src/clj/queue_api/db/core.clj
index 35a87fd..88e6eed 100644
--- a/src/clj/queue_api/db/core.clj
+++ b/src/clj/queue_api/db/core.clj
@@ -1,7 +1,8 @@
(ns queue-api.db.core
(:require [datascript.core :as d]
[mount.core :as mount]
- [clj-time.core :as time]))
+ [clj-time.core :as time])
+ (:import (clojure.lang Keyword)))
(def schema {:agent/id {:db/unique :db.unique/identity}
:agent/primary-skillset {:db/cardinality :db.cardinality/many}
@@ -106,7 +107,7 @@
(defn request-job
"Get the fittest job for a agent `id`."
- [id]
+ [^String id]
(let [a (d/entity @conn [:agent/id id])]
(if (not (nil? a))
(let [p (q-skillset (:agent/primary-skillset a))]
@@ -115,34 +116,42 @@
(q-skillset (:agent/secondary-skillset a)))))))
(defn t-job
- ([id s]
+ ([^String id ^Keyword s]
(d/transact! conn [{:job/id id
:job/status s}]))
- ([id s a]
+ ([^String id ^Keyword s ^String a]
(d/transact! conn [{:job/id id
:job/status s
:job/agent [:agent/id a]}])))
-(defn t-agent [id j]
- (d/transact! conn [{:agent/id id
+(defn bind-agent
+ "Bind an agent `a` to a job `j`"
+ [^String a ^String j]
+ (d/transact! conn [{:agent/id a
:agent/job [:job/id j]}]))
-(defn end-job [a]
+
+(defn end-job
+ "Change job's status to :completed from a job bound to an agent `a`"
+ [^String a]
(let [jid (-> a :agent/job :job/id)]
(if (not (nil? jid))
(t-job jid :completed))))
-(defn t-agent-job-status
- [a j]
+(defn start-job
+ "Change status of a job `j` to :processing
+ and bind it to an agent `a`"
+ [^String a ^String j]
(if (not (nil? j))
(do
(t-job j :processing a)
- (t-agent a j)
- {:job_request {:job_id j :agent_id a}})
- {:job_request {:job_id nil :agent_id a}}))
+ (bind-agent a j))))
-(defn dequeue-job [id]
- (let [j (request-job id)
- a (d/entity @conn [:agent/id id])]
+(defn dequeue-job
+ "Dequeue a job from a agent `id`"
+ [^String id]
+ (let [a (d/entity @conn [:agent/id id])]
(if (not (nil? a))
- (do (end-job a)
- (t-agent-job-status (-> a :agent/id) (-> j :job/id)))))) \ No newline at end of file
+ (let [jid (-> (request-job id) :job/id)]
+ (end-job a)
+ (start-job id jid)
+ {:job_id jid :agent_id id})))) \ 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 e8f03f9..694f0d8 100644
--- a/src/clj/queue_api/routes/services.clj
+++ b/src/clj/queue_api/routes/services.clj
@@ -44,13 +44,13 @@
(let [j (db/dequeue-job agent_id)]
(if (nil? j)
(bad-request {:message "Agent does not exist"})
- (ok j)))))
+ (ok {:job_request j})))))
(context "/queue" []
:tags ["queue"]
(GET "/" []
- :return {:completed [String]
- :processing[String]
+ :return {:completed [String]
+ :processing [String]
:unassigned [String]}
:summary "Get a summary of the queue"
(ok (db/sum-queue)))))