The way I'd do it is to write a function that caches the output writer or
stream. When the cache expires, the function closes the old writer and
opens a new one to the new file.
In the example below, I'm assuming you have a function that returns the
same name for the same hour.
(let [cache (volatile! nil)]
(defn get-output-writer []
(locking cache
(let [filename (get-output-filename-for-this-hour)]
(when (or (nil? @cache) (not= (:filename @cache) filename))
(if-let [writer (:writer @cache)] (.close writer))
(vreset! cache {:filename filename, :writer (io/writer filename)
})))
(:writer @cache))))
Note that I'm using the locking macro to ensure that the function cannot be
executed simultaneously by multiple threads.
- James
On 31 July 2015 at 13:23, Marcin Jurczuk <[email protected]> wrote:
> Hi all,
>
> This is my first post here so please be compassionate :)
>
> I'm evaluating clojure for some of my purposes and I've stuck on task that
> I don't know to handle.
> I have applicatin that is receiving a lot of data and writing some results
> to a file.
> However once per hour this output file must be changes so data are in
> chunks.
> I have working solution with writing to one file but I don't know where to
> start code for changeing "this on the fly".
> What I need is to avoid open-close cycle with every piece of data to save
> since data stream is big.I need only close one file and open another one
> (name generated on some rules) where data will be continously written once
> per hour.
>
> I have this app written in golang where there are just two goroutines and
> main code retriving data.
> Writing goroutine beside data to write channel also have filename channel
> and when it receives message here it just close current file and open
> another but file handle var (i.e outFile) is always the same.
> This file swap is made with mutex arround and other protection so there is
> no case when write is made to a closed file.
>
> There is another timer goroutine that every ~ยง1h just calculate new
> filename and sends message to write goroutine.
> Works like expected :)
>
> As a proof of concept I'm trying to rewrite this in clojure.
> Summary it's like kind of syslog which have to change destination file
> periodically.
>
> Kind regards to everyone.
>
> Marcin.
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.