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

Scheduled command runs at different times #1172

Closed
nakami opened this issue Feb 7, 2017 · 7 comments
Closed

Scheduled command runs at different times #1172

nakami opened this issue Feb 7, 2017 · 7 comments
Labels

Comments

@nakami
Copy link

nakami commented Feb 7, 2017

Running sopel on CentOS release 6.8, Python 3.4.3.

I have a module containing a command that is supposed to run at UTC 0:00 + 2 minutes offset.
However, it runs at unexpected times, after the first successful run.

this is the relevant code:

from sopel import module
from datetime import datetime

offset_seconds     = 60*2 # 2min
seconds_a_day           = 86400
channels                = ["#mybotchannel"]

def get_time_utc():
	return datetime.utcnow()

def get_seconds_to_midnight():
    utcnow = get_time_utc()
    sec_passed = utcnow.hour * 3600
    sec_passed += utcnow.minute * 60
    sec_passed += utcnow.second
    return seconds_a_day - sec_passed

def msg_multi_chan(bot, channels, message):
    for channel in channels:
        bot.msg(channel, message)

new_time = get_seconds_to_midnight() + offset_seconds 

@module.interval(new_time)
def run_at_midnight_plus_offset(bot):
	# init
    msg_multi_chan(bot, channels, bold("[Triggered]") +" this should trigger 2min after midnight")
    new_time = get_seconds_to_midnight() + offset_seconds
    return

@module.commands('debug')
def debug(bot, trigger):
    bot.say("seconds to midnight plus offset: " + str(get_seconds_to_midnight() + offset_seconds))
    return

first time:
00:02:01 bot: [Triggered] this should trigger 2min after midnight ✔️
same day, later:
23:54:49 bot: [Triggered] this should trigger 2min after midnight ✖️
day after:
23:43:39 bot: [Triggered] this should trigger 2min after midnight ✖️
day after:
23:40:28 bot: [Triggered] this should trigger 2min after midnight ✖️
day after:
23:33:16 bot: [Triggered] this should trigger 2min after midnight ✖️

however, my debug command returns the expected seconds value.
what did i screw up? is this expected behaviour?

@nakami
Copy link
Author

nakami commented Feb 7, 2017

i guess the parameter of

@module.interval(new_time)

is just set once and therefore contains the initial amount of seconds for the next runs.
is there any way to resolve my issue?
the first idea i come up with is hardcoding the interval to be a day in seconds and then starting the bot at the exact time i want it to run the command each day... meh.

@lramati
Copy link
Contributor

lramati commented Feb 7, 2017 via email

@nakami
Copy link
Author

nakami commented Feb 7, 2017

ah, it's that simple. argh. thanks.

so, is there a proper way to schedule things?

@dgw dgw added the Question label Mar 25, 2018
@dgw
Copy link
Member

dgw commented Mar 25, 2018

"Proper"? No, not at this time. I'll close this question, as the feature request of scheduling callables for a specific clock time (instead of an interval) is covered by #1222.

@dgw dgw closed this as completed Mar 25, 2018
@ronilaukkarinen
Copy link

@nakami Can you share the working script? Trying to figure out on how to get the sopel bot say something after midnight, cannot find any working example...

@ronilaukkarinen
Copy link

Any hint?

@dgw
Copy link
Member

dgw commented Feb 23, 2021

@ronilaukkarinen Did you visit the linked issue? #1222 (comment) shows some approaches to this.

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

No branches or pull requests

4 participants