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

docs: add advanced tip about scheduling function calls #2617

Merged
merged 2 commits into from
Oct 22, 2024

Conversation

dgw
Copy link
Member

@dgw dgw commented Sep 2, 2024

How to do this was discussed fairly well in #1222 a while ago, and I just cribbed an example from there.

I also found an apparently obsolete note about using sched with @plugin.rate and friends while poking around the topic of scheduled callables, because I tested what it said and found that the caveat no longer applies (see #825).

Checklist

  • I have read CONTRIBUTING.md
  • I can and do license this contribution under the EFLv2
  • No issues are reported by make qa (runs make lint and make test)
  • I have tested the functionality of the things this change touches

Notes

Along the way I also briefly considered giving the bot a schedule instance that it provides to plugins, but I'm not ready to commit to that kind of API addition yet. Putting this into the tips & tricks section is better than pointing to a random issue comment on the occasions when "how do I schedule a function at a specific time?" comes up, and it doesn't preclude API additions at a later time if we come up with a sane decorator interface for it.

We learned why replicating the API of another library sucks back when
`sopel.web` contained wrappers for various tools from `requests`, and we
don't want to do it again.

Setting up a scheduler for one's plugin doesn't take a lot of work. It's
not ideal for multiple plugins to all have their own `interval` checks
for whether there are any pending tasks to run, but we can drop this new
section from the docs if we find a decent compromise.
This appears to have been fixed as part of the big rule-system rewrite
back in 7.1. The rule system now also considers the *start* time of the
callable's most recent run in the given context, if it hasn't finished
running since the last time it was started.

I also tested this with a very basic plugin:

    import sched
    import time

    from sopel import plugin

    s = sched.scheduler(time.time, time.sleep)

    @plugin.rate(60)
    @plugin.command('testrate')
    def rate_limited(bot, trigger):
        s.enter(30, 2, bot.say, argument=('hi',))
        s.run()

Despite trying to run `.testrate` three times in a few seconds, as a non
admin user, the bot only said "hi" once.
@dgw dgw added Low Priority Documentation Housekeeping Code cleanup, removal of deprecated stuff, etc. labels Sep 2, 2024
@dgw dgw added this to the 8.1.0 milestone Sep 2, 2024
@dgw dgw requested a review from a team September 2, 2024 01:32
Copy link
Contributor

@Exirel Exirel left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I love how simple that is. Nice!

@dgw
Copy link
Member Author

dgw commented Oct 13, 2024

Ramping up to 8.0.1 soon, don't see why this should be held back for 8.1.

@dgw dgw modified the milestones: 8.1.0, 8.0.1 Oct 13, 2024
@dgw dgw merged commit 61b217f into master Oct 22, 2024
15 checks passed
@dgw dgw deleted the docs-scheduling-tips branch October 22, 2024 19:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Documentation Housekeeping Code cleanup, removal of deprecated stuff, etc. Low Priority
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants