-
Notifications
You must be signed in to change notification settings - Fork 39
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
New feature: request upcoming chimes #30
Comments
Hey @pmonks, You're right, we do have that information, although only as part of this closure: https://github.com/jarohen/chime/blob/master/src/chime/core.clj#L88 - we'd have to change this to be shared state between the caller's thread and the scheduling thread in order to return it as part of the return value from James |
not 100% on this API because they're not accessible from within the scheduling fn - but we can't make them accessible easily without changing the arity of the schedule fn
not 100% on this API because they're not accessible from within the scheduling fn - but we can't make them accessible easily without changing the arity of the schedule fn
I might be misunderstanding your proposed solution, but what I'd envisaged was a new public fn in the Returning a value from |
Ah, I see. Assuming I've understood you correctly, this is less appealing, I'm afraid. Chime doesn't store any information globally at the moment - doing so would mean that Chime would either risk leaking memory, or take on the responsibility of managing/cleaning up references - so it's something I'd rather avoid. |
@pmonks A stateful scheduler which keeps track of active jobs (id => cancel-fn) is pretty trivial to do on-top of |
@jarohen something is already keeping track of all of this information, in order to actually run the jobs at the scheduled times - my hope was simply that chime could provide a way to interrogate that existing data store. To @jimpil's point, I'd much rather not have to duplicate that information again in "user" space. |
@pmonks What you want (as I understand it), is a higher-level scheduler construct which can accept multiple scheduling jobs, and at any given time you can ask it for the actively-running jobs (not finished, nor cancelled). The construct I'm using expects something like the following: ;; job-id => job-details
{:foo [foo-times-fn foo-chime-fn]
:bar [bar-times-fn bar-chime-fn]} It calls |
@pmonks: @jimpil is right, we don't keep track of what jobs are active. If it helps, it's the same as the underlying Java Regarding upcoming chimes - we could expose more information that we currently do about an individual schedule. Again, @jimpil is right, Chime only knows about the next time a schedule will be called, because it expects the caller to have passed a lazy sequence, and we don't want to evaluate more of that lazy sequence than we need to - it may be infinite, after all. With the combination of the caller keeping track of the references, and Chime being more helpful about each individual reference, we might be able to provide what you're looking for? |
So my code has a daily job (created via While my code maintains a reference to the daily job, it discards the response from Based on your explanations, this raises a couple of questions:
|
If I understand correctly, it's the underlying JVM
Not that I know of, I'm afraid. Would there be anything preventing you from keeping the |
ScheduledFuture
(cancel [_ interrupt?] ;; expose interrupt facilities (opt-in)
(when-let [^ScheduledFuture fut @current]
(and (not (.isCancelled fut))
(.cancel fut interrupt?))))
(getDelay [_ time-unit] ;; expose remaining time until next chime (partly addresses #30)
(when-let [^ScheduledFuture fut @current]
(.getDelay fut time-unit)))
It's always correct, but only at the exact moment it returns it's calculated - after that, it's out of date, and the user doesn't know the 'now' basis that was used in the calculation. If we pass the raw date back to the user instead, they're in charge of choosing a fixed 'now' to make that calculation.
We've fixed an issue previously (#10) around holding on to the head of the lazy seq - I wouldn't want to ask the user to do the same, especially if it's infinite. We have the tail of the sequence available, and could expose it - this would be particularly suitable if the user only wanted the next time, as it wouldn't require evaluating any more of the sequence. |
@pmonks I am going to try to complement @jarohen 's answers - hope this helps you.
By the way, for tens/hundreds/thousands of one-off jobs I'd use |
If this information ends up encoded into threads, then those can be enumerated via
Sure, but how would that support the case where some 3rd party code I'm using also uses chime? How would I enumerate all chime jobs across both my own code and that 3rd party code? Note that this isn't a problem I'm currently facing, but it's not a far-fetched scenario imho. |
Ah, interesting, I hadn't considered JMX. Not sure I'd personally make application logic depend on it? (Obviously not my place to say 'don't', but just not something I've done.)
Maybe you could expand on why you might want to know what Chimes a third-party has scheduled? Seems it'd either be up to the third-party to expose its schedules to you via its own API, otherwise it'd be a bit of a leaky abstraction? |
Sure. I use chime in a chatbot, and amongst various chatops features I'd like it to support is to respond to an admin's request to enumerate upcoming jobs for the day. As mentioned above this varies quite wildly, and because the 3rd party data source charges per request, re-querying and regenerating the schedule of daily jobs from there is financially undesirable. If I were to start using a third party library that also uses chime, I'd like to know about its jobs too, since the production instance of this bot runs on a resource constrained platform (Heroku), and the code is designed to spread out processing throughout the day to stay within those resource constraints as much as possible. |
Oh and regarding:
Yeah I think I have much the same feeling about JMX as you do - it's not generally something I consider to be part of the "core" API the JVM exposes. That said, I have used JMX like this in the distant past (albeit not ThreadMXBean) and it's very handy when you need it. Another concern for me is where JMX is going to end up in the post-Java 9 / Jigsaw modularised world - it's plausible that JMX will be split out into an optional module, and then the question becomes whether it's in chime's best interest to effectively add a dependency on a (non-JVM-core) module. I should note that a cursory round of googling didn't answer the question of where JMX has ended up, post-Jigsaw. |
I read this thread. And seems not to be so easy to implement this feature. |
Actually finding (require '[clojure.string :as s])
(def tmx (java.lang.management.ManagementFactory/getThreadMXBean))
(filter #(s/starts-with? (.getThreadName %) "chime-") (map #(.getThreadInfo tmx %) (.getAllThreadIds tmx))) The only missing piece is to store the chime metadata (the lazy sequence of "run-at" times and the fn that will be called at those times) in each of those threads in such a way that it can be read back out. [edit] and a potentially more useful alternative (since it provides actual (require '[clojure.string :as s])
(filter #(s/starts-with? (.getName %) "chime-") (.keySet (Thread/getAllStackTraces))) I think if |
It would be useful to be able to ask
chime
for upcoming scheduled jobs. While my code could cache this information itself, I figure chime is already storing this information somewhere, so it would be better to just ask the source, rather than duplicating it in my own code.The text was updated successfully, but these errors were encountered: