From ff69f08e64ad890255fb0f9e957be4e00b1c404f Mon Sep 17 00:00:00 2001 From: "Gabriel A. Giovanini" Date: Sun, 18 Feb 2018 21:26:07 -0300 Subject: Improves code clarity --- src/clj/queue_api/db/core.clj | 43 ++++++++++++++++++++++++++----------------- 1 file changed, 26 insertions(+), 17 deletions(-) (limited to 'src/clj/queue_api/db/core.clj') 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 -- cgit v1.2.3