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

Merging Timers and Schedules #647

Closed
ArieHein opened this issue Dec 27, 2020 · 2 comments · Fixed by #1043
Closed

Merging Timers and Schedules #647

ArieHein opened this issue Dec 27, 2020 · 2 comments · Fixed by #1043
Assignees
Milestone

Comments

@ArieHein
Copy link
Contributor

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 ?

@Badgerati
Copy link
Owner

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 Badgerati moved this to Idea in ✏ Pode Draft Oct 25, 2022
@Badgerati Badgerati moved this to Backlog in 🚀 Pode Roadmap Nov 29, 2022
@Badgerati Badgerati added this to the 2.8.0 milestone Dec 3, 2022
@Badgerati Badgerati self-assigned this Dec 11, 2022
@Badgerati Badgerati moved this from Backlog to In Progress in 🚀 Pode Roadmap Dec 11, 2022
Badgerati added a commit that referenced this issue Dec 11, 2022
@Badgerati
Copy link
Owner

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)"
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Status: Done
Development

Successfully merging a pull request may close this issue.

2 participants