Fork me on GitHub
#beginners
<
2021-10-31
>
Benjamin07:10:36

What are ways to securely be able to "repl into" my app in production?

jumar08:10:56

Setup a Socket repl for your app listening on localhost, ssh into the prod machine, then nc localhost &lt;port&gt;

👀 1
GGfpc14:10:44

Is it normal for a clojure program to take 30s to 1 minute to start?

delaguardo15:10:16

Startup time has a strong dependency on the program itself. It might be normal if it require some preparations like connecting to external systems and collecting some information.

GGfpc15:10:54

I'm just trying to launch a repl. Even using integrant and assuming it's connecting to a local postgres and kafka before it lets me type in the repl, this seems like a strange amount of time

Ben Sless15:10:20

Are you using leinigen? Do you have a large class path? What JVM opts did you start with?

GGfpc15:10:16

Using leinigen through intelliJ, JVM opts are

"-Dclojure.spec.check-asserts=true"
Have 50 dependencies in profile.clj, not sure if that is considered a lot

uneman15:10:37

is there a way to enumerate all namespaces for my project? in other words, can I make (ns-all) work only for :paths, not for :deps?

Bob B16:10:12

I imagine you could probably filter the output of (all-ns) with str/starts-with? (once you get the names) or something like that, but I think that by the point you're calling (all-ns), Clojure is basically only aware of 'the classpath' as a thing, with no distinction between what was contributed to 'the classpath' via :paths vs :deps - I could totally be wrong, though

uneman23:10:14

I think filter by starts-with is the only way for now. I’ll try that. Thanks!

Vettukal Vincent Mathew17:10:48

Hi everyone, I am new to clojure and was trying out next-jdbc

