~subsetpark/bagatto

afc04f637ad02216e9b9b8336411e6916c8f221c — Zach Smith 2 months ago f2e3380
Monitor output jobs
2 files changed, 26 insertions(+), 11 deletions(-)

M src/core.janet
M src/writers.janet
M src/core.janet => src/core.janet +13 -2
@@ 78,8 78,19 @@
  ```
  [output-dir writer-specs]

  (def handler (writers/handle-writes output-dir))
  (def spec-count (length writer-specs))
  (def handler (writers/write-handler output-dir))
  (def cache @{})

  (printf "Writing %d output specs..." spec-count)

  (def supervisor (ev/chan spec-count))
  (each spec writer-specs
    (ev/spawn (handler spec cache))))
    (ev/go (fiber/new (fn [] (handler spec cache)) :a) nil supervisor))

  (var oked 0)
  (while (< oked spec-count)
    (ev/take supervisor)
    (++ oked))

  (printf "Done writing."))

M src/writers.janet => src/writers.janet +13 -9
@@ 7,26 7,30 @@
  []
  @{})

(defn handle-writes
(defn ensure-path
  [path cache]
  (let [s (string path)]
    (unless (in cache s)
      (util/mkpath s cache)
      (put cache s true))))

(defn write-handler
  [output-dir]
  (fn write-handler [msg path-cache]
    (defn ensure-path [path]
      (let [s (string path)]
        (unless (in path-cache s)
          (util/mkpath s path-cache)
          (put path-cache s true))))

  (fn handler
    [msg path-cache]

    (match msg
      [:write path contents]
      (let [path (if output-dir (path/join output-dir path) path)
            ppath (path/dirname path)]

        (ensure-path ppath)
        (ensure-path ppath path-cache)
        (spit path contents))

      [:copy from to]
      (let [to (if output-dir (path/join output-dir to) to)
            ppath (path/dirname to)]

        (ensure-path ppath)
        (ensure-path ppath path-cache)
        (util/copy-file from to path-cache)))))