Skip to content

Commit

Permalink
Add more CLI options and better error handlng
Browse files Browse the repository at this point in the history
  • Loading branch information
jeisses committed Sep 17, 2023
1 parent 22926f0 commit 119bef1
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 34 deletions.
72 changes: 45 additions & 27 deletions src/nosana_node/main.clj
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,15 @@
[nil "--ipfs-url URL" "IPFS url"]
[nil "--podman URI" "Podman connection URI"
:id :podman-conn-uri]
[nil "--market ADDR" "Solana address of the market the node will join."
:default-desc "$MARKET"
:id :nosana-market]
[nil "--pinata-jwt" "JWT used for communicating with Pinata."
:default-desc "$PINATA_JWT"]
["-n" "--network NETWORK" "Network to run on"
:default-desc "$SOLANA_NETWORK"
:parse-fn #(keyword %)
:id :solana-network]
[nil "--branch REF"
"Git branch to checkout when running a pipeline locally. If not set will use HEAD and include staged changes."
:id :git-branch]
Expand All @@ -56,15 +65,15 @@
options-summary
""
"Actions:"
" pipeline Run a local pipeilne"
" pipeline Run a local pipeline"
" start Start a node server"
""
"Please refer to the docs for more information."]
(string/join \newline)))

(defn use-cli
"Parse CLI arguments using `tools.cli` and add to system map."
[{:keys [cli-args] :as sys}]
[{:keys [cli-args nos/vault] :as sys}]

(let [{:keys [errors options arguments summary] :as res}
(cli/parse-opts cli-args cli-options)
Expand All @@ -79,43 +88,52 @@
(do
(println (:exit-message state))
(System/exit (if (:ok? state) 0 1)))

:else
(do
;; set logging level
(let [{:keys [verbosity]} options
log-level
(nth (reverse [:trace :debug :info :warn :error
:fatal :report])
verbosity)]
(log/set-min-level! log-level))

;; merge CLI over existing config
(cond->
(update sys :nos/vault merge state)
(= "pipeline" (first arguments))
(dissoc :run-server?))))))

(defn use-pipeline [{:keys [nos/vault] :as system}]
(let [{:keys [verbosity]} vault
log-level (nth (reverse [:trace :debug :info :warn :error
:fatal :report])
verbosity)]
(log/set-min-level! log-level))

(let [dir (System/getProperty "user.dir")]
(try
(pipeline/run-local-pipeline dir (:nos/vault system))
(catch Exception e
(println "Error: " (ex-message e))))))

(defn -main [& args]
(start-system
system
{:http/handler #'nos-sys/handler
:system/components [use-vault
use-cli
store/use-fs-store
(use-when :run-server?
use-nostromo
use-nosana
nos-sys/use-wrap-ctx
use-jetty)
(use-when #(not (:run-server? %))
use-pipeline)]
:system/profile :prod
:run-server? true
:cli-args args
:nos/log-dir "/tmp/logs"
:nos/store-path "/tmp/store"
:nos/vault-path (io/resource "config.edn")}))
(try
(start-system
system
{:http/handler #'nos-sys/handler
:system/components [use-vault
use-cli
store/use-fs-store
(use-when :run-server?
use-nostromo
use-nosana
nos-sys/use-wrap-ctx
use-jetty)
(use-when #(not (:run-server? %))
use-pipeline)]
:system/profile :prod
:run-server? true
:cli-args args
:nos/log-dir "/tmp/logs"
:nos/store-path "/tmp/store"
:nos/vault-path (io/resource "config.edn")})
(catch Exception e
(do
(log/log :trace e)
(println (ex-message e))))))
16 changes: 10 additions & 6 deletions src/nosana_node/nosana.clj
Original file line number Diff line number Diff line change
Expand Up @@ -205,9 +205,10 @@ Running Nosana Node %s
(.getPublicKey signer)
(:system sol/addresses))
programs (network nos-accounts)
market-pub (if (:nosana-market vault)
market-pub (try
(PublicKey. (:nosana-market vault))
(get-in nos-accounts [network :market]))
(catch Exception e
(throw (ex-info "Invalid market account" {}))))
market-vault (sol/pda
[(.toByteArray market-pub)
(.toByteArray (:nos-token programs))]
Expand Down Expand Up @@ -729,10 +730,11 @@ Running Nosana Node %s
(defn use-nosana
[{:nos/keys [store flow-chan vault] :as system}]
;; Wait a bit for podman to boot
(log :info "Waiting 5s for podman")
(Thread/sleep 6000)
(log :trace "Waiting 5s for podman")
(Thread/sleep 6)
(let [network (:solana-network vault)
_ (when (not (contains? sol/rpc network))
(throw (ex-info (str "Network must be in " (keys sol/rpc)) {})))
market (:nosana-market vault)
conf (make-config system)
exit-ch (chan)
Expand All @@ -754,8 +756,10 @@ Running Nosana Node %s

(case status
:success (println "Node healthy. LFG.")
:error (println (str "\u001B[31mNode not healthy:\u001B[0m\n- "
(string/join "\n- " msgs))))
:error (do
(println (str "\u001B[31mNode not healthy:\u001B[0m\n- "
(string/join "\n- " msgs)))
(throw (ex-info "Failed to start" {}))))

;; Add a shutdown hook, will be called when the JVM exits
(.addShutdownHook
Expand Down
2 changes: 1 addition & 1 deletion src/nosana_node/solana.clj
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@
first)]
(-> data util/base64->bytes byte-array)
(do
(log :error "No account data" {:addr addr})
(log :error "No account data for " (.toString addr))
nil)))

(defn get-token-accounts [owner network]
Expand Down

0 comments on commit 119bef1

Please sign in to comment.