Back
halt-key! (clj)
(source)multimethod
(halt-key! key value)
Halt a running or suspended implementation associated with a key. This is
often used for stopping processes or cleaning up resources. For example, a
database connection might be closed. This multimethod must be idempotent.
The return value of this multimethod is discarded.
Examples
integrant
(ns integrant.core-test
(:require #?(:clj [clojure.test :refer [are deftest is testing]]
:cljs [cljs.test :refer-macros [are deftest is testing]])
[integrant.core :as ig]
[weavejester.dependency :as dep]))
(defmethod ig/halt-key! :default [k v]
(swap! log conj [:halt k v]))
(defmethod ig/halt-key! ::error-halt [_ _]
(throw (ex-info "Testing" {:reason ::test})))
(testing "exception when running"
(let [system (ig/init {::a 1
::error-halt (ig/ref ::a)
::b (ig/ref ::error-halt)
::c (ig/ref ::b)})
ex (try (ig/halt! system)
(catch #?(:clj Throwable :cljs :default) t t))]
(is (some? ex))
(is (= (#?(:clj .getMessage :cljs ex-message) ex)
(str "Error on key " ::error-halt " when running system")))
(is (= (ex-data ex)
{:reason ::ig/run-threw-exception
:system {::a [1], ::error-halt [[1]]
::b [[[1]]], ::c [[[[1]]]]}
:completed-keys '(::c ::b)
:remaining-keys '(::a)
:function ig/halt-key!
:key ::error-halt
:value [[1]]}))
(let [cause (#?(:clj .getCause :cljs ex-cause) ex)]
(is (some? cause))
(is (= (#?(:clj .getMessage :cljs ex-message) cause) "Testing"))
(is (= (ex-data cause) {:reason ::test}))))))
cljdoc/cljdoc
Examples:
conco -> clj-concordion
ring -> ring
org.clojure -> org.clojure:clojure, org.clojure:clojurescript
nyam -> clj-nyam
re-frame -> re-frame with re-frame:re-frame first
frame -> licaltown/frame (because exact match weighs heavy) then later re-frame:re-frame
RiNg -> ring
RèWRïTÉ -> rewrite-clj
nervous ->io.nervous:* artifacts"
(:require
[cljdoc.server.search.search :as search]
[tea-time.core :as tt]
[clojure.tools.logging :as log]
[integrant.core :as ig])
(:import (java.util.concurrent TimeUnit)
(org.apache.lucene.store Directory)))
(defmethod ig/halt-key! :cljdoc/searcher [k {:keys [artifact-indexer index] :as _searcher}]
(log/info "Stopping" k)
(when artifact-indexer
(log/info "Stopping ArtifactIndexer")
(tt/cancel! artifact-indexer))
(when index
(search/index-close index)))
(def sr (ig/init-key :cljdoc/searcher {:index-dir "data/index"}))
(ig/halt-key! :cljdoc/searcher sr)
juxt/edge
(ns edge.executor
(:require
[integrant.core :as ig]
[clojure.tools.logging :as log]
[manifold.executor :as me]))
(defmethod ig/halt-key! :edge/executor [_ e]
(when e
(log/debug "Shutting down executor")
(.shutdownNow e)
(log/debug "Awaiting executor termination")
(.awaitTermination e 5 java.util.concurrent.TimeUnit/SECONDS)
(log/debug "Executor terminated")))
kit-clj/kit
(ns kit.edge.db.xtdb
(:require
[xtdb.api :as xtdb]
[integrant.core :as ig]))
(defmethod ig/halt-key! :db.xtdb/node
[_ xtdb-node]
(.close xtdb-node))
FundingCircle/jackdaw
(ns user
(:require [clojure.string :as str]
[jackdaw.admin :as ja]
[jackdaw.streams :as j]
[jackdaw.repl :refer :all]
[integrant.core :as ig]
[integrant.repl :refer [clear go halt prep init reset reset-all]]
[word-count]))
(defmethod ig/halt-key! :topics [_ {:keys [client-config topic-metadata]}]
(let [re (re-pattern (str "(" (->> topic-metadata
keys
(map name)
(str/join "|"))
")"))]
(re-delete-topics client-config re)))
(defmethod ig/halt-key! :app [_ {:keys [streams-config topics streams-app]}]
(j/close streams-app)
;; BUG: Does not delete state on reset!
(destroy-state-stores streams-config)
(let [re (re-pattern (str "(" (get streams-config "application.id") ")"))]
(re-delete-topics (:client-config topics) re)))
Zetawar/zetawar
(ns zetawar.system.router
(:require
[cljs.core.async :as async]
[integrant.core :as ig]
[zetawar.router :as router]))
(defmethod ig/halt-key! :zetawar.system/router [_ router]
(let [{:keys [ev-chan notify-chan notify-pub]} router]
(async/close! ev-chan)
(async/close! notify-chan)))