missionary

awb99 2024-07-27T18:40:05.832019Z

I have a missionary flow that needs to do run a missionary task when the flow gets Cancelled. But this seems not to be possible, as each time this flows gets cancelled I get exceptions on the m/?.

awb99 2024-07-27T18:40:27.477219Z

This is the code:

awb99 2024-07-27T18:40:30.741079Z

ns quanta.market.quote.current (:require [taoensso.timbre :as timbre :refer [debug info warn error]] [missionary.core :as m] [quanta.market.protocol :as p] [quanta.market.util :as util]) (:import [missionary Cancelled])) (defonce subscriptions (atom {})) (def lock (m/sem)) (defn subscribing-unsubscribing-quote-flow [qm sub] (info "get-quote will start a new subscription..") (let [q (p/last-trade-flow qm sub)] (m/? (p/subscribe-last-trade! qm sub)) (util/cont (m/ap (try (m/amb (m/?> q)) (catch Cancelled _ (do (info "get-quote will stop an existing subscription..") (m/? (p/unsubscribe-last-trade! qm sub)) (info "get-quote has unsubscribed. now removing from atom..") (m/holding lock (swap! subscriptions dissoc sub))))))))) (defn get-quote [qm sub] (or (get @subscriptions sub) (m/holding lock (let [qs (subscribing-unsubscribing-quote-flow qm sub)] (swap! subscriptions assoc sub qs) qs))))

awb99 2024-07-27T18:41:21.115819Z

I did make two tests:

awb99 2024-07-27T18:41:40.505299Z

1. Run subscribe as a standalone task

awb99 2024-07-27T18:42:25.847159Z

The standalone task works.

awb99 2024-07-27T18:43:24.695299Z

2. Trying to use the flow I posted above which fails: https://github.com/clojure-quant/quanta-market/blob/main/demo/src/demo/qm.clj

awb99 2024-07-27T18:45:10.991809Z

The issue is that I am using m/? Inside a (catch Cancelled) Block of a flow that us getting cancelled.

awb99 2024-07-27T18:47:00.785909Z

I believe that this is a valid scenario: a flow that does resource cleanup on cancelation. And that the cleanup is not 100% synchronous can also happen I would think.

awb99 2024-07-27T18:47:43.745859Z

Any ideas?

Vincent 2024-07-27T20:53:41.108029Z

isn't the exception what you want to signal to you that the flow was canceled?

awb99 2024-07-27T21:50:18.929079Z

I found the solution! If I want to m/? Tasks in a cancelled flow . Then I have to wrap (m/compel task. Now it works

awb99 2024-07-27T21:52:02.350519Z

@v1nc3ntpull1ng the cancel exceptions is what I need to Catch, the problem was to "await" a task that needs to run on cancelation of the flow.

Vincent 2024-07-28T00:25:54.084179Z

Oh cool!