Skip to content

Commit

Permalink
Merge pull request #1 from Cumulo/dev
Browse files Browse the repository at this point in the history
add function for picking port; release 0.1.8-a1
  • Loading branch information
TingYinHelen committed Apr 22, 2019
2 parents e2273f0 + 52481ad commit 3f44eb3
Show file tree
Hide file tree
Showing 10 changed files with 721 additions and 91 deletions.
2 changes: 1 addition & 1 deletion .gitattributes
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@

calcit.edn -diff linguist-generated
yarn.lock -diff
yarn.lock -diff linguist-generated
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@
.shadow-cljs/
.cpcache/
.nrepl-port

*.xml
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ Cumulo Util (WIP)
[![Clojars Project](https://img.shields.io/clojars/v/cumulo/util.svg)](https://clojars.org/cumulo/util)

```edn
[cumulo/util "0.1.6"]
[cumulo/util "0.1.8"]
```

Notice that you will need `shortid` in your project since it's depended on:
Expand All @@ -31,6 +31,7 @@ yarn add shortid
(cumulo-util.file/get-backup-path!)
(cumulo-util.file/merge-local-edn! {} "a.edn" (fn [found?] (println found?)))
(cumulo-util.file/sh! "ls") ; run shell command
(cumulo-util.file/chan-pick-port 7000) ; detect 7000, if inuse then inc the port

; macros
(cumulo-util.build/get-ip!)
Expand Down
707 changes: 646 additions & 61 deletions calcit.edn

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion meyvn.edn
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

{:pom {:group-id "cumulo",
:artifact-id "util",
:version "0.1.7",
:version "0.1.8-a1",
:name "Util functions for Cumulo projects"}
:packaging {:jar {:enabled true
:remote-repository {:id "clojars"
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@
"shortid": "^2.2.14"
},
"devDependencies": {
"shadow-cljs": "^2.7.9",
"source-map-support": "^0.5.9",
"ws": "^6.1.2"
"shadow-cljs": "^2.8.32",
"source-map-support": "^0.5.12",
"ws": "^6.2.1"
}
}
5 changes: 2 additions & 3 deletions shadow-cljs.edn
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
:repositories {"central" {:url "https://maven.aliyun.com/nexus/content/groups/public/"}
"clojars" {:url "https://mirrors.ustc.edu.cn/clojars/"}}
:open-file-command ["subl" ["%s:%s:%s" :file :line :column]]
:dev-http {7000 "target/"}
:builds {:app {:target :node-script
:output-to "target/app.js"
:main cumulo-util.app/main!
Expand All @@ -13,6 +14,4 @@
:output-dir "target/"
:asset-path "./"
:modules {:client {:init-fn cumulo-util.client/main!}}
:devtools {:after-load cumulo-util.client/reload!
:http-root "target"
:http-port 7000}}}}
:devtools {:after-load cumulo-util.client/reload!}}}}
12 changes: 9 additions & 3 deletions src/cumulo_util/app.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@
(ns cumulo-util.app
(:require [cumulo-util.core :refer [delay!]]
[clojure.core.async :refer [go chan >! <!]]
[cumulo-util.async :refer [all-once]]))
[cumulo-util.async :refer [all-once]]
[cumulo-util.file :refer [chan-pick-port]]
[clojure.core.async :refer [go <! chan]]))

(defn task! []
(defn pick-port! [] (go (let [port (<! (chan-pick-port 6001))] (println "got port" port))))

(defn wait-sleep! []
(go
(let [chan-rand-sleep (fn []
(let [<result (chan), x (rand-int 10)]
Expand All @@ -15,6 +19,8 @@
all-results (<! (all-once chan-rand-sleep (repeat (rand-int 10) true)))]
(println "all results:" all-results))))

(defn main! [] (println "Started") (task!))
(defn task! [] (comment wait-sleep!))

(defn main! [] (println "Started") (task!) (pick-port!))

(defn reload! [] (println "Reload") (task!))
39 changes: 38 additions & 1 deletion src/cumulo_util/file.cljs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,44 @@
(:require ["path" :as path]
["fs" :as fs]
["child_process" :as cp]
[cljs.reader :refer [read-string]]))
["net" :as net]
[cljs.reader :refer [read-string]]
[clojure.core.async :refer [go go-loop >! <! chan]])
(:require-macros [clojure.core.strint :refer [<<]]))

(defn chan-port-taken [port]
(let [<result (chan), tester (.createServer net)]
(.. tester
(once
"error"
(fn [err]
(go
(>!
<result
(if (not= (.-code err) "EADDRINUSE")
{:error err, :data true}
{:error nil, :data true})))))
(once
"listening"
(fn []
(.. tester
(once "close" (fn [] (go (>! <result {:error nil, :data false}))))
(close))))
(listen port))
<result))

(defn chan-pick-port [initial-port]
(let [<result (chan)]
(go-loop
[port initial-port]
(let [taken-info? (<! (chan-port-taken port))]
(cond
(some? (:error taken-info?))
(do (js/console.error (:error taken-info?)) (js/process.exit 1))
(:data taken-info?)
(do (println (<< "port ~{port} is in use.")) (recur (inc port)))
:else (>! <result port))))
<result))

(defn get-backup-path! []
(let [now (js/Date.)]
Expand Down
34 changes: 17 additions & 17 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3f44eb3

Please sign in to comment.