| | | 1 | | <# |
| | | 2 | | .SYNOPSIS |
| | | 3 | | Creates a new scheduled job in the active Kestrun host. |
| | | 4 | | .DESCRIPTION |
| | | 5 | | Supports either CRON or fixed interval triggers, and either an inline |
| | | 6 | | ScriptBlock or a script file path. Use -RunImmediately to execute |
| | | 7 | | once right after registration. |
| | | 8 | | .PARAMETER Server |
| | | 9 | | The Kestrun host instance to use for scheduling the job. |
| | | 10 | | This is typically the instance running the Kestrun server. |
| | | 11 | | .PARAMETER Name |
| | | 12 | | Unique job name. |
| | | 13 | | .PARAMETER Language |
| | | 14 | | Script language to use for the job. |
| | | 15 | | .PARAMETER Cron |
| | | 16 | | 6-field cron expression (seconds precision). Mutually exclusive with -Interval. |
| | | 17 | | .PARAMETER Interval |
| | | 18 | | System.TimeSpan string (e.g. '00:05:00'). Mutually exclusive with -Cron. |
| | | 19 | | .PARAMETER ScriptBlock |
| | | 20 | | Inline PowerShell code to run. |
| | | 21 | | This is the default parameter for the job's script content. |
| | | 22 | | .PARAMETER Code |
| | | 23 | | Inline code in the specified language (e.g. C#) to run. |
| | | 24 | | .PARAMETER ScriptPath |
| | | 25 | | Path to a .ps1 file. The file is read once at registration time. |
| | | 26 | | .PARAMETER RunImmediately |
| | | 27 | | Execute once right after being registered. |
| | | 28 | | .PARAMETER PassThru |
| | | 29 | | If specified, the cmdlet will return the newly registered job info. |
| | | 30 | | .EXAMPLE |
| | | 31 | | Register-KrSchedule -Name Cache -Interval '00:15:00' -ScriptBlock { |
| | | 32 | | Clear-KrCache |
| | | 33 | | } |
| | | 34 | | Register a job that runs every 15 minutes, clearing the cache. |
| | | 35 | | .EXAMPLE |
| | | 36 | | Register-KrSchedule -Name Nightly -Cron '0 0 3 * * *' -ScriptPath 'Scripts/Cleanup.ps1' |
| | | 37 | | Register a job that runs nightly at 3 AM, executing the script at 'Scripts/Cleanup.ps1'. |
| | | 38 | | .EXAMPLE |
| | | 39 | | Register-KrSchedule -Name Heartbeat -Cron '*/10 * * * * *' -ScriptBlock { |
| | | 40 | | Write-KrLog -Level Information -Message "💓 Heartbeat at {0:O}" -Values $([DateTimeOffset]::UtcNow) |
| | | 41 | | } |
| | | 42 | | Register a job that runs every 10 seconds, logging a heartbeat message. |
| | | 43 | | .EXAMPLE |
| | | 44 | | Register-KrSchedule -Name 'InlineJob' -Interval '00:01:00' -ScriptBlock { |
| | | 45 | | Write-Information "[$([DateTime]::UtcNow.ToString('o'))] 🌙 Inline job ran." |
| | | 46 | | } |
| | | 47 | | Register a job that runs every minute, executing the inline PowerShell code. |
| | | 48 | | .EXAMPLE |
| | | 49 | | Register-KrSchedule -Server $server -Name 'CSharpJob' -Cron '0 0/5 * * * *' -Language CSharp -Code @" |
| | | 50 | | Console.WriteLine($"C# job executed at {DateTime.UtcNow:O}"); |
| | | 51 | | "@ |
| | | 52 | | Register a job that runs every 5 minutes, executing inline C# code. |
| | | 53 | | .EXAMPLE |
| | | 54 | | Register-KrSchedule -Server $server -Name 'FileJob' -Cron '0 0 1 * * *' -ScriptPath 'Scripts/Backup.cs' -Languag |
| | | 55 | | Register a job that runs daily at 1 AM, executing the C# script at 'Scripts/Backup.cs'. |
| | | 56 | | .EXAMPLE |
| | | 57 | | Register-KrSchedule -Server $server -Name 'RunOnce' -Interval '00:01:00' -ScriptBlock { |
| | | 58 | | Write-KrLog -Level Information -Message "Running once at {0:O}" -Values $([DateTimeOffset]::UtcNow) |
| | | 59 | | } -RunImmediately |
| | | 60 | | Register a job that runs once immediately after registration, then every minute. |
| | | 61 | | .EXAMPLE |
| | | 62 | | Register-KrSchedule -Server $server -Name 'CSharpFileJob' -Cron '0 0 2 * * *' -ScriptPath 'Scripts/ProcessData.c |
| | | 63 | | Register a job that runs daily at 2 AM, executing the C# script at 'Scripts/ProcessData.cs'. |
| | | 64 | | .EXAMPLE |
| | | 65 | | Register-KrSchedule -Server $server -Name 'PythonJob' -Cron '0 0/10 * * * *' -Language Python -ScriptPath 'Scrip |
| | | 66 | | Register a job that runs every 10 minutes, executing the Python script at 'Scripts/AnalyzeData.py'. |
| | | 67 | | .EXAMPLE |
| | | 68 | | Register-KrSchedule -Server $server -Name 'JavaScriptJob' -Cron '0 0/15 * * * *' -Language JavaScript -ScriptPat |
| | | 69 | | Register a job that runs every 15 minutes, executing the JavaScript script at 'Scripts/GenerateReport.js'. |
| | | 70 | | |
| | | 71 | | #> |
| | | 72 | | function Register-KrSchedule { |
| | | 73 | | [KestrunRuntimeApi('Everywhere')] |
| | | 74 | | [CmdletBinding(DefaultParameterSetName = 'IntervalScriptBlock')] |
| | | 75 | | [OutputType([Kestrun.Scheduling.JobInfo])] |
| | | 76 | | param( |
| | | 77 | | [Parameter(Mandatory = $false, ValueFromPipeline = $true)] |
| | | 78 | | [Kestrun.Hosting.KestrunHost]$Server, |
| | | 79 | | |
| | | 80 | | [Parameter(Mandatory)] |
| | | 81 | | [string]$Name, |
| | | 82 | | |
| | | 83 | | [Parameter(Mandatory = $true, ParameterSetName = 'CronFile')] |
| | | 84 | | [Parameter(Mandatory = $true, ParameterSetName = 'CronBlock')] |
| | | 85 | | [Parameter(Mandatory = $true, ParameterSetName = 'IntervalBlock')] |
| | | 86 | | [Parameter(Mandatory = $true, ParameterSetName = 'IntervalFile')] |
| | | 87 | | [Kestrun.Scripting.ScriptLanguage]$Language, |
| | | 88 | | |
| | | 89 | | [Parameter(Mandatory = $true, ParameterSetName = 'CronScriptBlock')] |
| | | 90 | | [Parameter(Mandatory = $true, ParameterSetName = 'CronBlock')] |
| | | 91 | | [Parameter(Mandatory = $true, ParameterSetName = 'CronFile')] |
| | | 92 | | [string]$Cron, |
| | | 93 | | |
| | | 94 | | [Parameter(Mandatory = $true, ParameterSetName = 'IntervalScriptBlock')] |
| | | 95 | | [Parameter(Mandatory = $true, ParameterSetName = 'IntervalBlock')] |
| | | 96 | | [Parameter(Mandatory = $true, ParameterSetName = 'IntervalFile')] |
| | | 97 | | [timespan]$Interval, |
| | | 98 | | |
| | | 99 | | [Parameter(Mandatory = $true, ParameterSetName = 'CronScriptBlock')] |
| | | 100 | | [Parameter(Mandatory = $true, ParameterSetName = 'IntervalScriptBlock')] |
| | | 101 | | [scriptblock]$ScriptBlock, |
| | | 102 | | |
| | | 103 | | [Parameter(Mandatory = $true, ParameterSetName = 'CronBlock')] |
| | | 104 | | [Parameter(Mandatory = $true, ParameterSetName = 'IntervalBlock')] |
| | | 105 | | [string]$Code, |
| | | 106 | | |
| | | 107 | | [Parameter(Mandatory = $true, ParameterSetName = 'CronFile')] |
| | | 108 | | [Parameter(Mandatory = $true, ParameterSetName = 'IntervalFile')] |
| | | 109 | | [string]$ScriptPath, |
| | | 110 | | |
| | | 111 | | [Parameter()] |
| | | 112 | | [switch]$RunImmediately, |
| | | 113 | | |
| | | 114 | | [Parameter()] |
| | | 115 | | [switch]$PassThru |
| | | 116 | | ) |
| | | 117 | | begin { |
| | | 118 | | # Ensure the server instance is resolved |
| | 0 | 119 | | $Server = Resolve-KestrunServer -Server $Server |
| | | 120 | | } |
| | | 121 | | process { |
| | | 122 | | # Ensure the scheduler service is enabled |
| | 0 | 123 | | $sched = $Server.Scheduler |
| | 0 | 124 | | if ($null -eq $sched) { |
| | 0 | 125 | | throw 'SchedulerService is not enabled. Call host.EnableScheduling() first.' |
| | | 126 | | } |
| | | 127 | | |
| | | 128 | | try { |
| | 0 | 129 | | switch ($PSCmdlet.ParameterSetName) { |
| | | 130 | | |
| | | 131 | | 'CronScriptBlock' { |
| | 0 | 132 | | $sched.Schedule($Name, $Cron, $ScriptBlock, $RunImmediately.IsPresent) |
| | | 133 | | } |
| | | 134 | | 'IntervalScriptBlock' { |
| | 0 | 135 | | $sched.Schedule($Name, $Interval, $ScriptBlock, $RunImmediately.IsPresent) |
| | | 136 | | } |
| | | 137 | | 'CronBlock' { |
| | 0 | 138 | | $sched.Schedule($Name, $Cron, $Code, $Language, $RunImmediately.IsPresent) |
| | | 139 | | } |
| | | 140 | | 'IntervalBlock' { |
| | 0 | 141 | | $sched.Schedule($Name, $Interval, $Code, $Language, $RunImmediately.IsPresent) |
| | | 142 | | } |
| | | 143 | | 'CronFile' { |
| | 0 | 144 | | $fileInfo = [System.IO.FileInfo]$ScriptPath |
| | 0 | 145 | | if (-not $fileInfo.Exists) { |
| | 0 | 146 | | throw "Script file '$ScriptPath' does not exist." |
| | | 147 | | } |
| | 0 | 148 | | $sched.Schedule($Name, $Cron, $fileInfo, $Language, $RunImmediately.IsPresent) |
| | | 149 | | } |
| | | 150 | | 'IntervalFile' { |
| | 0 | 151 | | $fileInfo = [System.IO.FileInfo]$ScriptPath |
| | 0 | 152 | | if (-not $fileInfo.Exists) { |
| | 0 | 153 | | throw "Script file '$ScriptPath' does not exist." |
| | | 154 | | } |
| | 0 | 155 | | $sched.Schedule($Name, $Interval, $fileInfo, $Language, $RunImmediately.IsPresent) |
| | | 156 | | } |
| | | 157 | | } |
| | 0 | 158 | | if ($PassThru.IsPresent) { |
| | | 159 | | # if the PassThru switch is specified, return the job info |
| | | 160 | | # Return the newly registered job info |
| | 0 | 161 | | Write-KrLog -Level Information -Message "Schedule '{0}' registered successfully." -Values $Name |
| | | 162 | | |
| | | 163 | | # return the freshly-registered JobInfo |
| | 0 | 164 | | return $sched.GetSnapshot() | Where-Object Name -EQ $Name |
| | | 165 | | } else { |
| | 0 | 166 | | Write-KrLog -Level Information -Message "Schedule '{0}' registered successfully. Use -PassThru to return |
| | | 167 | | } |
| | | 168 | | } catch { |
| | 0 | 169 | | Write-KrLog -Level Error -Message 'Failed to register schedule' -ErrorRecord $_ |
| | | 170 | | } |
| | | 171 | | } |
| | | 172 | | } |
| | | 173 | | |