Skip to content

Commit

Permalink
Merge pull request #232 from justinstoller/task/master/SERVER-893_clj…
Browse files Browse the repository at this point in the history
…-style-applied2

(SERVER-893) Apply pl-clojure-style recommendations
  • Loading branch information
rlinehan committed Apr 5, 2016
2 parents 1149b72 + 4e59be5 commit 007ac60
Show file tree
Hide file tree
Showing 25 changed files with 782 additions and 782 deletions.
30 changes: 15 additions & 15 deletions examples/java_service/src/clj/java_service_example/java_service.clj
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,20 @@
(meaning-of-life-fn [this]))

(defservice java-service
JavaService
[]
;; Service functions are implemented in a java `ServiceImpl` class
(msg-fn [this] (ServiceImpl/getMessage))
(meaning-of-life-fn [this] (ServiceImpl/getMeaningOfLife)))
JavaService
[]
;; Service functions are implemented in a java `ServiceImpl` class
(msg-fn [this] (ServiceImpl/getMessage))
(meaning-of-life-fn [this] (ServiceImpl/getMeaningOfLife)))

(defservice java-service-consumer
[[:JavaService msg-fn meaning-of-life-fn]
[:ShutdownService request-shutdown]]
(init [this context]
(log/info "Java service consumer!")
(log/infof "The message from Java is: '%s'" (msg-fn))
(log/infof "The meaning of life is: '%s'" (meaning-of-life-fn))
context)
(start [this context]
(request-shutdown)
context))
[[:JavaService msg-fn meaning-of-life-fn]
[:ShutdownService request-shutdown]]
(init [this context]
(log/info "Java service consumer!")
(log/infof "The message from Java is: '%s'" (msg-fn))
(log/infof "The meaning of life is: '%s'" (meaning-of-life-fn))
context)
(start [this context]
(request-shutdown)
context))
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
(trapperkeeper/defservice test-service
[]
(stop [this context]
(println "If you see this printed out then shutdown works correctly!")
context))
(println "If you see this printed out then shutdown works correctly!")
context))

(defn -main
[& args]
(println "Waiting for a shutdown signal - use Ctrl-C or kill.")
(println "You should see a message printed out when services are being shutdown.")
(trapperkeeper/run
{:config testutils/empty-config
:bootstrap-config "examples/shutdown_app/bootstrap.cfg"}))
{:config testutils/empty-config
:bootstrap-config "examples/shutdown_app/bootstrap.cfg"}))
2 changes: 1 addition & 1 deletion ext/pl-clojure-style
Submodule pl-clojure-style updated from dbfe1e to 414116
30 changes: 15 additions & 15 deletions src/puppetlabs/trapperkeeper/bootstrap.clj
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,13 @@
an IllegalArgumentException if the line is not valid."
[line :- schema/Str]
(if-let [[match namespace service-name] (re-matches
#"^([a-zA-Z0-9\.\-]+)/([a-zA-Z0-9\.\-]+)$"
line)]
#"^([a-zA-Z0-9\.\-]+)/([a-zA-Z0-9\.\-]+)$"
line)]
{:namespace namespace :service-name service-name}
(throw (IllegalArgumentException.
(str "Invalid line in bootstrap config file:\n\n\t"
line
"\n\nAll lines must be of the form: '<namespace>/<service-fn-name>'.")))))
(str "Invalid line in bootstrap config file:\n\n\t"
line
"\n\nAll lines must be of the form: '<namespace>/<service-fn-name>'.")))))

(schema/defn ^:private resolve-service! :- (schema/protocol services/ServiceDefinition)
"Given the namespace and name of a service, loads the namespace,
Expand All @@ -42,12 +42,12 @@
(try (require (symbol resolve-ns))
(catch FileNotFoundException e
(throw (IllegalArgumentException.
(str "Unable to load service: " resolve-ns "/" service-name)
e))))
(str "Unable to load service: " resolve-ns "/" service-name)
e))))
(if-let [service-def (ns-resolve (symbol resolve-ns) (symbol service-name))]
(internal/validate-service-graph! (var-get service-def))
(throw (IllegalArgumentException.
(str "Unable to load service: " resolve-ns "/" service-name)))))
(str "Unable to load service: " resolve-ns "/" service-name)))))

(schema/defn ^:private remove-comments :- schema/Str
"Given a line of text from the bootstrap config file, remove
Expand Down Expand Up @@ -77,8 +77,8 @@
;; (in the case of a normal path, it is useless - there is no
;; reason to mess with URIs)
(throw (IllegalArgumentException.
(str "Specified bootstrap config file does not exist: '"
config-path "'")))))))
(str "Specified bootstrap config file does not exist: '"
config-path "'")))))))

(schema/defn ^:private config-from-cli :- (schema/maybe BootstrapFiles)
"Given the data from the command-line (parsed via `core/parse-cli-args!`),
Expand All @@ -91,10 +91,10 @@
(when (contains? cli-data :bootstrap-config)
(when-let [config-path (cli-data :bootstrap-config)]
(let [config-files (flatten (map
find-bootstraps-from-path
(string/split config-path #",")))]
find-bootstraps-from-path
(string/split config-path #",")))]
(log/debug (format "Loading bootstrap configs:\n%s"
(string/join "\n" config-files)))
(string/join "\n" config-files)))
config-files))))

(schema/defn ^:private config-from-cwd :- (schema/maybe BootstrapFiles)
Expand Down Expand Up @@ -129,8 +129,8 @@
(config-from-classpath))]
bootstrap-configs
(throw (IllegalStateException.
(str "Unable to find bootstrap.cfg file via --bootstrap-config "
"command line argument, current working directory, or on classpath")))))
(str "Unable to find bootstrap.cfg file via --bootstrap-config "
"command line argument, current working directory, or on classpath")))))

(schema/defn chain-files :- [schema/Str]
"Takes a list of files, reads all their lines in, and returns a flattened seq
Expand Down
40 changes: 20 additions & 20 deletions src/puppetlabs/trapperkeeper/config.clj
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
(defprotocol ConfigService
(get-config [this] "Returns a map containing all of the configuration values")
(get-in-config [this ks] [this ks default]
"Returns the individual configuration value from the nested
"Returns the individual configuration value from the nested
configuration structure, where ks is a sequence of keys.
Returns nil if the key is not present, or the default value if
supplied."))
Expand Down Expand Up @@ -68,13 +68,13 @@
[path]
(when-not (.canRead (io/file path))
(throw (FileNotFoundException.
(format "Configuration path '%s' must exist and must be readable."
path))))
(format "Configuration path '%s' must exist and must be readable."
path))))
(if-not (fs/directory? path)
[path]
(mapcat
#(fs/glob (fs/file path %))
["*.ini" "*.conf" "*.json" "*.properties" "*.edn" "*.yaml" "*.yml"])))
#(fs/glob (fs/file path %))
["*.ini" "*.conf" "*.json" "*.properties" "*.edn" "*.yaml" "*.yml"])))

(defn load-config
"Given a path to a configuration file or directory of configuration files,
Expand All @@ -89,7 +89,7 @@
(apply ks/deep-merge-with-keys
(fn [ks & _]
(throw (IllegalArgumentException.
(str "Duplicate configuration entry: " ks)))))
(str "Duplicate configuration entry: " ks)))))
(merge {}))))

(defn config-service
Expand All @@ -98,18 +98,18 @@
parameter should be the path to an .ini file or a directory of .ini files."
[config-data-fn]
(service ConfigService
[]
(init [this context]
(assoc context :config (config-data-fn)))
(get-config [this]
(let [{:keys [config]} (service-context this)]
config))
(get-in-config [this ks]
(let [{:keys [config]} (service-context this)]
(get-in config ks)))
(get-in-config [this ks default]
(let [{:keys [config]} (service-context this)]
(get-in config ks default)))))
[]
(init [this context]
(assoc context :config (config-data-fn)))
(get-config [this]
(let [{:keys [config]} (service-context this)]
config))
(get-in-config [this ks]
(let [{:keys [config]} (service-context this)]
(get-in config ks)))
(get-in-config [this ks default]
(let [{:keys [config]} (service-context this)]
(get-in config ks default)))))

(schema/defn parse-config-data :- (schema/pred map?)
"Parses the .ini, .edn, .conf, .json, or .properties configuration file(s)
Expand All @@ -121,8 +121,8 @@
(if-not (contains? cli-data :config)
{:debug debug?}
(-> (:config cli-data)
(load-config)
(assoc :debug debug?)))))
(load-config)
(assoc :debug debug?)))))

(defn initialize-logging!
"Initializes the logging system based on the configuration data."
Expand Down
26 changes: 13 additions & 13 deletions src/puppetlabs/trapperkeeper/core.clj
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
:doc "An alias for the `puppetlabs.trapperkeeper.services/service` macro
so that it is accessible from the core namespace along with the
rest of the API."}
service #'services/service)
service #'services/service)

(def #^{:macro true
:doc "An alias for the `puppetlabs.trapperkeeper.services/defservice` macro
so that it is accessible from the core namespace along with the
rest of the API."}
defservice #'services/defservice)
defservice #'services/defservice)

(defn build-app
"Given a list of services and a map of configuration data, build an instance
Expand Down Expand Up @@ -168,14 +168,14 @@
(binding [*out* stream] (println msg) (flush))
(System/exit status))]
(try+
(-> (or args '())
(internal/parse-cli-args!)
(run))
(catch map? m
(case (without-ns (:type m))
:cli-error (quit 1 (:message m) *err*)
:cli-help (quit 0 (:message m) *out*)
(throw+)))
(finally
(log/debug "Finished TK main lifecycle, shutting down Clojure agent threads.")
(shutdown-agents)))))
(-> (or args '())
(internal/parse-cli-args!)
(run))
(catch map? m
(case (without-ns (:type m))
:cli-error (quit 1 (:message m) *err*)
:cli-help (quit 0 (:message m) *out*)
(throw+)))
(finally
(log/debug "Finished TK main lifecycle, shutting down Clojure agent threads.")
(shutdown-agents)))))
34 changes: 17 additions & 17 deletions src/puppetlabs/trapperkeeper/internal.clj
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
service graph."
[service-graph]
(and
(map? service-graph)
(every? keyword? (keys service-graph))
(every? (some-fn ifn? service-graph?) (vals service-graph))))
(map? service-graph)
(every? keyword? (keys service-graph))
(every? (some-fn ifn? service-graph?) (vals service-graph))))

(defn validate-service-graph!
"Validates that a ServiceDefinition contains a valid trapperkeeper service graph.
Expand All @@ -50,9 +50,9 @@
{:post [(satisfies? s/ServiceDefinition %)]}
(if-not (satisfies? s/ServiceDefinition service-def)
(throw (IllegalArgumentException.
(str "Invalid service definition; expected a service "
"definition (created via `service` or `defservice`); "
"found: " (pr-str service-def)))))
(str "Invalid service definition; expected a service "
"definition (created via `service` or `defservice`); "
"found: " (pr-str service-def)))))
(if (service-graph? (s/service-map service-def))
service-def
(throw (IllegalArgumentException. (str "Invalid service graph; service graphs must "
Expand Down Expand Up @@ -103,13 +103,13 @@

(if (sequential? (:error data))
(let [missing-services (keys (ks/filter-map
(fn [_ v] (= v 'missing-required-key))
(.error (first (:error data)))))]
(fn [_ v] (= v 'missing-required-key))
(.error (first (:error data)))))]
(if (= 1 (count missing-services))
(throw (RuntimeException.
(format "Service '%s' not found" (first missing-services))))
(format "Service '%s' not found" (first missing-services))))
(throw (RuntimeException.
(format "Services '%s' not found" missing-services)))))
(format "Services '%s' not found" missing-services)))))
(throw e)))))

