Pode icon indicating copy to clipboard operation
Pode copied to clipboard

Merging Timers and Schedules

Open ArieHein opened this issue 4 years ago • 1 comments

Should Timers and Schedules be refactored and merged into one function.

Possible adding a flag -schedule to make it in a schedule? I understand cron has a well known timing schedule but still not as common to those coming from windows world. And as a long time PowerShell user, having the Timers syntax is a bit more nicer to look : -Start -End -Count and similar

Are schedules suppose to persist after a server a restart, while timers would not ?

ArieHein avatar Dec 27 '20 12:12 ArieHein

There is actually a slight difference between the two:

  • Timers work in intervals of seconds, the moment the server starts, timers immediately start from that point in time - and repeat every X seconds (unless you set a start/end/count, of course). If you have a timer that repeats every 10secs, it runs, then you restart the server after 5secs, then the timer triggers after another 10secs - 15secs later, rather than 5secs. They're more designed for quick simple repeatable tasks, such as saving scope, or making a quick web request. They all run one-after-another, so long timers slow down other timers.

  • Schedules have a lower interval of 1 minute, and although they start with the server as well, a server restart doesn't re-extend the time. If you have a schedule of 1min, it will run at Xmin 00secs; so if it runs and you restart the server 30secs later, the schedule will still trigger as expected 30secs later - not a new 1min later. They're designed for longer running tasks - like reports or numerous requests - as each schedule is run separate from other schedules in a new runspace.

Schedules could have an interval of 1sec, but they would start getting messy/laggy very quickly - so normally 1min is preferred.

I do agree that Cron Expressions on Wndows is a weird thing not many people would be familiar with. We could maybe introduce a New-PodeScheduleCron that builds a cron for you, and it's output could be used for -Cron on Add-PodeSchedule (as well as a raw string for more advanced use cases)?

Such as every 10mins, but only on a Tuesday would be:

$cron = New-PodeScheduleCron -Minute 0, 10, 20, 30, 40, 50 -Day Tuesday
Add-PodeSchedule -Cron $cron -Etc

🤔

Badgerati avatar Dec 31 '20 17:12 Badgerati

I've added a new helper function, New-PodeCron, to help with creating cron expressions for schedules:

The main way to use New-PodeCron is to start with the -Every parameter, such as -Every Hour or -Every Day. From this, you can customise the expression to run at specific times/days, or apply a recurring -Interval:

# Everyday, at 00:00
New-PodeCron -Every Day

# Every Tuesday and Friday, at 01:00
New-PodeCron -Every Day -Day Tuesday, Friday -Hour 1

# Every 15th of the month at 00:00
New-PodeCron -Every Month -Date 15

# Every other day, starting from the 2nd of each month, at 00:00
New-PodeCron -Every Date -Interval 2 -Date 2

# Every 1st June, at 00:00
New-PodeCron -Every Year -Month June

# Every hour, starting at 01:00
New-PodeCron -Every Hour -Hour 1 -Interval 1

# Every 15 minutes, between 01:00 and 05:00
New-PodeCron -Every Minute -Hour 1, 2, 3, 4, 5 -Interval 15

# Every hour of every Monday (ie: 00:00, 01:00, 02:00, etc.)
New-PodeCron -Every Hour -Day Monday

# Every 1st January, April, July, and October, at 00:00
New-PodeCron -Every Quarter

# Everyday at 05:15
New-PodeCron -Every Day -Hour 5 -Minute 15

You can also use New-PodeCron without using the -Every parameter. In this state, every part of the cron expression will be wildcarded by default - such as every minute, every hour, every day, etc. - unless you specify the parameter explicitly:

# Every 10 minutes on Tuesdays
New-PodeCron -Day Tuesday -Minute 0, 10, 20, 30, 40, 50

# Every minute on Tuesdays
New-PodeCron -Day Tuesday

The value returned by New-PodeCron is a valid cron expression, that can then be used when creating Schedules:

# Every Tuesday, at 00:05
$cron = New-PodeCron -Day Tuesday -Hour 0 -Minute 5

Add-PodeSchedule -Name 'date' -Cron $cron -ScriptBlock {
    Write-Host "$([DateTime]::Now)"
}

Badgerati avatar Dec 11 '22 12:12 Badgerati