Cool! Btw. there's no built-in alias for babashka.process?
There isn’t
If anyone here is using babashka on the cloud / serverless, you might want to reply here: https://www.reddit.com/r/Clojure/comments/k8fc5s/askreddit_have_you_tried_to_run_babashka_on_a/
It definitely would be more easy to do than to convert your clojure web app to be graalvm compatible
with latest Babashka, using babashka.process/process
, setting any :env
options map is causing command to fail
warnings can be silenced by the --no-warnings (-n) option
Assertion failed: (str1!= NULL), function add3strings, file string_utils.c, line 103.
not sure if I’m doing anything wrong, but only way script is working is by commenting out the env options. even an empty map causes that errorthat doesn't sound good, let me check
@alidcastano I can't repro that on my machine:
user=> (slurp (:out @(babashka.process/process ["ls"] {:env {"FOO" "BAR"}})))
what is your repro and which OS? latest = 0.2.4?Also please specify which binary you have downloaded
hm this works for me, so may be a mix of options I’m passing. testing that out now
a standalone minimal repro would be most helpful.
sorry it took a bit, was out for lunch
i’m on macos btw, bb version 0.2.4
I can repro now, thanks
I think it's related to rlwrap
perhaps.
This works:
@(proc/process
["clj"]
{:inherit true
:env (merge (into {} (System/getenv)) {"FOO" "BAR"})
:shutdown proc/destroy})
I can reproduce the problem with this code:
(let [pb
(java.lang.ProcessBuilder.
["clj"])]
(.clear (.environment pb))
(.inheritIO pb)
(let [p (.start pb)]
(.waitFor p)))
It doesn't matter if you run this with bb or clojure
, you get the same error, which I think is coming from rlwrapNot sure why though.
@alidcastano I found out by experimentation that rlwrap apparently expects TERM
, HOME
and PATH
to be set correctly
hm should those properties be auto-included in env by babashka.process? or you think it’s better for user to explicitly set them?
thanks for finding source of error, btw.
This isn't a bb or process specific problem. rlwrap could maybe give better errors when these env vars aren't set
Why are you invoking clj
and not clojure
btw?
Seems like this should have been fixed with this: https://github.com/hanslub42/rlwrap/pull/80
> Why are you invoking `clj` and not `clojure` btw? bad habit, i tend to forget difference and end up using shorter version. but error does seem to happen for both commands
hmyeah, that needs at least PATH and HOME it seems. clojure
is a bash script which needs that
Why not pass through all env vars?
before using babashka process I was setting env vars like this, btw, which was not causing this error
(defn- set-process-env-vars
"Set env vars on process builder environment object."
[pb env-map]
(let [env-obj (.environment pb)]
(doseq [[k v] env-map]
(.put env-obj (-> k name str) v))
pb))
> Why not pass through all env vars? did not consider it. was mainly just passing the ones i knew i needed in my program
ok, we could document this better. So the preferred way when adding things to the current env, would be this:
:env (assoc (into {} (System/getenv)) "FOO" "BAR")
yep doing something like that now - thanks for helping to track down issue!
:thumbsup:
@alidcastano btw, your shutdown hook should be something like proc/destroy
or proc/destroy-tree
since the hook will receive a map
Hi! To patrice, I converting this following bash script: https://gist.github.com/mg6/7321d66ac93d9550322afd8aeec2789a
It's still a draft, I want to refactor a little bit.
Is it doseq
the best candidate for looping and print out in this case?
The Babashka version: https://gist.github.com/PrestanceDesign/7d5b8b577932f17660cdaf5b902e409c
Looks good to me!
Ok great, thank you @borkdude ! I'll create a PR to add in BB's examples dir
Cool. Can you use [skip ci]
in your commit message? Then CircleCI won't get triggered. This is good practice when adding docs/examples.
You might also want to do some error handling because on my macOS system the script doesn't work as expected.
Also a description what the script is for would be nice, since it isn't entirely clear to me who and why this is for
@borkdude curious what the error is on your machine?
If you look at the error message returned from clojure shell:
p\nApple specific options (to be specified before mcast-group or host like all options)\n -b boundif # bind the socket to the interface\n -k traffic_class # set traffic class socket option\n -K net_service_type # set traffic class socket options\n -apple-connect # call connect(2) in the socket\n -apple-time # display current time\n"
"<http://speedtest-blr1.digitalocean.com|speedtest-blr1.digitalocean.com> => ms"
One minor mistake I notice is that it should be (shell/sh "ping" "-c" "5" "-w3" endpoint)
Sounds like maybe -w is a gnuism?
That doesn't fix it for me:
(defn ping-result [endpoint]
(let [{:keys [out err]} (shell/sh "ping" "-c" "5" "-w3" endpoint)]
(prn err)
(prn (str endpoint " => " (get-average out) "ms"))))
Not listed here: https://nixdoc.net/man-pages/FreeBSD/man8/ping.8.html So I guess it's a gnuism.
Right, that's a separate "it's wrong", but the impl of some pings may be lenient to it. (gnu's is)
@borkdude BSD uses -t
instead of -w
. See if that works?
bb ping, cross-platform ping? :P
Yes, of course. In two words, the script allows you to define which DigitalOcean cloud datacenter (region) had best ping latency to take a server (droplet) on. Little bit, a cli version of this page: https://cloudpingtest.com/digital_ocean
#!/usr/bin/env bb
;; explicit requires
(require '[babashka.curl :as curl]
'[babashka.process :as p]
'[clojure.string :as str])
(def url "<http://speedtest-ams2.digitalocean.com/>")
(def get-endpoints
(let [{:keys [body]} (curl/get url)]
(re-seq #"speedtest\-.+.<http://digitalocean.com|digitalocean.com>" body)))
(defn get-average [result]
(-> result
str/split-lines
last
(str/split #"/")
(get 4)))
(def mac?
(str/starts-with? (System/getProperty "os.name") "Mac"))
;; TODO: test on Windows
(def timeout-arg (if mac? "-t3" "-w3"))
(defn ping-result [endpoint]
(let [out (-> (p/process ["ping" "-c" "5" timeout-arg endpoint]
{:out :string})
p/check ;; crash on non-zero exit code with error output
:out)
msg (str endpoint " => " (get-average out) "ms")]
(println msg)))
(doseq [endpoint get-endpoints]
(ping-result endpoint))
Hi @borkdude!
I have a question. I saw in your script version that you required explictily babashka.curl
and clojure.string
.
I thought that alias are already present in some of last bb versions, right?
Do you recommanded to always manually required?
Are you using clj-kondo?
Clj-kondo will give warnings if you use a namespace alias without a prior require.
This is why I advice to always use explicit requires. I think it is also more readable. The built-in aliases are for command line one-liners. See https://book.babashka.org/#style
Yes I use clj-kindo with Flycheck in my Spacemacs conf
Right, so when adding explicit requires these warnings go away
The script will work either way, it's just a style thing
OK, I understand, thanks! I also prefer when my linting is clean so now I go for the explicit requires 👍
Perfect!
^ some feedback @admin055
nice! you could include that in the README
I can test this on Windows too, but not tonight :)
Here they list the args that Windows accepts: https://www.howtogeek.com/wp-content/uploads/2018/06/x2018-06-20-8.png.pagespeed.gp+jp+jw+pj+ws+js+rj+rp+rw+ri+cp+md.ic.hG3nPp3yId.png https://www.howtogeek.com/355664/how-to-use-ping-to-test-your-network/
I just tested your updated version (with process) and I had this error
----- Error --------------------------------------------------------------------
Type: clojure.lang.ExceptionInfo
Message:
Location: /home/mike/Lab/Clojure/Babashka/digitalocean-ping.clj:25:13
----- Locals -------------------------------------------------------------------
endpoint: "<http://speedtest-ams2.digitalocean.com|speedtest-ams2.digitalocean.com>"
I have removed :err :inherit
from the above example, did you copy that?
Yes. Same error with the modified version.
What's the return code of ping when you execute ping from the command line?
Feel free to change back to clojure shell, this is just a way to trigger an error when there's something suspicious
OK I understand. 👍 FYI, the return code is 1
I'm on Linux Unbuntu.
If ping does not receive any reply packets at all it will exit with code 1. If a packet count and deadline are both specified, and fewer than count packets are received by the time the deadline has arrived, it will also exit with code 1. ... Otherwise it exits with code 0.
Maybe that's the case?Thank you for everything and your availability! I'll continue tomorrow.
Same here. Goodnight.
🙏
Thx, goodnight. 🙂
ah ok, thanks for catching that