(defn compile-graph
Expand Down Expand Up @@ -174,11 +174,11 @@
updated-ctxt (lifecycle-fn s (get-in @app-context [:service-contexts service-id] {}))]
(if-not (map? updated-ctxt)
(throw (IllegalStateException.
(format
"Lifecycle function '%s' for service '%s' must return a context map (got: %s)"
lifecycle-fn-name
(or (s/service-symbol s) service-id)
(pr-str updated-ctxt)))))
(format
"Lifecycle function '%s' for service '%s' must return a context map (got: %s)"
lifecycle-fn-name
(or (s/service-symbol s) service-id)
(pr-str updated-ctxt)))))
;; store the updated service context map in the application context atom
(swap! app-context assoc-in [:service-contexts service-id] updated-ctxt)))

Expand Down Expand Up @@ -397,7 +397,7 @@
(s/service ShutdownService
[]
(get-shutdown-reason [this] (when (realized? shutdown-reason-promise)
@shutdown-reason-promise))
@shutdown-reason-promise))
(wait-for-shutdown [this] (deref shutdown-reason-promise))
(request-shutdown [this] (request-shutdown* shutdown-reason-promise))
(shutdown-on-error [this svc-id f] (shutdown-on-error* shutdown-reason-promise app-context svc-id f))
Expand Down Expand Up @@ -553,7 +553,7 @@
(a/service-graph [this] graph-instance)
(a/app-context [this] app-context)
(a/check-for-errors! [this] (throw-app-error-if-exists!
this))
this))
(a/init [this]
(run-lifecycle-fns app-context s/init "init" ordered-services)
this)
Expand Down
12 changes: 6 additions & 6 deletions src/puppetlabs/trapperkeeper/plugins.clj
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@
[name]
(and
;; ignore directories
(not (or (.isDirectory (file name))
(.endsWith name "/"))) ; necessary for directories in .jars
(not (or (.isDirectory (file name))
(.endsWith name "/"))) ; necessary for directories in .jars

;; don't care about anything in META-INF
(not (.startsWith name "META-INF"))
(not (.startsWith name "META-INF"))

;; lein includes project.clj ... no thank you
(not (= name "project.clj"))))
(not (= name "project.clj"))))

(defn- handle-duplicate!
"Helper for `process-file`; handles a found duplicate. Throws an exception
Expand Down Expand Up @@ -60,7 +60,7 @@
(map #(.getName %) (enumeration-seq (.entries (JarFile. file)))))]
(reduce (partial process-file container-filename) acc filenames))
acc ; There may be directories on the classpath that do not exist.
)))
)))

(defn jars-in-dir
"Given a path to a directory on disk, returns a collection of all of the .jar
Expand Down Expand Up @@ -100,4 +100,4 @@
(log/info "Adding plugin .jar " (.getAbsolutePath jar) " to classpath.")
(kitchensink/add-classpath jar)))
(throw (IllegalArgumentException.
(str "Plugins directory " plugins-path " does not exist")))))))
(str "Plugins directory " plugins-path " does not exist")))))))
4 changes: 2 additions & 2 deletions src/puppetlabs/trapperkeeper/services.clj
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@
(let [{:keys [service-sym service-protocol-sym service-id service-fn-map
dependencies fns-map]}
(si/parse-service-forms!
lifecycle-fn-names
forms)
lifecycle-fn-names
forms)
output-schema (si/build-output-schema (keys service-fn-map))]
`(reify ServiceDefinition
(service-def-id [this] ~service-id)
Expand Down
Loading

0 comments on commit 007ac60

Please sign in to comment.