< Summary - Kestrun — Combined Coverage

Information
Class: Public.Scheduling.Register-KrSchedule
Assembly: Kestrun.PowerShell.Public
File(s): /home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Scheduling/Register-KrSchedule.ps1
Tag: Kestrun/Kestrun@0d738bf294e6281b936d031e1979d928007495ff
Line coverage
0%
Covered lines: 0
Uncovered lines: 22
Coverable lines: 22
Total lines: 173
Line coverage: 0%
Branch coverage
N/A
Covered branches: 0
Total branches: 0
Branch coverage: N/A
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Coverage history

Coverage history 0 25 50 75 100 08/26/2025 - 14:53:17 Line coverage: 0% (0/22) Total lines: 171 Tag: Kestrun/Kestrun@78d1e497d8ba989d121b57aa39aa3c6b22de743109/04/2025 - 18:11:31 Line coverage: 0% (0/22) Total lines: 172 Tag: Kestrun/Kestrun@de99e24698289f3f61ac7b73e96092732ae12b0509/08/2025 - 20:34:03 Line coverage: 0% (0/24) Total lines: 176 Tag: Kestrun/Kestrun@3790ee5884494a7a2a829344a47743e0bf492e7210/13/2025 - 16:52:37 Line coverage: 0% (0/22) Total lines: 173 Tag: Kestrun/Kestrun@10d476bee71c71ad215bb8ab59f219887b5b4a5e

Metrics

File(s)

/home/runner/work/Kestrun/Kestrun/src/PowerShell/Kestrun/Public/Scheduling/Register-KrSchedule.ps1

#LineLine coverage
 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#>
 72function 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
 0119        $Server = Resolve-KestrunServer -Server $Server
 120    }
 121    process {
 122        # Ensure the scheduler service is enabled
 0123        $sched = $Server.Scheduler
 0124        if ($null -eq $sched) {
 0125            throw 'SchedulerService is not enabled. Call host.EnableScheduling() first.'
 126        }
 127
 128        try {
 0129            switch ($PSCmdlet.ParameterSetName) {
 130
 131                'CronScriptBlock' {
 0132                    $sched.Schedule($Name, $Cron, $ScriptBlock, $RunImmediately.IsPresent)
 133                }
 134                'IntervalScriptBlock' {
 0135                    $sched.Schedule($Name, $Interval, $ScriptBlock, $RunImmediately.IsPresent)
 136                }
 137                'CronBlock' {
 0138                    $sched.Schedule($Name, $Cron, $Code, $Language, $RunImmediately.IsPresent)
 139                }
 140                'IntervalBlock' {
 0141                    $sched.Schedule($Name, $Interval, $Code, $Language, $RunImmediately.IsPresent)
 142                }
 143                'CronFile' {
 0144                    $fileInfo = [System.IO.FileInfo]$ScriptPath
 0145                    if (-not $fileInfo.Exists) {
 0146                        throw "Script file '$ScriptPath' does not exist."
 147                    }
 0148                    $sched.Schedule($Name, $Cron, $fileInfo, $Language, $RunImmediately.IsPresent)
 149                }
 150                'IntervalFile' {
 0151                    $fileInfo = [System.IO.FileInfo]$ScriptPath
 0152                    if (-not $fileInfo.Exists) {
 0153                        throw "Script file '$ScriptPath' does not exist."
 154                    }
 0155                    $sched.Schedule($Name, $Interval, $fileInfo, $Language, $RunImmediately.IsPresent)
 156                }
 157            }
 0158            if ($PassThru.IsPresent) {
 159                # if the PassThru switch is specified, return the job info
 160                # Return the newly registered job info
 0161                Write-KrLog -Level Information -Message "Schedule '{0}' registered successfully." -Values $Name
 162
 163                # return the freshly-registered JobInfo
 0164                return $sched.GetSnapshot() | Where-Object Name -EQ $Name
 165            } else {
 0166                Write-KrLog -Level Information -Message "Schedule '{0}' registered successfully. Use -PassThru to return
 167            }
 168        } catch {
 0169            Write-KrLog -Level Error -Message 'Failed to register schedule' -ErrorRecord $_
 170        }
 171    }
 172}
 173

Methods/Properties

Register-KrSchedule()