Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make fire-rules "cancellable" #443

Open
Zylox opened this issue Nov 20, 2019 · 2 comments
Open

Make fire-rules "cancellable" #443

Zylox opened this issue Nov 20, 2019 · 2 comments

Comments

@Zylox
Copy link
Contributor

Zylox commented Nov 20, 2019

Consider the following just as an example (infinitely looping rules are just to show a "long running" session, not the case we are expecting to catch) :

(ns clara.test-interruption
  (:require [clara.rules :as r]))

(def counter (atom 0))

(defrecord A [])
(r/defrule a-rule
  [A]
  =>
  (swap! counter inc)
  (r/insert! (->A)))
(defn lock-me
  []
  (-> (r/mk-session)
      (r/insert (->A))
      (r/fire-rules)))
(defn background-lock
  []
  (let [fut (future (lock-me))
        val (deref fut 5000 :dead)]
    (when (= :dead val)
      (println "session timed out, cancelling")
      (println "counter at " (deref counter))
      (future-cancel fut))
    (println "done")))

If you execute this, then periodically check on the state of the counter, you can see that the "cancelled" future is still executing in the background.

We have a use case where we would like to abort firing rules and throw away the session if it takes too long. Using futures, we are able to return control to the parent thread, but the session keeps executing in the background. This can be problematic.

The most naive way to do this is the following: Zylox@81c1fb0. Running the above example with this line DOES cancel the future.

If the session is "stuck" in somewhere other than fire-rules* this obviously wont help, but i think thats where the vast majority of long running session would be. Also this doesn't have any answer for how this would happen in cljs (because to be honest i know near nothing about cljs)

This might be too naive though. The session is in a garbage state in this situation and we should probably find a way to indicate as much. I haven't had time to think about the best way to do this and am open to ideas.

EDIT: updated example to something slightly better.

@mrrodriguez
Copy link
Collaborator

The session being “in a bad state” after maybe isn’t really a big concern. I think this would be the case for other exceptions thrown during rule execution anyways right? So not really a unique case if so

@Zylox
Copy link
Contributor Author

Zylox commented Nov 20, 2019

upon some further thought, i do think there are probably some more places we would want to make "interruptable" than just the one in my example....i'll mock up some examples when i get some more time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants