Skip to content

Commit

Permalink
#435: ability to set number of concurrent schedules
Browse files Browse the repository at this point in the history
  • Loading branch information
Badgerati committed Dec 19, 2019
1 parent da9276a commit 30d18e1
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 5 deletions.
2 changes: 1 addition & 1 deletion docs/Tutorials/Schedules.md
@@ -1,6 +1,6 @@
# Schedules

A Schedule in Pode is a long-running async task, and unlike timers, when they trigger they are run in their own separate runspace - so they don't affect each other if they take a while to process.
A Schedule in Pode is a long-running async task, and unlike timers, when they trigger they are run in their own separate runspace - so they don't affect each other if they take a while to process. By default up to a maximum of 10 schedules can run concurrently, this can be changed by using the [`Set-PodeScheduleConcurrency`](../../Functions/Core/Set-PodeScheduleConcurrency) function.

Schedule triggers are defined using [`cron expressions`](../Misc/CronExpressions), basic syntax is supported as well as some predefined expressions. Schedules can start immediately, have a delayed start time, and also have a defined end time.

Expand Down
26 changes: 26 additions & 0 deletions examples/schedules-long-running.ps1
@@ -0,0 +1,26 @@
$path = Split-Path -Parent -Path (Split-Path -Parent -Path $MyInvocation.MyCommand.Path)
Import-Module "$($path)/src/Pode.psm1" -Force -ErrorAction Stop

# or just:
# Import-Module Pode

# create a server, and start listening on port 8085
Start-PodeServer {

# listen on localhost:8085
Add-PodeEndpoint -Address * -Port 8085 -Protocol Http

# add lots of schedules that each sleep for a while
1..30 | ForEach-Object {
Add-PodeSchedule -Name "Schedule_$($_)" -Cron '@minutely' -ArgumentList @{ ID = $_ } -ScriptBlock {
param($ID)

$seconds = (Get-Random -Minimum 5 -Maximum 40)
Start-Sleep -Seconds $seconds
"ID: $($ID) [$($seconds)]" | Out-PodeHost
}
}

Set-PodeScheduleConcurrency -Maximum 30

}
1 change: 1 addition & 0 deletions src/Pode.psd1
Expand Up @@ -125,6 +125,7 @@
'Clear-PodeSchedule',
'Invoke-PodeSchedule',
'Edit-PodeSchedule',
'Set-PodeScheduleConcurrency',

# timers
'Add-PodeTimer',
Expand Down
2 changes: 1 addition & 1 deletion src/Private/Context.ps1
Expand Up @@ -265,7 +265,7 @@ function New-PodeRunspacePools
$PodeContext.RunspacePools.Signals.Open()

# setup schedule runspace pool
$PodeContext.RunspacePools.Schedules = [runspacefactory]::CreateRunspacePool(1, 2, $PodeContext.RunspaceState, $Host)
$PodeContext.RunspacePools.Schedules = [runspacefactory]::CreateRunspacePool(1, 10, $PodeContext.RunspaceState, $Host)
$PodeContext.RunspacePools.Schedules.Open()

# setup gui runspace pool (only for non-ps-core)
Expand Down
6 changes: 3 additions & 3 deletions src/Private/Server.ps1
Expand Up @@ -16,6 +16,9 @@ function Start-PodeInternalServer
# create the shared runspace state
New-PodeRunspaceState

# start the runspace pools for web, schedules, etc
New-PodeRunspacePools

# get the server's script and invoke it - to set up routes, timers, middleware, etc
$_script = $PodeContext.Server.Logic
if (Test-PodePath -Path $PodeContext.Server.LogicPath -NoStatus) {
Expand All @@ -24,9 +27,6 @@ function Start-PodeInternalServer

Invoke-PodeScriptBlock -ScriptBlock $_script -NoNewClosure

# start the runspace pools for web, schedules, etc
New-PodeRunspacePools

# create timer/schedules for auto-restarting
New-PodeAutoRestartServer

Expand Down
37 changes: 37 additions & 0 deletions src/Public/Core.ps1
Expand Up @@ -1068,6 +1068,43 @@ function Add-PodeSchedule
}
}

<#
.SYNOPSIS
Set the maximum number of concurrent schedules.
.DESCRIPTION
Set the maximum number of concurrent schedules.
.PARAMETER Maximum
The Maximum number of schdules to run.
.EXAMPLE
Set-PodeScheduleConcurrency -Maximum 25
#>
function Set-PodeScheduleConcurrency
{
[CmdletBinding()]
param(
[Parameter(Mandatory=$true)]
[int]
$Maximum
)

# error if <=0
if ($Maximum -le 0) {
throw "Maximum concurrent schedules must be >=0 but got: $($Maximum)"
}

# ensure max > min
$_min = $PodeContext.RunspacePools.Schedules.GetMinRunspaces()
if ($_min -gt $Maximum) {
throw "Maximum concurrent schedules cannot be less than the minimum of $($_min) but got: $($Maximum)"
}

# set the max schedules
$PodeContext.RunspacePools.Schedules.SetMaxRunspaces($Maximum)
}

<#
.SYNOPSIS
Adhoc invoke a Schedule's logic.
Expand Down

0 comments on commit 30d18e1

Please sign in to comment.