12
\$\begingroup\$

Disclaimer: This is just an educational task to learn how to program in Clojure and switch brain to immutable state-way of developing (the provided snipped has some unused variables which I didn't clean up since they will be used soon and are easy to understand).

The current milestone: to implement a solution that reads all lines from a file, if reaching file end - sleep and reopen a file, then continue trying reading the file from the same place.

This code is ugly without doubts. What makes me worrying is that it uses recursion in an ugly manner to maintain state. I think my next iteration will be to try using atoms to maintain mutable state instead of recursions, but not sure yet if it will be any better.

The main function currently for testing purposes just reads 3 lines from the channel.

(ns log-multiline-join.tailer
  (:use [clojure.core.async :only (>!! >! <!! thread chan timeout alt!)])
  (:import [java.io RandomAccessFile])
  (:gen-class))
 
(defn create-rac [filename]
  (RandomAccessFile. filename "r"))
 
(defn read-lines [rac position out stop]
  (.seek rac position)
  (if-let [line (.readLine rac)]
    (do
      (>!! out line)
      (recur rac (.getFilePointer rac) out stop))
    (.getFilePointer rac)))
 
(defn read-file-recur [filename position out]
  (let [rac (create-rac filename)
        position (read-lines rac position out 2)]
 
    (println "sleep")
    (Thread/sleep 1000)
    (recur filename position out)))
 
(defn tailer [filename out]
  (read-file-recur filename 0 out))
 
(defn -main []
  (let [out (chan)]
 
    (thread (tailer "/tmp/file" out))
 
    (println (<!! out))
    (println (<!! out))
    (println (<!! out))
 
    ))
\$\endgroup\$
2
  • \$\begingroup\$ You're braver than I am--if I had to do something like this in Clojure, I'd probably just call into Java. \$\endgroup\$ Commented Nov 15, 2014 at 19:07
  • \$\begingroup\$ @tsleyson hehe :-) PS: I added a link to the latest revision, which looks much better \$\endgroup\$ Commented Nov 15, 2014 at 19:20

0

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.