(jdbc/execute-one! ds ["
insert into address(name,email)
  values('Someone Else','')
"])
returns
#:next.jdbc{:update-count 1}
I was wondering how to get just the map of {:update-count 1} out of this. Also {:builder-fn rs/as-unqualified-lower-maps} works for select query but not insert.

dpsutton17:10:14

(get #:next.jdbc{:update-count 1} :next.jdbc/update-count) #_ -> 1

dpsutton17:10:54

(= #:next.jdbc{:update-count 1} {:next.jdbc/update-count 1}) is true. That prefix on the map is just a shorthand for indicating all of the keys in the map have that namespace. And you can turn it off if you like:

(set! *print-namespace-maps* false)
false
user=> #:next.jdbc{:update-count 1}
{:next.jdbc/update-count 1}

🙏 1
Vettukal Vincent Mathew17:10:34

Thanks @dpsutton that did work. (= #:next.jdbc{:update-count 1} {:next.jdbc/update-count 1}) That does explain whats happening here. Never saw namespace-maps while studying any of the clojure books and got confused.

👍 2
dharrigan17:10:00

If you want, you could an also destructure it, i.e., (let [{:next.jdbc/keys [update-count]} (jdbc/execute-one! ds .......)] (println "number of rows updated " update-count))

🙌 1
dharrigan17:10:20

More info here

Zach Mitchell18:10:27

I have a Clojure API design question. I have a function foo that takes an integer n and returns a map. It is incorrect to call this function with n < N. So, my question is what is the idiomatic way to handle when someone calls foo with n < N? Raise an exception? Return nil? Try to catch n < N beforehand so that foo doesn't have to worry about it?

R.A. Porter18:10:31

There are several ways of solving that. You could use a :pre condition (https://clojure.org/reference/special_forms#toc10), or an fdef (https://clojuredocs.org/clojure.spec.alpha/fdef) or hand-code an assertion check of your own if you want to have more control over the behavior of the fn when called with semantically incorrect input.

dorab18:10:58

I'm not sure what is considered idiomatic, but my experience is that Clojure generally considers the response to invalid input to be "undefined" (meaning, "all bets are off"), unless specifically stated. In your specific example and without knowing more about the context, I'd probably just return nil and mention that behavior in the docstring.

dpsutton19:10:43

This is always a “your system” type of question. What do you think should happen? Is this bad but innocent data? Failure to validate human input? A broken invariant that absolutely should not be able to happen? Recoverable? Fatal? Amusing?

☝️ 2
Shmarobes18:10:37

Hello everyone! Are there any good tools that make clojure's stack traces more readable? Do you personally use such tools or is there a reason not to? Other languages seem to do a much better job at telling you what went wrong. I've seen some people say it's something you learn to work with, which I find hard to believe because usually the stack trace is a lot more cryptic than "can't cast one class to another". Sometimes I think that a better error message could have saved me a lot of time. Thanks in advance.

1
phronmophobic18:10:34

This blog post gives some decent advice, https://8thlight.com/blog/connor-mendenhall/2014/09/12/clojure-stacktraces.html. What IDE are you using? There might be some more specific tools that are available depending on your setup.

👍 1
Shmarobes18:10:57

@U7RJTCH6J Thank you. I'm using neovim as my IDE.

phronmophobic18:10:20

I'm a weirdo that has never been bothered by the stack traces. I think some mitigations clojure has to make stack traces less troublesome: • easier to build programs in small pieces and then compose the pieces to make larger programs • repl driven development for tighter feedback loops If you haven't tried repl-driven development, I highly recommend it. It accentuates clojure's strengths and minimizes its weaknesses.

phronmophobic18:10:43

Unfortunately, I'm unfamiliar with neovim. I'm not sure what options might be available to help with stack traces.

Shmarobes18:10:56

Yes, I'm using vim-iced for interacting with REPL and it is awesome. Thanks for the advice!

phronmophobic18:10:59

My usual work flow is similar to what's recommended in the blog post. Find the stack trace that points to a line in my current namespace and work from there.

amarnah19:10:18

I came across https://github.com/athos/stacktracer recently. Haven’t tried it, but it looks nice

👍 1
dpsutton19:10:23

Do you have a stack trace that you can put in here and we can look at it together?

Shmarobes20:10:11

@dpsutton It's not really a problem with any particular stack trace, but rather me not being used to reading clojure stack traces. There was a particularly annoying one today, though. I was going to post it here, but while copying noticed that the reason was stated in plain english 🙂 It was somewhat buried, but I still feel silly. I am reading the article that @U7RJTCH6J suggested and it seems that it really is a question of getting used to it. Thanks for being so ready to help though. I'm glad I've discovered clojurians.

dpsutton20:10:13

I think that’s the crux of it. It’s easy for your eyes to gloss over at first, but if you take a breath the stack trace will point exactly to the offending code and often has a very useful message

Shmarobes20:10:35

Yep, I guess I'm just more used to reading shorter error messages in other languages. But I'm going to practice then.

👍 1
dpsutton20:10:23

If you do find yourself confused after a minute of reading drop it here in a snippet (so it’s collapsing) and ask for help

phronmophobic20:10:36

I’m sure there’s still room for improvement as it’s certainly not just you. I think there has been quite a bit of improvement in clojure core, but I think the tools that surface the stack traces are still integrating the newer/better info that’s now available.

👍 1
Shmarobes20:10:44

Okay, will do

Shmarobes20:10:49

Yes, I've read that the situation has improved since the release. It must not be so easy to implement into the core language.

Lukas20:10:30

Hey, when building an uber jar with tools.build the build command clj -T:build uber also starts the program. Is there a flag or something for just building?

Lukas20:10:43

I don't know if this is needed as context

(defn uber [_]
  (clean nil)
  (b/write-pom {:class-dir class-dir
                :lib lib
                :version version
                :basis basis
                :src-dirs ["src"]})
  (b/copy-dir {:src-dirs ["src" "resources"]
               :target-dir class-dir})
  (b/compile-clj {:basis basis
                  :src-dirs ["src"]
                  :class-dir class-dir})
  (b/uber {:class-dir class-dir
           :uber-file uber-file
           :basis basis
           :manifest {"Main-Class" "lhrb.wdylt.server"}}))

dpsutton20:10:07

Do you require any namespaces from your program in build.clj?

phronmophobic20:10:22

Are there any top level forms that have side effects other than defining constants or functions?

Lukas20:10:31

not that Im aware of

(ns build
  (:require [clojure.tools.build.api :as b]))

(def lib 'lhrb/wdylt)
(def version (format "1.0.0"))
(def class-dir "target/classes")
(def basis (b/create-basis {:project "deps.edn"}))
(def uber-file (format "target/wdylt.jar"))

(defn clean [_]
  (b/delete {:path "target"}))


;; build with clj -T:build uber
(defn uber [_]
  (clean nil)
  (b/write-pom {:class-dir class-dir
                :lib lib
                :version version
                :basis basis
                :src-dirs ["src"]})
  (b/copy-dir {:src-dirs ["src" "resources"]
               :target-dir class-dir})
  (b/compile-clj {:basis basis
                  :src-dirs ["src"]
                  :class-dir class-dir})
  (b/uber {:class-dir class-dir
           :uber-file uber-file
           :basis basis
           :manifest {"Main-Class" "lhrb.wdylt.server"}}))

Lukas20:10:06

:thinking_face:

Lukas20:10:09

:manifest {"Main-Class" "lhrb.wdylt.server"} I copied this from one of the test cases everything else is from the offical guide

dpsutton20:10:20

if you comment out all of the forms in your uber function so it does absolutely nothing, does it still run your program when running clj -T:build uber?

Lukas20:10:33

let me check

Lukas20:10:16

no it does not

Lukas20:10:09

(b/compile-clj {:basis basis
                  :src-dirs ["src"]
                  :class-dir class-dir})
this seems to run the program

Lukas20:10:53

I use (gen-class) in this namespace

dpsutton20:10:30

that's likely not the problem. Compiling is reading the clojure source files and writing the bytecode to classfiles. You have some top level def that does work rather than can be invoked to do work

Lukas20:10:17

such as this one

(def server (jetty/run-jetty #'app {:port 3000
                                    :join? false}))

phronmophobic20:10:21

what about in the lhrb.wdylt.server namespace or its requires?

Lukas20:10:02

this is my main ns

Lukas20:10:09

with the main method inside

dpsutton20:10:13

if you eval that form you don't have a function that can create a webserver you are running a webserver

Lukas20:10:57

tbh I don't know yet how to eval forms in the build.clj file 😇

Lukas20:10:18

I was just reactivating one form after another and run clj -T:build uber

dpsutton20:10:22

clj -A:build and then just load the file

dpsutton20:10:41

or eval the namespace form and each of the other forms you need (which is exactly what load-file does)

dpsutton20:10:55

usually the important bits are just to have tools.build on the classpath and then you can just eval that file as you like. Note it is not on the classpath normally unless your classpath has . on it (the root directory in addition to just src/ or whatever

Lukas20:10:09

guys sry my knowledge around tooling is limited

Lukas20:10:12

I use cider

Lukas20:10:42

and when I jack-in and try to eval something in the build file i get a filenotfoundException

Lukas20:10:48

1. Unhandled java.io.FileNotFoundException
   Could not locate clojure/tools/build/api__init.class,
   clojure/tools/build/api.clj or clojure/tools/build/api.cljc on classpath.

dpsutton20:10:52

For these purposes, build is just another alias that includes an extra dependency tools.build and specifies to use the build namespace

dpsutton20:10:00

that means you don't have tools.build on your classpath

dpsutton20:10:05

so you cannot use the dependency

Lukas20:10:23

:aliases {:build {:deps {io.github.clojure/tools.build {:git/tag "v0.6.2" :git/sha "226fb52"}}
                   :ns-default build}}}
I have this in my deps file

dpsutton20:10:36

and did you start your repl with the build alias included?

Lukas20:10:38

:paths ["src" "resources"]
these are the configured paths

dpsutton20:10:39

that's not automatic

Lukas20:10:52

okay I did not

Lukas20:10:17

do you have any resource where I can look up how to include a namespace with cider, when starting a repl?

phronmophobic20:10:58

if you do C-u M-x cider-jack-in, then it let's you edit the command

Lukas20:10:22

Where would I put the build alias?

:aliases {:cider/nrepl {:main-opts ["-m" "nrepl.cmdline" "--middleware" "[refactor-nrepl.middleware/wrap-refactor,cider.nrepl/cider-middleware]"]}}

dpsutton20:10:02

there should be a whole command there. with the beginning /usr/bin/clojure blah blah

phronmophobic20:10:04

or you can edit the cider-clojure-cli-parameters emacs variable, but I forget how to do that as a one off

Lukas20:10:46

yes it is like this

/usr/bin/clojure -Sdeps '{:deps {nrepl/nrepl {:mvn/version "0.9.0-beta2"} refactor-nrepl/refactor-nrepl {:mvn/version "3.0.0-alpha13"} cider/cider-nrepl {:mvn/version "0.26.0"}} :aliases {:cider/nrepl {:main-opts ["-m" "nrepl.cmdline" "--middleware" "[refactor-nrepl.middleware/wrap-refactor,cider.nrepl/cider-middleware]"]}}}' -M:cider/nrepl

dpsutton20:10:05

change it to -M:build:cider/nrepl

Lukas20:10:05

ty guys this worked

phronmophobic20:10:40

probably wouldn't* be that hard to add a command to your emacs config that starts a build repl

phronmophobic20:10:47

I should probably do that for my setup

Lukas20:10:39

Guys thank you so much. I figured it out. It was actually the (def server ...) form which led to the behavior

Lukas20:10:58

Damn I was wrong it still starts the program in background :thinking_face: this is odd isn't it

dpsutton21:10:27

did you remove the use of (def server ...)?

dpsutton21:10:24

what did you replace it with?

Lukas21:10:46

I put it into the-main fn as let

Lukas21:10:50

(defn -main []
  (let [server (jetty/run-jetty #'app {:port 3000
                                       :join? false})]
   (log/error "damn this gone horrible wrong")
   (schema/create-db-structure ds)
   (start server)
   (.. (Runtime/getRuntime)
       (addShutdownHook (proxy [Thread] []
                          (run []
                            (log/error "shutdown hook")
                            (.stop server)
                            (System/exit 0)))))))

Lukas21:10:20

the build does not start the server

Lukas21:10:28

but it starts the program for some reason

Lukas21:10:36

clj -T:build uber
21:59:00.378 [main] DEBUG org.eclipse.jetty.util.log - Logging to Logger[org.eclipse.jetty.util.log] via org.eclipse.jetty.util.log.Slf4jLog
21:59:00.384 [main] INFO org.eclipse.jetty.util.log - Logging initialized @1815ms to org.eclipse.jetty.util.log.Slf4jLog
WARNING: set already refers to: #'clojure.core/set in namespace: lhrb.wdylt.server, being replaced by: #'honey.sql.helpers/set
WARNING: into already refers to: #'clojure.core/into in namespace: lhrb.wdylt.server, being replaced by: #'honey.sql.helpers/into
WARNING: group-by already refers to: #'clojure.core/group-by in namespace: lhrb.wdylt.server, being replaced by: #'honey.sql.helpers/group-by
WARNING: update already refers to: #'clojure.core/update in namespace: lhrb.wdylt.server, being replaced by: #'honey.sql.helpers/update
WARNING: partition-by already refers to: #'clojure.core/partition-by in namespace: lhrb.wdylt.server, being replaced by: #'honey.sql.helpers/partition-by
WARNING: for already refers to: #'clojure.core/for in namespace: lhrb.wdylt.server, being replaced by: #'honey.sql.helpers/for
WARNING: filter already refers to: #'clojure.core/filter in namespace: lhrb.wdylt.server, being replaced by: #'honey.sql.helpers/filter

Lukas21:10:09

(ns lhrb.wdylt.server
    (:require [ring.adapter.jetty :as jetty]
              [ring.middleware.params :as params]
              [reitit.ring.middleware.muuntaja :as muuntaja]
              [muuntaja.core :as m]
              [reitit.ring.coercion :as coercion]
              [reitit.ring :as ring]
              [hiccup.core :as html]
              [clojure.spec.alpha :as s]
              [next.jdbc :as jdbc]
              [honey.sql :as sql]
              [honey.sql.helpers :refer :all]
              [lhrb.wdylt.migration :as schema]
              [clojure.tools.logging :as log])
    (:gen-class))

dpsutton21:10:09

do you see anything else? those are all warnings from compiling code. and initializing a logger isn't crazy

phronmophobic21:10:29

building is going to load the main namespace and its requires which will evaluate all of the top level forms

Lukas21:10:18

ah right okay nvm sry guys

Lukas21:10:35

I guess it was just a zombie process which I saw than

dpsutton21:10:16

don't know how to read the monkey ha. is everything working as you expect or still having issues?

Lukas21:10:46

It is now working as expected

Lukas21:10:13

Thanks a lot for helping me out

👍 1