Fork me on GitHub
#babashka
<
2022-10-05
>
rlj11:10:48

Hi there. I'm writing a bb script using the AWS pod. When the script terminates (because of Ctrl-C) I'd like to remove some resources that were made during the script's execution. I do this in a shutdown hook. However, the pod process terminates before I have a chance to call it. I understand that this is because shutdown hooks run in a nondeterministic order. Has anyone found a way around this?

borkdude11:10:53

Interesting... When you have a chance, you could also take a look at https://github.com/grzm/awyeah-api which is a rewrite of Cognitect's AWS API for bb

rlj11:10:20

I will - thanks

Crispin13:10:02

You could try catching SIGINT and doing your cleanup there

Crispin13:10:33

Babashka v0.9.163-SNAPSHOT REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> (sun.misc.Signal/handle
 (sun.misc.Signal. "INT")
 (reify sun.misc.SignalHandler
   (handle [_ _]
     (println "caught sigint")
     ;; do something
     (Thread/sleep 3000)
     (println "Finishing")
     (System/exit 0))))
#object[babashka.impl.sigint_handler$handle_sigint_BANG_$reify__30967 0xb2ffbfb "babashka.impl.sigint_handler$handle_sigint_BANG_$reify__30967@b2ffbfb"]
user=> ^Ccaught sigint
Finishing

rlj13:10:21

I'll try that - thanks

borkdude13:10:59

Oh right, that's now supported too ;)

rlj11:10:36

I'd prefer to use a pod-based solution (I like the self-contained nature of a pod-based script) so I'm pursuing Crispin's suggestion. However, I end up in the same situation. My guess is that bb and the AWS pod receive SIGINT simultaneously, so I get this when I'm attempting to make AWS calls from my signal handler:

Exception in thread "SIGINT handler" java.io.IOException: Stream closed
	at java.lang.ProcessBuilder$NullOutputStream.write(ProcessBuilder.java:442)
	at java.io.OutputStream.write(OutputStream.java:157)
	at java.io.BufferedOutputStream.flushBuffer(BufferedOutputStream.java:81)
	at java.io.BufferedOutputStream.flush(BufferedOutputStream.java:142)
	at babashka.pods.impl$write.invokeStatic(impl.clj:23)
	at babashka.pods.impl$invoke.invokeStatic(impl.clj:109)
	at babashka.pods.impl$bencode__GT_vars$fn__27160$fn__27165.doInvoke(impl.clj:134)
	at clojure.lang.RestFn.invoke(RestFn.java:421)
	at sci.lang.Var.invoke(lang.cljc:182)

borkdude11:10:25

What are the things you want to clean up?

rlj11:10:43

The idea of my script is to 'tail' an SNS topic, by making an SQS queue, subscribing the topic to the queue, doing some processing on the stuff coming off the queue, and then deleting the queue when the user hits Ctrl-C

rlj11:10:16

(and deleting the subscription too)

borkdude11:10:48

Hmm. Java currently doesn't support shutdown hook dependencies

borkdude11:10:09

> (I like the self-contained nature of a pod-based script) you can get this with deps too:

(require '[babashka.deps :as deps])

(deps/add-deps '{:deps {com.cognitect.aws/endpoints {:mvn/version "1.1.12.206"}
                        com.cognitect.aws/s3 {:mvn/version "822.2.1109.0"}
                        com.grzm/awyeah-api {:git/url ""
                                            :git/sha "0fa7dd51f801dba615e317651efda8c597465af6"}}})


(require '[com.grzm.awyeah.client.api :as aws])

;; etc

👍 1
borkdude11:10:22

if you mean: single script

rlj11:10:45

Yes, that's what I mean. I didn't realise you could do that! I'll give awyeah a try, then

rlj11:10:57

Thanks again!

borkdude11:10:37

No problem, let me know how it goes.

rlj14:10:16

It worked beautifully!

borkdude14:10:47

Nice! I'll ping @U0EHU1800 here too since I can imagine he would like to know his library is getting some good use

🎉 1
rlj14:10:14

:thumbsup:

borkdude14:10:01

I'll also put a note at the pods' repository to consider using awyeah-api instead

madstap16:10:48

Hi, I'm trying to decrypt something using the buddy pod, but I'm getting clojure.lang.ExceptionInfo: No matching method init found taking 3 args for class javax.crypto.Cipher This works on clj, but not on bb. In the docs it says that jwk->private-key returns a byte-array and decrypt takes a byte array, so it should theoretically work the same on both if I pass the return of one to the other, right?

(defn- jwk->keys [jwk]
  {:public-key (keys/jwk->public-key jwk),
   :private-key (keys/jwk->private-key jwk)})

(jwt/decrypt encrypted
             (:private-key (jwk->keys jwk))
             {:alg :rsa-oaep, :enc :a128cbc-hs256})

Crispin00:10:45

what is the type of encrypted?

madstap10:10:55

It's a string encrypted with that same key (in clj). I'll make a full repro in a bit (I didn't generate that key so I have to do some digging in the code to see how it was generated).