aboutsummaryrefslogtreecommitdiff
path: root/src/clj/queue_api/db/core.clj
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 /src/clj/queue_api/db/core.clj
parent9dd6aeb32d21ed8673bdd490341302fd7f37d2fc (diff)
downloadqueue-api-ff69f08e64ad890255fb0f9e957be4e00b1c404f.tar.gz
queue-api-ff69f08e64ad890255fb0f9e957be4e00b1c404f.tar.bz2
queue-api-ff69f08e64ad890255fb0f9e957be4e00b1c404f.zip
Improves code clarity
Diffstat (limited to 'src/clj/queue_api/db/core.clj')
-rw-r--r--src/clj/queue_api/db/core.clj43
1 files changed, 26 insertions, 17 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