Skip to content

Instantly share code, notes, and snippets.

@bbbates
bbbates / gist:c3e13f65bb37518b0865
Created January 1, 2015 23:47
Korma - entity transforms + key naming strategy = no worky
(use 'korma.core)
(use 'korma.db
(defdb db
(postgres {:naming {:keys (comp keyword clojure.string/upper-case name)}}))
(declare user)
(defentity address
(transform identity)
@bbbates
bbbates / gist:05cb66644f59918a70d0
Last active August 29, 2015 14:09
fizzbuzz - no conditionals
(defn fizzbuzz
[n]
(let [r [(= 0 (mod n 3)) (= 0 (mod n 5))]]
(get {[true false] "fizz" [false true] "buzz" [true true] "fizzbuzz"} r n)))
(map fizzbuzz (range 1 100))
@bbbates
bbbates / scheduler.clj
Created January 3, 2014 03:27
Using leader-guarantee to activate a quartz scheduler
(ns my-app.scheduler
(:use leader-guarantee.core
confijulate.core)
(:require [clojurewerkz.quartzite.scheduler :as qs]))
(defn initialise
"The initialisation function for the scheduling system"
[]
(when (get-cfg :jobs :enable-schedule) ;; Still may want to disable schedule in local dev environment
(when-leader "my-app-cluster"
@bbbates
bbbates / core.clj
Created January 3, 2014 03:23
Allowing for multiple forms to wait until leader election
(ns leader-guarantee.core
(:import (org.jgroups.blocks.locking LockService)
(org.jgroups.protocols CENTRAL_LOCK)
(org.jgroups JChannel)
(java.util.concurrent.locks Lock)))
(def get-lock-threads (atom {}))
(defn- get-lock
[lock-service lock-promise]
@bbbates
bbbates / core.clj
Created January 3, 2014 03:15
Using LockService to guarantee leader with Clojure and JGroups
(ns leader-guarantee.core
(:import (org.jgroups.blocks.locking LockService)
(org.jgroups.protocols CENTRAL_LOCK)
(org.jgroups JChannel)
(java.util.concurrent.locks Lock)))
(defn- get-lock
[lock-service with-lock-fn]
(fn []
(let [lock (.getLock lock-service "leader")]
@bbbates
bbbates / gist:8230170
Last active January 2, 2016 01:28
Enabling scheduling on a single node with Confijulate
(ns
^:cfj-config
my-app.config
(:require [confijulate.core :as cfj]))
(def ^:cfj-base base-config
{
:jobs
{:enable-schedule false} ;;disabled by default
}
@bbbates
bbbates / gist:8167675
Created December 29, 2013 05:15
Confijulate example configuration namespace
(ns
^:cfj-config
example.config
(:require [confijulate.core :as cfj]))
(def ^:cfj-base base-config
{:db
{:classname "net.sf.log4jdbc.DriverSpy"
:subprotocol "log4jdbc"
:subname "postgresql:example"
@bbbates
bbbates / gist:8167593
Last active January 1, 2016 15:59
Merging simple map-based environment configuration
(def base-config
{
:value "some value"
})
(defn- merge-cfg-item
[base-item override-item]
(cond
(and (every? map? [base-item override-item]) (not (contains? override-item :replace-all!)))
(merge-with merge-cfg-item base-item override-item)
@bbbates
bbbates / gist:8167455
Last active January 1, 2016 15:59
Simple map-based clojure configuration
(ns app.config)
(def local-dev-config
{:enable-schedule false
:db {
:username "local-user"
:password "password"
}})
(def test-config