This page is not created by, affiliated with, or supported by Slack Technologies, Inc.
2020-10-10
Channels
- # announcements (1)
- # babashka (5)
- # beginners (44)
- # calva (45)
- # cider (8)
- # clj-kondo (9)
- # cljdoc (12)
- # clojars (7)
- # clojure (20)
- # clojure-czech (1)
- # clojure-dev (26)
- # clojure-europe (24)
- # clojure-france (8)
- # clojure-uk (1)
- # clojurescript (12)
- # conjure (8)
- # datascript (10)
- # fulcro (7)
- # leiningen (2)
- # malli (19)
- # meander (5)
- # off-topic (113)
- # pathom (3)
- # precept (4)
- # re-frame (13)
- # reagent (19)
- # reitit (3)
- # rewrite-clj (69)
- # shadow-cljs (9)
- # spacemacs (16)
- # tools-deps (1)
- # vim (1)
- # xtdb (10)
;; ----------------------------------------------------------------------------
(defn create-pub-sub-listener
"Creates a listener for pub-sub messages. Borrows a connection
from the pool and doesn't return it until it is shut down."
[pool]
(let [client ^Jedis (.getResource pool)
logic (proxy [JedisPubSub] []
(onMessage [channel message]
(log/info :channel channel :message message)))
executor ^ExecutorService (Executors/newSingleThreadExecutor)]
(.submit
executor
(reify Runnable
(run [_]
(.subscribe client
logic
^{:tag "[Ljava.lang.String;"}
(into-array String [post-notification-channel])))))
{:borrowed-client client
:pub-sub logic
:executor executor}))
;; ----------------------------------------------------------------------------
(defn shutdown-pub-sub-listener!
[pub-sub-listener]
(let [{:keys [borrowed-client pub-sub executor]} pub-sub-listener]
(.shutdownNow executor)
(.unsubscribe pub-sub (into-array String [post-notification-channel]))
(.close borrowed-client)))
yeah, because the try/catch is inside the while it just creates the subscription again after you unsubscribe
(while true
(try
(.subscribe client
logic
^{:tag "[Ljava.lang.String;"}
(into-array String [post-notification-channel]))
(catch InterruptedException e
(throw e))
(catch Exception e
(log/error ::jedis-pub-sub-error e
::action-to-take "retry subscription")))))))
;; ----------------------------------------------------------------------------
(defn create-pub-sub-listener
"Creates a listener for pub-sub messages. Borrows a connection
from the pool and doesn't return it until it is shut down."
[pool]
(let [client ^Jedis (.getResource pool)
logic (proxy [JedisPubSub] []
(onMessage [channel message]
(log/info :channel channel :message message)))
executor ^ExecutorService (Executors/newSingleThreadExecutor)]
(.submit
executor
(reify Runnable
(run [_]
(let [completed (atom false)]
(while (not @completed)
(try
(.subscribe client
logic
^{:tag "[Ljava.lang.String;"}
(into-array String [post-notification-channel]))
(catch Exception e
(log/error ::jedis-pub-sub-error e
::action-to-take "retry subscription"))
(finally
(reset! completed true))))))))
{:borrowed-client client
:pub-sub logic
:executor executor}))
Also if you want the while there you likely need to move the get resource call inside the while, otherwise if the connection dies you'll never get a new one
;; ----------------------------------------------------------------------------
(defn create-pub-sub-listener
"Creates a listener for pub-sub messages. Borrows a connection
from the pool and doesn't return it until it is shut down."
[pool]
(let [client ^Jedis (.getResource pool)
logic (proxy [JedisPubSub] []
(onMessage [channel message]
(log/info :channel channel :message message)))
executor ^ExecutorService (Executors/newSingleThreadExecutor)]
(.submit
executor
(reify Runnable
(run [_]
(let [completed (atom false)]
(while (not @completed)
(try
(do (.subscribe client
logic
^{:tag "[Ljava.lang.String;"}
(into-array String [post-notification-channel]))
(reset! completed true))
(catch Exception e
(log/error ::jedis-pub-sub-error e
::action-to-take "retry subscription"))))))))
{:borrowed-client client
:pub-sub logic
:executor executor}))
like, if i could know that i messed up handling and shut down and restart this component
or if i had it in my dashboard (i'm using heroku for everything right now) then I could manually handle it
I believe when you return a connection to it, it tests it and if it is bad it throws it away and adds a new one to the pool
the structure of this in our code at work looks something like this using core.async:
(async/go-loop []
(async/alt!
exit ([_] ...)
(get-connection redis-pool) ([connection]
(async/alt!
(async/thread
(try
(.subscribe connection ...)
(finally
(close-connection connection)))) ([_] (recur))
exit ([_]
(.unsubscribe connection ...)
(close-connection connection)
....)))))
Are there any libraries (e.g. on top of rewrite-clj) that can merge EDN files and write back to a file, while preserving order / whitespace / comments etc? Maybe I'll have to write my own, but just asking in case there's already one
The use case: I want to write a package manager, but both the user and the package manager should be able to put stuff in an .edn file
I guess npm has it easier since comments aren't allowed in JSON, but still, it seems to preserve some of the user-defined order in maps
order is relatively easy to preserve, but only if you tweaked the reading code to use something like the ordered library https://github.com/clj-commons/ordered as long as you were then also careful about what operations you did on those maps so the ordering wasn't lost (e.g. select-keys I think always returns a built-in Clojure unordered map, so you would need to avoid using that, and/or write your own variant that returned the type of map it was given. The same goes for many other core functions on maps).
preserving white space and/or comments is a special purpose reader that only a few libraries implement, like rewrite-clj.
I suspect it would be less work to take a library that already preserved whitespace and comments, and add preservation of map/set order, than vice versa.
if I would drop the comments/whitespace requirement, ordered maps could work. not sure if I can read ordered maps from plain edn maps or if I'll have to represent them using an ordered coll in .edn with a reader tag, which is kind of ugly
I think it would be a fairly small change to something like tools.reader lib to make it return ordered maps/sets, rather than the core unordered maps/sets.
yeah, I think I could also support it in edamame (which is already built on top of tools.reader)
I haven't dug into rewrite-clj, but perhaps it already maintains order, but perhaps it does so by not returning regular Clojure data structures, but a custom one representing the sequence of 'things' in the input file?
Attempting to preserve white space/comments, and returning data that one can do regular Clojure data manipulation on, sounds like magic.
or writing your own custom implementation of those data manipulation functions on new data structures that contain representations of the white space/comments?
I suspect your deep rewrite-clj knowledge will give you a better implementation here
I guess this could become useful pretty quickly to update deps.edn files, fixing some stuff like unqualified symbols
I wrote it for deps, yah. I have an alternative tag resolver that preserves comments.
Something I handled btw, was detection for that thing people do where they align their keys. That was feedback I got a lot of :)
In one of my commits I accidently deleted a file; how do I tell git that I want this file? I read the instructions for `git ammend` — you basically choose a commit and git will rebase on top of it. However; I’m unsure how to add back a file. This is not an Clojure question; but I didn’t know where else to go;
I probably wouldn't rewrite history if you don't have to. I would probably do something like:
git checkout <commit-where-file-exists> -- <filepath>
and then commit as normalIf you want to be really really safe about it, you can make a second clone of your repository elsewhere, do 'git checkout <commit-where-file exists>' in that copy, copy the file you want into your working repository, and do 'git add <filename>' in your working repository. Then delete the other copy of the repository if you don't need it any more.
Crude, but effective, and harder to mess up your working repo's state
my suggestion works even if the file is two commits backwards and it will keep all your changes
I do not know off hand whether that technique will let you do 'git blame' on that file and see its entire history since it was first created, or not.
my guess is that it won't let you do git blame
all the way backwards
@U7RJTCH6J is giving you solid advice here. If you really want to really want to put your mind at ease, check out the chapter on branching in the pro git book. At the risk of oversimplifying, a branch is just a pointer to a commit. So by checking out an earlier commit, all you’re really doing is pointing to an earlier snapshot of your work. Nothing is being deleted or erased. https://git-scm.com/book/en/v2
the command I gave doesn't actually do a full checkout of the whole commit. it only gets the version of <filename>
from that commit and stages it
git checkout
is overloaded and can do a bunch of different things
So this git checkout 183ca537 -- apps/admin_web/lib/admin_web/templates/layout/_sidebar.html.eex
for reference, the fancy way to fix your issue and preserve git blame
is to use git interactive rebase, but I wouldn't recommend doing that unless you feel confident in your git
fu
the git-scm
book that @U01BTEYDC05 linked is really good and I would recommend reading it even if you don't use git
if you're the only one working on the repo, that should not have required a force push
since it only added a new commit
force pushing is almost never required
well, the good news is that someone else likely has the commits that were overwritten and it's likely they can recover them
@U013HT66AG4 check "git reflog" on internet, you will get your previous branches back. They are still in your repository.
Anyway, thank you all for all your help. This was very informative for me. I will not use git commands without knowing exactly what they do 🙂
Shooting yourself in the foot with git is a software engineering right of passage. This Summer I accidentally nuked all of my Intern’s work one week before he was to present it. I was up until midnight on a Friday reading git documentation. I was eventually able to recover it after about 5 tense hours. Likewise, I no longer copy/paste git commands without understanding what’s going on under the hood.
Fortunately it happened late on a Friday so everyone was already signed off for the weekend. I sent an email that there was an “issue” I was working to resolve and left it at that. After I fixed it, I told them the whole story on Monday and everyone had a good laugh at my expense.
@dominicm Fully qualify dep symbols in deps.edn: https://github.com/borkdude/rewrite-edn#examples
Also added example of adding dependency: https://github.com/borkdude/rewrite-edn#add-dependency