Skip to content

Instantly share code, notes, and snippets.

@pcn
Last active December 31, 2015 13:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pcn/7992191 to your computer and use it in GitHub Desktop.
Save pcn/7992191 to your computer and use it in GitHub Desktop.
This function is supposed to allow a single thread to be the file writer for a spool which creates files only when needed.
carbon-relaj.core=> (>!! spool-channel ["a.b.c.d" (System/currentTimeMillis) (System/currentTimeMillis) ])
nil
Not nil: [a.b.c.d 1387219958954 1387219958954]
["a.b.c.d",1387219958954,1387219958954]
file-ref-map has ([:writable-file #<BufferedWriter java.io.BufferedWriter@1c62f6a>] [:file-name "/tmp/foo/temp/1387219958.962"] [:the-time 1387219958962])
writable-file is java.io.BufferedWriter@1c62f6a
Exception in thread "Thread-1" java.lang.IllegalArgumentException: No matching method found: write for class java.io.BufferedWriter
at clojure.lang.Reflector.invokeMatchingMethod(Reflector.java:53)
at clojure.lang.Reflector.invokeInstanceMethod(Reflector.java:28)
at carbon_relaj.core$write_data_to_file.invoke(core.clj:199)
at carbon_relaj.core$write_metrics_to_file.invoke(core.clj:223)
at carbon_relaj.core$eval3410$fn__3411.invoke(core.clj:283)
at clojure.lang.AFn.run(AFn.java:24)
at java.lang.Thread.run(Thread.java:744)
(defn write-data-to-file [config file-ref-map data]
(if (= (@file-ref-map :file-name) "") ; activate the writeable file if it's not there
(let [the-time-ms (System/currentTimeMillis)
new-file-name (make-file-name (config :temp-dir) the-time-ms)
new-writable-file (clojure.java.io/writer new-file-name)]
(dosync
(alter file-ref-map assoc-in [:writable-file] new-writable-file)
(alter file-ref-map assoc-in [:file-name] new-file-name)
(alter file-ref-map assoc-in [:the-time] the-time-ms))))
(println (str "file-ref-map has " (seq @file-ref-map)))
(println (str "writable-file is " (@file-ref-map :writable-file)))
(.write (@file-ref-map :writable-file) (json/write-str data) "\n"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment