Fork me on GitHub
#biff
<
2023-12-08
>
macrobartfast03:12:54

So, a message from Mr. Never Going To Get It. I ask variants of this every quarter. It's on my calendar. So let's do it!

;; This function should only be used from the REPL. Regular application code
;; should receive the system map from the parent Biff component. For example,
;; the use-jetty component merges the system map into incoming Ring requests.
(defn get-context []
  (biff/assoc-db @main/system))
So, the example is for the REPL. Another scenario would be for, as mentioned, ring requests. However, if I have a dedicated ns called get-the-things.clj which simply is a function on a scheduler which fetches data from, say, an api and stuffs it into xtdb, what would be the 'parent component' from which I should get the system map? Similarly, I may have a scheduled task to run through the fetched data in the db to find Interesting Things and send notifications or whatnot. 😳

Jacob O'Bryant03:12:29

scheduled functions also receive the system map as a parameter, similar to ring handlers. the parent component is use-chime: https://github.com/jacobobryant/biff/blob/d29db7ac9c6b7c39890b6d6cdd0545336ade0f0a/src/com/biffweb/impl/misc.clj#L124 see also the example scheduled task, which takes :biff/db from the system map: https://github.com/jacobobryant/biff/blob/d29db7ac9c6b7c39890b6d6cdd0545336ade0f0a/example/src/com/example/worker.clj#L9

macrobartfast03:12:20

Ah, sweet. And I should just try it (I will shortly) and not ask... but does the following approach work as well for transactions as queries:

(defn print-usage [{:keys [biff/db]}]
  ;; For a real app, you can have this run once per day and send you the output
  ;; in an email.
  (let [n-users (nth (q db
                        '{:find (count user)
                          :where [[user :user/email]]})
                     0
                     0)]
    (log/info "There are" n-users "users.")))

macrobartfast03:12:51

I remember having to something different for transactions, but I may be wrong.

macrobartfast03:12:04

Jacob O'Bryant is typing I'm so excited.

🎅 1
Jacob O'Bryant03:12:33

yep, it works--biff/submit-tx takes the entire system map as a parameter, so you can do (defn some-task [{:keys [biff/db] :as ctx}] ... (biff/submit-tx ctx ...))

macrobartfast05:12:31

After foraging around (defn use-chime and https://biffweb.com/api/com.biffweb.html#var-use-chime and https://github.com/jarohen/chime I'm still a little unclear how one would daily call, say, print-usage in https://github.com/jacobobryant/biff/blob/d29db7ac9c6b7c39890b6d6cdd0545336ade0f0a/example/src/com/example/worker.clj... apologies for going on... I tend to stop asking before I truly get something working (out of bashfulness).

Jacob O'Bryant06:12:47

no worries! you just need to set the :schedule key to something else. e.g. the print-usage task uses an every-minute function. you could change that to every-day, and change the ( 5 60) bit to ( 60 60 24). (that function should actually be called every-5-minutes--I need to update that)

Olav Fosse09:12:41

What is the motivation for having repl.clj instead of user.clj?

Olav Fosse01:12:52

Might be worth looking into renaming. CIDER and FlowStorm assume user.clj.

Jacob O'Bryant01:12:30

I'm most likely down to rename. What exactly do you mean by them assuming user.clj? Do they open an interactive prompt that starts in user.clj? (I always just eval things from my editor, so there's no starting namespace)

john-shaffer01:12:26

it's generally discouraged to use user.clj because of a variety of issues that people run into with tooling. One issue is mentioned https://www.karimarttila.fi/clojure/2023/03/14/creating-uberjar-for-clojure-fullstack-app.html#userclj-considered-harmful, but I ran into a different problem which I think had to do with how calva loads things. It's also pretty annoying to try to run an alias with its own deps and have it fail because of a user.clj

👀 1
john-shaffer01:12:56

You can work around with an alias like in that blog post, but I've found it easier to just set the namespace to load in the tooling options. You have to configure the tooling anyways if you use the alias, so it's one less step. But either way works

Olav Fosse10:12:15

> What exactly do you mean by them assuming user.clj? cider’s “open repl buffer” command opens a repl with namespace user, regardless of the user’s current buffer (If you prefix it with C-u it uses the current buffer) FlowStorm has a feature where you can capture variables and it defaults to defining them in user.

Olav Fosse10:12:00

but yeah , if it breaks ppls workflows it’s prolly not a great change to make 😅

Jacob O'Bryant11:12:48

👍 you can always move the repl.clj file to src/user.clj in your own project by the way (maybe you already have)

👍 1
Olav Fosse11:12:11

Oh, for sure, just looking for ways to improve the project :^)

🙌 1
🎅 1