Skip to content

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

@AngleOSaxon

Description

@AngleOSaxon

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.

  • For example, if a new command is scheduled to run every hour at five minutes after the hour, with a StartAfter of 3:05 PM, and the current time is 3:15 PM, the command would execute immediately. Subsequent runs would take place at 4:05 PM, 5:05 PM, etc.

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.

  • For example, if a command is scheduled to run every hour at five minutes after the hour, with a StartAfter of 3:05 PM, and the bot is brought online at 3:15 PM, the command would next run at 4:05 PM.

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.

  • For example, if a command was originally scheduled to run with --Interval days --Value 1 --StartAfter '2018-01-01' and the bot is started or the command is registered on 2018-01-05, the command will execute 5 times immediately after the schedule is processed.

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

  • Module version used: 0.11.0
  • Operating System and PowerShell version: Windows Server 2012, PowerShell 5.1

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions