You could probably move setting the atom and calling my-wait-loop inside
the try.
(defn test-mailer []
(async/thread
(try
(do
(reset! mailer-status
(mailer/send-speaker-confirmation-notification 1 "
http://localhost"))
(my-wait-loop))
(catch Exception e
(println (.getMessage e))))
"Success"))
I also dont see the need for atoms. You could probably pass the channel as
parameter to my-wait-loop. And also you dont need to put async/thread if
the send-speaker-confirmation-notification doesnt wait until the message is
put (which probably it shouldnt).
(defn my-wait-loop [mailer connections]
(async/go-loop [status (async/<! mailer)]
(if status
(do
(println "Ready to send " status)
(doseq [chan connections]
(println "Channel" chan)
(send! chan (pr-str "Done" status)))
(recur (async/<! mailer)))
(println "Go away"))))
(defn test-mailer []
(try
(do
(my-wait-loop (mailer/send-speaker-confirmation-notification 1 "
http://localhost") @connections))
(catch Exception e
(println (.getMessage e))))
"Success")
Thanks,
Uday.
On Sun, Dec 21, 2014 at 9:26 AM, Chris Freeman <[email protected]> wrote:
> I'm a little uncertain exactly what your code is trying to do, but I
> believe you're trying to notify a bunch of connections after your speaker
> notification emails are sent.
>
> In which case, I'd do something like this:
>
> (defn send-notifications []
> (try
> (mailer/send-speaker-confirmation-notification 1 "http://localhost")
> true
> (catch Exception e
> (println (.getMessage e))
> false)))
>
> (defn test-mailer []
> (let [done (async/thread-call send-notifications)]
> (when-let [status (async/<! done)]
> (doseq [chan @connections]
> (async/>! chan (pr-str "Done" status))))))
>
> I've replaced the string "Success" with an explicit true and added an
> explicit false. I'd prefer if send-speaker-confirmation-notification
> returned a truthy value, but I don't know that it does.
>
> In your original, the doseq call was done in a separate thread. If you'd
> still like that, wrap the when-let in a async/go call, as followings:
>
> (defn test-mailer []
> (let [done (async/thread-call send-notifications)]
> (async/go
> (when-let [status (async/<! done)]
> (doseq [chan @connections]
> (async/>! chan (pr-str "Done" status)))))))
>
> But if you do, keep in mind that the main thread will end before the other
> two threads do, and that's probably unhealthy.
>
> Chris
>
> On Sat, Dec 20, 2014 at 9:17 PM, Jonathon McKitrick <[email protected]>
> wrote:
>
>> I'd like to implement a thread that will send an email, then send a
>> response via websocket to the client when the send completes.
>>
>> (defn my-wait-loop []
>> (async/go-loop [status (async/<! @mailer-status)]
>> (if status
>> (do
>> (println "Ready to send " status)
>> (doseq [chan @connections]
>> (println "Channel" chan)
>> (send! chan (pr-str "Done" status)))
>> (recur (async/<! @mailer-status)))
>> (println "Go away"))))
>>
>> (defn test-mailer []
>> ;;(my-wait-loop)
>> (reset! mailer-status
>> (async/thread
>> (try
>> (mailer/send-speaker-confirmation-notification 1 "
>> http://localhost")
>> (catch Exception e
>> (println (.getMessage e))))
>> "Success")))
>>
>> I would like to have the go-loop inside my-wait-loop run at all times,
>> waiting for mailer-status to have a value.
>> But I believe that can never happen, since the go-loop is waiting on an
>> empty channel, and the reset! with the mailer will replace the channel with
>> a new one after the emails are sent.
>>
>> Is there a batter way to do this, without needing to call my-wait-loop
>> before the email thread is dispatched?
>>
>> --
>> You received this message because you are subscribed to the Google
>> Groups "Clojure" group.
>> To post to this group, send email to [email protected]
>> Note that posts from new members are moderated - please be patient with
>> your first post.
>> To unsubscribe from this group, send email to
>> [email protected]
>> For more options, visit this group at
>> http://groups.google.com/group/clojure?hl=en
>> ---
>> You received this message because you are subscribed to the Google Groups
>> "Clojure" group.
>> To unsubscribe from this group and stop receiving emails from it, send an
>> email to [email protected].
>> For more options, visit https://groups.google.com/d/optout.
>>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Clojure" group.
> To post to this group, send email to [email protected]
> Note that posts from new members are moderated - please be patient with
> your first post.
> To unsubscribe from this group, send email to
> [email protected]
> For more options, visit this group at
> http://groups.google.com/group/clojure?hl=en
> ---
> You received this message because you are subscribed to the Google Groups
> "Clojure" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.
>
--
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
---
You received this message because you are subscribed to the Google Groups
"Clojure" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
For more options, visit https://groups.google.com/d/optout.