grzm / awyeah-api

Cognitect's aws-api for babashka
Apache License 2.0
89 stars 3 forks source link

Auto-refresh credentials #11

Closed grzm closed 10 months ago

grzm commented 10 months ago

aws-api supports caching and auto-refreshing credentials:

https://github.com/cognitect-labs/aws-api/blob/7352ec647ff19f6a14cbbfc5e581c48a8183ebf1/src/cognitect/aws/credentials.clj#L76-L103

awyeah-api does not currently support this feature as babashka did not include the requisite Java classes at the time of initial development, namely:

Babashka added support for some of these classes:

So, as of now it looks like we're only missing java.util.concurrent.ScheduledFuture to support refreshable creds in in the same manner as aws-api.

borkdude commented 10 months ago

Class added. Master version can be tested with:

bash <(curl https://raw.githubusercontent.com/babashka/babashka/master/install) --dev-build --dir /tmp
grzm commented 10 months ago

Thanks, @borkdude ! For what it's worth, testing prompted by @lispyclouds 's suggestion in slack with a shipping version of babashka (v1.3.181) confirms I can close the result of ScheduledExecutorService.schedule out of the box. I haven't confirmed whether any reflection warning is generated.

% bb
Babashka v1.3.181 REPL.
Use :repl/quit or :repl/exit to quit the REPL.
Clojure rocks, Bash reaches.

user=> (import (java.util.concurrent Executors))
java.util.concurrent.Executors
user=> (import (java.util.concurrent ThreadFactory))
java.util.concurrent.ThreadFactory
user=> (defonce ^:private scheduled-executor-service
  (delay
   (Executors/newScheduledThreadPool 1 (reify ThreadFactory
                                         (newThread [_ r]
                                           (doto (Thread. r)
                                             (.setName "cognitect.aws-api.credentials-provider")
                                             (.setDaemon true)))))))
#'user/scheduled-executor-service
user=> (import (java.util.concurrent TimeUnit))   
java.util.concurrent.TimeUnit
user=> (def f (.schedule @scheduled-executor-service (fn [] (prn :yup)) 5 TimeUnit/SECONDS))
#'user/f
user=> :yup

f
#object[java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask 0x4f0d4632 "java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask@4f0d4632[Completed normally]"]
user=> (.cancel f true)
false
user=> (def f (.schedule @scheduled-executor-service (fn [] (prn :yup)) 5 TimeUnit/SECONDS))
#'user/f
user=> (.cancel f true)
true
grzm commented 10 months ago

Implemented in https://github.com/grzm/awyeah-api/commit/5ecad021b599bce69296fccd9afc6cc49b20ab7d