Skip to content

Commit

Permalink
log-and-fail to avoid repetition
Browse files Browse the repository at this point in the history
  • Loading branch information
TimoKramer committed Feb 23, 2020
1 parent d38ff6c commit b8eda9d
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 14 deletions.
25 changes: 11 additions & 14 deletions src/bob/execution/internals.clj
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
; along with Bob. If not, see <http://www.gnu.org/licenses/>.

(ns bob.execution.internals
(:require [failjure.core :as f]
(:require [clojure.string :as cs]
[failjure.core :as f]
[clj-docker-client.core :as docker]
[taoensso.timbre :as log]
[bob.util :as u]
Expand All @@ -27,34 +28,31 @@
(let [result (filter #(= (:RepoTags %) [name])
(docker/invoke states/images {:op :ImageList}))]
(if (zero? (count result))
(f/fail "Failed to find %s" name)
(u/log-and-fail "Failed to find" name)
(do (log/debugf "Found image locally: %s" name)
name))))

(defn kill-container
"Kills a running container using SIGKILL.
Returns the name or the error if any."
[name]
(let [result (docker/invoke states/containers {:op :ContainerKill :params {:id name}})]
(if (clojure.string/blank? result)
(let [result (docker/invoke states/containers {:op :ContainerKill :params {:id name}})]
(if (cs/blank? result)
name
(do (log/errorf "Error killing container %s" name)
(f/fail "Could not kill %s" name)))))
(u/log-and-fail "Could not kill" name))))

(defn pull
"Pulls in an image if it's not present locally.
Returns the name or the error if any."
[name]
(let [validate-name (fn [name]
(let [split-name (str/split name #":")]
(let [split-name (cs/split name #":")]
(if (= 2 (count split-name))
split-name
(do (log/errorf "Please provide a repository and a tag as image name: %s" split-name)
(f/fail "Please provide a repository and a tag as image name: %s" split-name)))))
(u/log-and-fail "Please provide a repository and a tag as image name:" split-name))))
pull-invoke (fn [split-name]
(let [repo (first split-name)
tag (second split-name)
_ (log/debugf "Pulling image: %s" name)
(let [{:keys [repo tag]} split-name
_ (log/debugf "Pulling image: %s" name)
result (docker/invoke states/images {:op :ImageCreate
:params {:fromImage repo
:tag tag}})]
Expand All @@ -63,8 +61,7 @@
result)))]
(if (and (f/failed? (has-image name))
(f/failed? (pull-invoke (validate-name name))))
(do (log/errorf "Could not pull image: %s" name)
(f/fail "Could not pull image: %s" name))
(u/log-and-fail "Could not pull image:" name)
(do (log/debugf "Successfully pulled image: %s" name)
name))))

Expand Down
11 changes: 11 additions & 0 deletions src/bob/util.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

(ns bob.util
(:require [ring.util.http-response :as res]
[taoensso.timbre :as log]
[failjure.core :as f]
[bob.states :as states]
[bob.pipeline.db :as db])
(:import (java.util UUID)))
Expand Down Expand Up @@ -46,3 +48,12 @@
[data run-id]
(db/upsert-log states/db {:run run-id
:content data}))

(defn log-and-fail
[& strings]
(let [errormessage (clojure.string/join " " strings)]
(do (log/errorf errormessage)
(f/fail errormessage))))

(comment
(log-and-fail "foo" :bar))
8 changes: 8 additions & 0 deletions test/bob/util_test.clj
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
[clojure.test.check.properties :as prop]
[clojure.spec.alpha :as s]
[clojure.test :refer [deftest testing is]]
[failjure.core :as f]
[bob.util :refer :all]))

(defspec respond-returns-a-ring-response
Expand Down Expand Up @@ -52,3 +53,10 @@
(let [id1 (get-id)
id2 (get-id)]
(is (not= id1 id2)))))

(deftest log-and-fail-test
(testing "variadic arguments"
(is (f/failed? (log-and-fail (gen/generate gen/string-alphanumeric)
(gen/generate gen/string-alphanumeric)
(gen/generate gen/string-alphanumeric)
(gen/generate gen/string-alphanumeric))))))

0 comments on commit b8eda9d

Please sign in to comment.