-
Notifications
You must be signed in to change notification settings - Fork 0
/
cache.clj
54 lines (46 loc) · 1.53 KB
/
cache.clj
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
(ns monkey.ci.cache
"Functionality for saving/restoring caches. This uses blobs."
(:require [babashka.fs :as fs]
[clojure.string :as cs]
[clojure.tools.logging :as log]
[manifold.deferred :as md]
[monkey.ci
[artifacts :as art]
[blob :as blob]
[config :as config]
[oci :as oci]
[runtime :as rt]]))
(defn cache-archive-path [{:keys [build]} id]
;; The cache archive path is the repo sid with the cache id added.
;; Build id is not used since caches are meant to supersede builds.
(str (cs/join "/" (concat (butlast (:sid build)) [id])) ".tgz"))
(def cache-config {:store-key :cache
:job-key :caches
:build-path cache-archive-path})
(defn save-caches
"If the job configured in the context uses caching, saves it according
to the cache configurations."
[rt]
(art/save-generic rt cache-config))
(defn restore-caches
[rt]
(art/restore-generic rt cache-config))
(defn wrap-caches
"Wraps fn `f` so that caches are restored/saved as configured on the job."
[f]
(fn [rt]
(md/chain
(restore-caches rt)
(fn [c]
(assoc-in rt [:job :caches] c))
f
(fn [r]
(md/chain
(save-caches rt)
(constantly r))))))
;;; Config handling
(defmethod config/normalize-key :cache [k conf]
(config/normalize-typed k conf (partial blob/normalize-blob-config k)))
(defmethod rt/setup-runtime :cache [conf k]
(when (k conf)
(blob/make-blob-store conf k)))