Generate PNG files on worker pool

next
Marcin Kulik 8 years ago
parent f0c25626d9
commit 93ec479bb0

@ -4,14 +4,15 @@
:db #var asciinema.component.db/hikaricp :db #var asciinema.component.db/hikaricp
:ragtime #var duct.component.ragtime/ragtime :ragtime #var duct.component.ragtime/ragtime
:file-store #var asciinema.component.s3-file-store/s3-file-store :file-store #var asciinema.component.s3-file-store/s3-file-store
:exp-set #var asciinema.component.redis-client/redis-client} :exp-set #var asciinema.component.redis-client/redis-client
:executor #var asciinema.component.fixed-thread-executor/fixed-thread-executor}
:endpoints :endpoints
{:asciicasts #var asciinema.endpoint.asciicasts/asciicasts-endpoint} {:asciicasts #var asciinema.endpoint.asciicasts/asciicasts-endpoint}
:dependencies :dependencies
{:http [:app] {:http [:app]
:app [:asciicasts] :app [:asciicasts]
:ragtime [:db] :ragtime [:db]
:asciicasts [:db :file-store :exp-set]} :asciicasts [:db :file-store :exp-set :executor]}
:config :config
{:app {:app
{:middleware {:middleware
@ -64,4 +65,7 @@
:path-prefix "uploads/"} :path-prefix "uploads/"}
:exp-set :exp-set
{:host redis-host {:host redis-host
:port redis-port}}} :port redis-port}
:executor
{:threads 1
:queue-length 1}}}

@ -0,0 +1,4 @@
(ns asciinema.boundary.executor)
(defprotocol Executor
(execute [this f]))

@ -0,0 +1,39 @@
(ns asciinema.component.fixed-thread-executor
(:require [aleph.flow :as flow]
[asciinema.boundary.executor :as executor]
[com.stuartsierra.component :as component]
[manifold.deferred :as d])
(:import [java.util.concurrent
ExecutorService
RejectedExecutionException
TimeUnit]))
(defrecord FixedThreadExecutor [threads queue-length]
executor/Executor
(execute [{:keys [^ExecutorService executor]} f]
(try
(let [result (d/deferred)
f (fn []
(try
(d/success! result (f))
(catch Exception e
(d/error! result e))))]
(.execute executor f)
result)
(catch RejectedExecutionException _
{:status 503 :headers {"Retry-After" "5"} :body "<h1>503</h1>"})))
component/Lifecycle
(start [{:keys [threads queue-length] :as component}]
(let [executor (flow/fixed-thread-executor threads {:onto? false
:initial-thread-count threads
:queue-length queue-length})]
(assoc component :executor executor)))
(stop [{:keys [^ExecutorService executor] :as component}]
(.shutdown executor)
(when-not (.awaitTermination executor 1000 TimeUnit/MILLISECONDS)
(.shutdownNow executor))
(assoc component :executor nil)))
(defn fixed-thread-executor [{:keys [threads queue-length]}]
(->FixedThreadExecutor threads queue-length))

@ -3,6 +3,7 @@
[asciicast-database :as adb] [asciicast-database :as adb]
[expiring-set :as exp-set] [expiring-set :as exp-set]
[file-store :as fstore] [file-store :as fstore]
[executor :as executor]
[user-database :as udb]] [user-database :as udb]]
[asciinema.model.asciicast :as asciicast] [asciinema.model.asciicast :as asciicast]
[asciinema.util.io :refer [with-tmp-dir]] [asciinema.util.io :refer [with-tmp-dir]]
@ -37,7 +38,7 @@
(def png-ttl-days 7) (def png-ttl-days 7)
(defn asciicasts-endpoint [{:keys [db file-store exp-set]}] (defn asciicasts-endpoint [{:keys [db file-store exp-set executor]}]
(api (api
{:exceptions {:handlers {:compojure.api.exception/default exception-handler}}} {:exceptions {:handlers {:compojure.api.exception/default exception-handler}}}
(context (context
@ -63,7 +64,9 @@
theme (assoc :theme theme) theme (assoc :theme theme)
scale (assoc :scale (Integer/parseInt scale))) scale (assoc :scale (Integer/parseInt scale)))
png-store-path (asciicast/png-store-path asciicast png-params)] png-store-path (asciicast/png-store-path asciicast png-params)]
(when-not (exp-set/contains? exp-set png-store-path) (if (exp-set/contains? exp-set png-store-path)
(fstore/serve-file file-store png-store-path {})
(executor/execute executor (fn []
(with-tmp-dir [dir "asciinema-png-"] (with-tmp-dir [dir "asciinema-png-"]
(let [json-store-path (asciicast/json-store-path asciicast) (let [json-store-path (asciicast/json-store-path asciicast)
json-local-path (str dir "/asciicast.json") json-local-path (str dir "/asciicast.json")
@ -74,6 +77,6 @@
(io/copy in out))) (io/copy in out)))
(a2png json-local-path png-local-path png-params) (a2png json-local-path png-local-path png-params)
(fstore/put-file file-store (io/file png-local-path) png-store-path) (fstore/put-file file-store (io/file png-local-path) png-store-path)
(exp-set/conj! exp-set png-store-path expires)))) (exp-set/conj! exp-set png-store-path expires)))
(fstore/serve-file file-store png-store-path {})) (fstore/serve-file file-store png-store-path {})))))
(response/not-found)))))) (response/not-found))))))

Loading…
Cancel
Save