Skip to content

Instantly share code, notes, and snippets.

@bbirec
Created June 13, 2013 20:03
Show Gist options
  • Save bbirec/5776873 to your computer and use it in GitHub Desktop.
Save bbirec/5776873 to your computer and use it in GitHub Desktop.
;; SQS에 쌓인 메세지의 갯수를 보고 worker를 scale함
(def heroku-api-key (get (System/getenv) "HEROKU_API_KEY"))
(def heroku-app-id (get (System/getenv) "HEROKU_APP_ID"))
(def heroku-worker-name (get (System/getenv) "HEROKU_WORKER_NAME"))
(defn heroku-scale [api-key app-id type qty]
(client/post (format "https://api.heroku.com/apps/%s/ps/scale" app-id)
{:basic-auth ["" api-key]
:accept :json
:form-params {:type type
:qty qty}}))
(defn get-num-attr [attrs name]
(Integer/parseInt
(get attrs name "0")))
(defn -main []
(if-not (or (empty? heroku-api-key)
(empty? heroku-app-id)
(empty? heroku-worker-name))
(if-let [attrs (sqs/queue-attrs client dabang-queue)]
(let [visible (get-num-attr attrs
"ApproximateNumberOfMessages")
not-visible (get-num-attr attrs
"ApproximateNumberOfMessagesNotVisible")
delayed (get-num-attr attrs
"ApproximateNumberOfMessagesDelayed")
all-count (+ visible not-visible delayed)]
;; Worker를 켜거나 끔
(if (> all-count 0)
(do
(println "Scale up to " 1)
(heroku-scale heroku-api-key heroku-app-id heroku-worker-name 1))
(do
(println "Scale down to 0")
(heroku-scale heroku-api-key heroku-app-id heroku-worker-name 0))
)))
(println "Need api-key, app-id, and worker-name")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment