poshbotio / PoshBot

Powershell-based bot framework
MIT License
537 stars 108 forks source link

StartAfter set in the past causes commands to run multiple times #120

Closed AngleOSaxon closed 5 years ago

AngleOSaxon commented 5 years ago

Expected Behavior

When a command is scheduled with an IntervalMS and a StartAfter value, it will execute on the StartAfter time and each subsequent time the IntervalMS elapses after the StartAfter.

If the StartAfter time is in the past and the schedule has just been created, the command will execute immediately and will execute again on each passing of the IntervalMS.

If the bot is brought online with a schedule containing a StartAfter in the past, the command will execute on each passing of the IntervalMS.

Current Behavior

Whenever scheduled commands are processed (either by new registration or by loading a schedules.psd1 file), the command is executed as many times as IntervalMS have elapsed between the StartAfter and the current time.

Possible Solution

Prevent RecalculateStartAfter() from producing new StartAfter values that are in the past--advance the initial StartAfter value by as many IntervalMS as are required to take it past the current time.

Have Scheduler call RecalculateStartAfter() for any stored schedules it loads whose StartAfter is in the past.

Steps to Reproduce (for bugs)

  1. Run the command !newschedule --command 'help' --interval minutes --value 5 --startafter '2018-10-01 12:25pm'
  2. Should see a large number of !help results outputted to the channel

Context

I have some long-standing scheduled commands that do some monitoring and report statuses out to a channel. They were originally scheduled in June, and don't need to run a few hundred times when I reboot the server.

Your Environment

devblackops commented 5 years ago

Thanks @AngleOSaxon. This looks like a much needed improvement!