Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

extract initializers, loaders, and services into their own namespaces

  • Loading branch information...
commit f127a605e051ca052570e934e1e3324456cec11e 1 parent a1aaef4
@duck1123 authored
View
86 src/ciste/config.clj
@@ -22,7 +22,8 @@ Example:
(:use [lamina.executor :only [task]])
(:require [clojure.string :as string]
[clojure.tools.logging :as log])
- (:import java.net.InetAddress))
+ (:import java.io.FileNotFoundException
+ java.net.InetAddress))
;; TODO: read from env var
(defonce
@@ -44,10 +45,6 @@ Example:
(ref {}))
-(defonce ^:dynamic *initializers* (ref []))
-(defonce ^:dynamic *initializer-order* (ref []))
-
-
(defn get-host-name
"Returns the hostname of the host's local adapter."
[]
@@ -141,56 +138,6 @@ Example:
assoc-in (concat [(environment)] ks) value))
value)
-(defmacro definitializer
- "Defines an initializer. When an environment is bound, the initializers will
-be run in the order that they are loaded.
-
-Initializers are blocks of code that need to set up the environment of
-the namespace, but cannot run until the configuration system is
-available with a valid environment.
-
-Whenever the environment is changed, the initializers will run in the
-order they were declared.
-
-Note: At this time, Initializers will be re-run if the namespace is
-reloaded. For this reason, it is recommended that initializers be able
-to handle being run multiple times gracfully.
-
-Example:
-
- (ns ciste.example
- (:use [ciste.config :only (definitializer)]))
-
- (definitializer
- (println \"This will be run when the environment is set\")
- (println (config :hostname)))
-
- (println \"out of the initializer\"
-
-
- > (use 'ciste.example)
- out of the initializer
- > (set-environment! :development)
- This will be run when the environment is set
- server1.example.com"
- [& body]
- `(let [init-fn# (fn []
- #_(log/debug (str "running initializer - " *ns*))
- ~@body)]
- (dosync
- #_(log/debug (str "adding initializer - " *ns*))
- (alter *initializers* conj init-fn#))
- (try
- (when (environment) (init-fn#))
- (catch RuntimeException e#))))
-
-(defn run-initializers!
- "Run all initializers"
- []
- (task
- (doseq [init-fn @*initializers*]
- (init-fn))))
-
(defn set-environment!
"Sets's the environment globally"
[env]
@@ -226,3 +173,32 @@ Example:
(println (:path m))
(println " " (:type m))
(println (:doc m))))
+
+(defonce
+ ^{:doc "By default, the runner will look for a file with this name at the root
+ of the project directory."}
+ default-site-config-filename "ciste.clj")
+
+(defonce
+ ^{:doc "Ref containing the currently loaded site config"}
+ default-site-config (ref {}))
+
+(defn read-site-config
+ "Read the site config file"
+ ([] (read-site-config default-site-config-filename))
+ ([filename]
+ (try
+ ;; TODO: Check a variety of places for this file.
+ (-> filename slurp read-string)
+ (catch FileNotFoundException ex
+ ;; TODO: Throw an exception here
+ (throw (RuntimeException.
+ "Could not find service config. Ensure that ciste.clj exists at the root of your application and is readable"))))))
+
+(defn load-site-config
+ "Read the site config and store it for later use"
+ []
+ (let [site-config (read-site-config)]
+ (dosync
+ (ref-set default-site-config site-config))))
+
View
61 src/ciste/initializer.clj
@@ -0,0 +1,61 @@
+(ns ciste.initializer
+ (:use [ciste.config :only [environment]]
+ [lamina.executor :only [task]])
+ (:require [clojure.string :as string]
+ [clojure.tools.logging :as log]))
+
+
+(defonce ^:dynamic *initializers* (ref []))
+(defonce ^:dynamic *initializer-order* (ref []))
+
+
+(defmacro definitializer
+ "Defines an initializer. When an environment is bound, the initializers will
+be run in the order that they are loaded.
+
+Initializers are blocks of code that need to set up the environment of
+the namespace, but cannot run until the configuration system is
+available with a valid environment.
+
+Whenever the environment is changed, the initializers will run in the
+order they were declared.
+
+Note: At this time, Initializers will be re-run if the namespace is
+reloaded. For this reason, it is recommended that initializers be able
+to handle being run multiple times gracfully.
+
+Example:
+
+ (ns ciste.example
+ (:use [ciste.config :only (definitializer)]))
+
+ (definitializer
+ (println \"This will be run when the environment is set\")
+ (println (config :hostname)))
+
+ (println \"out of the initializer\"
+
+
+ > (use 'ciste.example)
+ out of the initializer
+ > (set-environment! :development)
+ This will be run when the environment is set
+ server1.example.com"
+ [& body]
+ `(let [init-fn# (fn []
+ #_(log/debug (str "running initializer - " *ns*))
+ ~@body)]
+ (dosync
+ #_(log/debug (str "adding initializer - " *ns*))
+ (alter *initializers* conj init-fn#))
+ (try
+ (when (environment) (init-fn#))
+ (catch RuntimeException e#))))
+
+(defn run-initializers!
+ "Run all initializers"
+ []
+ (task
+ (doseq [init-fn @*initializers*]
+ (init-fn))))
+
View
43 src/ciste/loader.clj
@@ -0,0 +1,43 @@
+(ns ciste.loader
+ (:use [ciste.config :only [config default-site-config load-config set-environment!]])
+ (:require [clojure.tools.logging :as log])
+ (:import java.util.concurrent.ConcurrentLinkedQueue))
+
+(defonce pending-requires (ConcurrentLinkedQueue.))
+
+(defn consume-require
+ [sym]
+ (try
+ (log/debugf "Loading %s" sym)
+ (require sym)
+ (log/debugf " - %s loaded" sym)
+ (catch Exception ex
+ (log/error ex)
+ (.printStackTrace ex)
+ (System/exit 0))))
+
+
+(defn require-namespaces
+ "Require the sequence of namespace strings"
+ [namespaces]
+ (doseq [sn namespaces]
+ (let [sym (symbol sn)]
+ (log/debugf "enqueuing %s" sym)
+ (.add pending-requires sym))))
+
+(defn require-modules
+ "Require each namespace"
+ ([] (require-modules @default-site-config))
+ ([service-config]
+ (require-namespaces (concat (:modules service-config)
+ (:services service-config)
+ (config :modules)
+ (config :services)))))
+
+(defn process-requires
+ []
+ (loop [sym (.poll pending-requires)]
+ (when sym
+ (consume-require sym)
+ (recur (.poll pending-requires)))))
+
View
4 src/ciste/routes.clj
@@ -60,7 +60,7 @@ possible to write Serializers that will respond to a request by
transmitting the response in any number of ways. (XMPP, Email,
Filesystem, etc.)"
(:use [ciste.config :only [config]]
- [ciste.core :only [with-context apply-template serialize-as]]
+ [ciste.core :only [with-context apply-template serialize-as *serialization*]]
[ciste.filters :only [filter-action]]
[clojure.core.incubator :only [-?>>]])
(:require [ciste.formats :as formats]
@@ -147,7 +147,7 @@ Returns either a (possibly modified) request map if successful, or nil."
(views/apply-view request)
(apply-template request)
(formats/format-as format request)
- (serialize-as (:serialization request))))))
+ (serialize-as *serialization*)))))
(defn resolve-route
"If the route matches the predicates, invoke the action"
View
111 src/ciste/runner.clj
@@ -2,117 +2,20 @@
"This is the runner for ciste applications.
Specify this namespace as the main class of your application."
- (:use [ciste.config :only [config load-config run-initializers!
- set-environment!]]
+ (:use [ciste.config :only [config default-site-config load-config load-site-config set-environment!]]
+ [ciste.initializer :only [run-initializers!]]
[lamina.core :only [enqueue on-drained permanent-channel
receive-in-order receive-all]]
lamina.executor)
- (:require [clojure.tools.logging :as log])
- (:import java.io.FileNotFoundException
- java.util.concurrent.ConcurrentLinkedQueue))
+ (:require [ciste.service :as service]
+ [clojure.tools.logging :as log]))
(defonce application-promise (ref nil))
-(defonce
- ^{:doc "By default, the runner will look for a file with this name at the root
- of the project directory."}
- default-site-config-filename "ciste.clj")
-
-(defonce
- ^{:doc "Ref containing the currently loaded site config"}
- default-site-config (ref {}))
-
-(defonce pending-requires (ConcurrentLinkedQueue.))
-
-(defn consume-require
- [sym]
- (try
- #_(log/debugf "Loading %s" sym)
- (require sym)
- #_(log/debugf " - %s loaded" sym)
- (catch Exception ex
- (log/error ex)
- (.printStackTrace ex)
- (System/exit 0))))
-
-
-(defn read-site-config
- "Read the site config file"
- ([] (read-site-config default-site-config-filename))
- ([filename]
- (try
- ;; TODO: Check a variety of places for this file.
- (-> filename slurp read-string)
- (catch FileNotFoundException ex
- ;; TODO: Throw an exception here
- (throw (RuntimeException.
- "Could not find service config. Ensure that ciste.clj exists at the root of your application and is readable"))))))
-
-(defn load-site-config
- "Read the site config and store it for later use"
- []
- (let [site-config (read-site-config)]
- (dosync
- (ref-set default-site-config site-config))))
-
-(defn require-namespaces
- "Require the sequence of namespace strings"
- [namespaces]
- (doseq [sn namespaces]
- (let [sym (symbol sn)]
- #_(log/debugf "enqueuing %s" sym)
- (.add pending-requires sym))))
-
-(defn require-modules
- "Require each namespace"
- ([] (require-modules @default-site-config))
- ([service-config]
- (require-namespaces (concat (:modules service-config)
- (:services service-config)
- (config :modules)
- (config :services)))))
-
-(defn start-services!
- "Start each service."
- ([] (start-services! @default-site-config))
- ([site-config]
- (doseq [service-name (concat (:services site-config)
- (config :services))]
- (let [service-sym (symbol service-name)]
- (log/info (str "Starting " service-name))
- (require service-sym)
- ((intern (the-ns service-sym) (symbol "start")))))))
-
-(defn process-requires
- []
- (loop [sym (.poll pending-requires)]
- (when sym
- (consume-require sym)
- (recur (.poll pending-requires)))))
-
-(defn init-services
- "Ensure that all namespaces for services have been required and that the
- config provider has benn initialized"
- [environment]
- ;; TODO: initialize config backend
- (load-config)
- (set-environment! environment)
- (require-modules)
- (run-initializers!)
- (process-requires))
-
-(defn stop-services!
- ([] (stop-services! @default-site-config))
- ([site-config]
- (doseq [service-name (concat (:services site-config)
- (config :services))]
- (log/info (str "Stopping " service-name))
- ((intern (the-ns (symbol service-name)) (symbol "stop"))))))
-
(defn stop-application!
[]
(log/info "Stopping application")
- (stop-services!)
+ (service/stop-services!)
(deliver @application-promise true))
(defn start-application!
@@ -121,10 +24,10 @@ Specify this namespace as the main class of your application."
(:environment @default-site-config))))
([environment]
(log/info "Starting application")
- (init-services environment)
+ (service/init-services environment)
(dosync (ref-set application-promise (promise)))
;; (run-initializers!)
- (start-services!)
+ (service/start-services!)
@application-promise))
(defn -main
View
37 src/ciste/service.clj
@@ -0,0 +1,37 @@
+(ns ciste.service
+ (:use [ciste.config :only [config load-config set-environment!
+ default-site-config]]
+ [ciste.initializer :only [run-initializers!]]
+ [ciste.loader :only [process-requires require-modules]])
+ (:require [clojure.tools.logging :as log]))
+
+(defn start-services!
+ "Start each service."
+ ([] (start-services! @default-site-config))
+ ([site-config]
+ (doseq [service-name (concat (:services site-config)
+ (config :services))]
+ (let [service-sym (symbol service-name)]
+ (log/info (str "Starting " service-name))
+ (require service-sym)
+ ((intern (the-ns service-sym) (symbol "start")))))))
+
+(defn init-services
+ "Ensure that all namespaces for services have been required and that the
+ config provider has benn initialized"
+ [environment]
+ ;; TODO: initialize config backend
+ (load-config)
+ (set-environment! environment)
+ (require-modules)
+ (run-initializers!)
+ (process-requires))
+
+(defn stop-services!
+ ([] (stop-services! @default-site-config))
+ ([site-config]
+ (doseq [service-name (concat (:services site-config)
+ (config :services))]
+ (log/info (str "Stopping " service-name))
+ ((intern (the-ns (symbol service-name)) (symbol "stop"))))))
+
View
4 src/ciste/test_helper.clj
@@ -1,6 +1,6 @@
(ns ciste.test-helper
- (:use [ciste.runner :only [load-site-config start-application!
- stop-application!]]))
+ (:use [ciste.config :only [load-site-config]]
+ [ciste.runner :only [start-application! stop-application!]]))
(defmacro test-environment-fixture
"Wrapper to ensure tests are run in the test environment"
View
3  src/ciste/triggers.clj
@@ -19,7 +19,8 @@ Example:
\"Do something in a different thread\")
(ciste.trigger/add-trigger! #'my-action #'my-trigger)"
- (:use [ciste.config :only [config definitializer describe-config]])
+ (:use [ciste.config :only [config describe-config]]
+ [ciste.initializer :only [definitializer]])
(:require [clojure.tools.logging :as log])
(:import java.util.concurrent.Executors
java.util.concurrent.ExecutorService))
Please sign in to comment.
Something went wrong with that request. Please try again.