001  (ns cmp.worker.read-exchange
002    ^{:author "wactbprot"
003      :doc "Reads from exchange interface and 
004            writes to docpath."}
005    (:require [cmp.doc                 :as doc]
006              [cmp.exchange            :as exch]
007              [com.brunobonacci.mulog  :as mu]
008              [cmp.st-mem              :as st]))
009  
010  (defn read-exchange!
011    "Reads the `exch-val` from `:ExchangePath` and writes the result to
012    `:DocPath`. The `exch-val` have to be turned into a vector, to fit
013    the `doc/store!` function.
014    
015    ```clojure
016    (read-exchange! {})
017    ```"
018    [{doc-path :DocPath exch-path :ExchangePath mp-id :MpName state-key :StateKey :as task}]
019    (st/set-state! state-key :working)
020    (let [exch-val (exch/read! mp-id exch-path)
021          res-doc  (doc/store! mp-id [exch-val] doc-path)]
022      (if (:ok res-doc)
023        (st/set-state! state-key (if (exch/stop-if task) :executed :ready) "res doc ok")
024        (st/set-state! state-key :error))))