[#160] Housekeeping

This commit is contained in:
Peter Taoussanis 2016-03-31 10:55:25 +07:00
parent f984bc8e08
commit 345a0d03f5
3 changed files with 56 additions and 47 deletions

View File

@ -35,7 +35,9 @@
[com.draines/postal "1.11.4"]
[irclj "0.5.0-alpha4"]
[org.graylog2/gelfclient "1.3.1"]
[org.julienxx/clj-slack "0.5.3"]]}
[org.julienxx/clj-slack "0.5.3"]
[org.clojure/java.jdbc "0.5.0"]
[com.mchange/c3p0 "0.9.5.2"]]}
:dev
[:1.7 :test
{:dependencies [[org.clojure/clojurescript "1.7.28"]]

View File

@ -1,47 +1,53 @@
(ns taoensso.timbre.appenders.3rd-party.postgresdb
"postgresql database appender"
(:require [clojure.java.jdbc :as j]
[environ.core :refer [env]]
[taoensso.timbre :as timbre]
[taoensso.encore :as encore])
(:import com.mchange.v2.c3p0.ComboPooledDataSource))
(def default-args {:type "postgresql" :host "127.0.0.1" :port 5432})
"PostgreSQL database appender.
Requires https://github.com/clojure/java.jdbc,
https://github.com/swaldman/c3p0"
{:author "Yue Liu (@yuliu-mdsol)"}
(:require
[taoensso.timbre :as timbre]
[taoensso.encore :as enc]
[clojure.java.jdbc :as jdbc]))
(defn pool [spec]
(let [cpds (doto (ComboPooledDataSource.)
(.setJdbcUrl (str "jdbc:" (:type spec) "://" (:host spec) ":" (:port spec) "/" (:database spec)))
(.setUser (:username spec))
(.setPassword (:password spec))
;; expire excess connections after 30 minutes of inactivity:
(.setMaxIdleTimeExcessConnections (* 30 60))
;; expire connections after 3 hours of inactivity:
(.setMaxIdleTime (* 3 60 60)))]
(let [cpds
(doto (com.mchange.v2.c3p0.ComboPooledDataSource.)
(.setJdbcUrl (str "jdbc:" (:type spec) "://" (:host spec) ":" (:port spec) "/" (:database spec)))
(.setUser (:username spec))
(.setPassword (:password spec))
;; expire excess connections after 30 minutes of inactivity:
(.setMaxIdleTimeExcessConnections (* 30 60))
;; expire connections after 3 hours of inactivity:
(.setMaxIdleTime (* 3 60 60)))]
{:datasource cpds}))
(defn connect [{:keys [server]}]
(let [args (merge default-args server)]
@(delay (pool args))))
(def default-pool-spec {:type "postgresql" :host "127.0.0.1" :port 5432})
(defn connect [config]
(let [spec (merge default-pool-spec (:server config))]
(pool spec)))
(def conn (atom nil))
(defn ensure-conn [config] (swap! conn #(or % (connect config))))
(defn log-message [data config]
(let [entry {:instant (java.sql.Timestamp. (.getTime (:instant data)))
:level (str (:level data))
:namespace (str (:?ns-str data))
:hostname (str @(:hostname_ data))
:content (str @(:vargs_ data))
:error (str @(:?err_ data))}]
(ensure-conn config)
(j/insert! @conn :logs entry)))
(defn log-message [config data]
(let [entry
{:instant (java.sql.Timestamp. (.getTime ^java.util.Date (:instant data)))
:level (str (:level data))
:namespace (str (:?ns-str data))
:hostname (str @(:hostname_ data))
:content (str @(:vargs_ data))
:error (str @(:?err_ data))}]
(defn pglog-appender
"Returns a postgresqlDB appender.
(ensure-conn config)
(jdbc/insert! @conn :logs entry)))
(defn pgsql-appender
"Returns a PostgreSQL appender for `clojure.java.jdbc`.
(pglog-appender {:server {:host \"127.0.0.1\" :port 5432}})
For log table creation and rollback, here are sql ddl script to use:
SQL ddl script for table creation and rollback
----------------------------------------------
```
CREATE TABLE IF NOT EXISTS logs (
log_id bigserial primary key,
instant timestamp NOT NULL,
@ -53,21 +59,20 @@
);
DROP TABLE IF EXISTS logs;
```
To automate database migration, you can use migratus for Leiningen,
or use ragtime for Boot.
To automate database migration
------------------------------
Leiningen migratus (in profiles.clj)
------------------------------------
:database-url \"postgresql://<db_username>:<db_password>@<db_servername>:<db_port>/<db_schema>\"
* Using Migratus + Leiningen (in profiles.clj):
:database-url \"postgresql://<db_username>:<db_password>@<db_servername>:<db_port>/<db_schema>\"
Boot ragtime (in build.boot)
----------------------------
ragtime {:driver-class \"org.postgresql.Driver\"
:database (str \"jdbc:postgresql://<dbserver_name>:<db_port>/\"
\"<db_schema>\"
\"?user=<db_usernmae>\"
\"&password=<db_password>\")})"
* Using Boot + Ragtime (in build.boot):
ragtime {:driver-class \"org.postgresql.Driver\"
:database (str \"jdbc:postgresql://<dbserver_name>:<db_port>/\"
\"<db_schema>\"
\"?user=<db_usernmae>\"
\"&password=<db_password>\")})"
[db-config]
{:enabled? true
@ -75,4 +80,4 @@
:min-level nil
:rate-limit nil
:output-fn :inherit
:fn (fn [data] (log-message data db-config))})
:fn (fn [data] (log-message db-config data))})

View File

@ -2,12 +2,14 @@
"You can copy this namespace if you'd like a starting template for
writing your own Timbre appender. PRs for new appenders welcome!
TODO Please document any dependency GitHub links here
- Peter Taoussanis"
{:author "Your Name (@your-github-username)"}
{:author "TODO Your Name (@your-github-username)"}
(:require
[taoensso.timbre :as timbre]))
;; If you add any special ns imports above, please remember to update
;; TODO If you add any special ns imports above, please remember to update
;; Timbre's `project.clj` to include the necessary dependencies under
;; the `:test` profile