Scheduling Quickstart

Enable the scheduler, register a couple of jobs, and expose diagnostic routes.

Full source

File: pwsh/tutorial/examples/12.1-Scheduling-Quickstart.ps1

<#
    Scheduling Quickstart Script
    Demonstrates enabling the SchedulerService and registering jobs
    with both PowerShell and C# code. Also exposes a small route to
    inspect the live schedule report and a counter route.
    FileName: 12.1-Scheduling-Quickstart.ps1
#>
param(
    [int]$Port = 5000,
    [IPAddress]$IPAddress = [IPAddress]::Loopback
)

## 1. Logging
New-KrLogger |
    Add-KrSinkConsole |
    Register-KrLogger -Name 'console' -SetAsDefault

## 2. Server
New-KrServer -Name 'Scheduling Demo'

## 3. Listener
Add-KrEndpoint -Port $Port -IPAddress $IPAddress

## 4. Runtime + Scheduler

Add-KrScheduling -MaxRunspaces 4

## 5. Shared state (for demo route)
Set-KrSharedState -Name 'Visits' -Value @{ Count = 0 }

## 6. Apply configuration
Enable-KrConfiguration

## 7. Register scheduled jobs

# (A) PowerShell heartbeat every 10s, run immediately once
Register-KrSchedule -Name 'heartbeat-ps' -Interval '00:00:10' -RunImmediately -ScriptBlock {
    Write-KrLog -Level Information -Message 'πŸ’“ Heartbeat (PS) at {0:O}' -Values $([DateTimeOffset]::UtcNow)
}

# (B) C# heartbeat every 15s
Register-KrSchedule -Name 'heartbeat-cs' -Interval '00:00:15' -Language CSharp -Code 'Serilog.Log.Information("πŸ’“ Heartbeat (C#) at {0:O}", DateTimeOffset.UtcNow);'

## 8. Routes

# /visit increments a shared counter
Add-KrMapRoute -Verbs Get -Pattern '/visit' -ScriptBlock {
    $Visits['Count']++
    Write-KrTextResponse -InputObject ('Visits now: {0}' -f $Visits['Count']) -StatusCode 200
}

# /schedule/report returns the aggregated report
Add-KrMapRoute -Verbs Get -Pattern '/schedule/report' -ScriptBlock {
    $report = Get-KrScheduleReport
    Write-KrJsonResponse -InputObject $report -StatusCode 200
}

## 9. Start server
Start-KrServer -CloseLogsOnExit

Step-by-step

  1. Logging β€” console sink for visibility
  2. Server β€” create host and listener (loopback:5000)
  3. Runtime β€” add PowerShell runtime and scheduler (4 runspaces)
  4. Shared state β€” global Visits counter injected into runspaces
  5. Enable configuration β€” finalize the pipeline
  6. Jobs β€” two heartbeats (PS every 10s; C# every 15s)
  7. Routes β€” /visit increments counter; /schedule/report returns a JSON report
  8. Start β€” run the server; observe log heartbeats and query endpoints

Try it

Invoke-RestMethod http://127.0.0.1:5000/visit -SkipCertificateCheck
Invoke-RestMethod http://127.0.0.1:5000/schedule/report -SkipCertificateCheck | ConvertTo-Json -Depth 4

You should see job entries similar to:

{
  "generatedAt": "2025-10-03T12:00:00Z",
  "jobs": [
    { "name": "heartbeat-ps", "lastRunAt": "…", "nextRunAt": "…", "isSuspended": false },
    { "name": "heartbeat-cs", "lastRunAt": "…", "nextRunAt": "…", "isSuspended": false }
  ]
}

Troubleshooting

Symptom Cause Fix
No jobs appear Scheduler not added or config not enabled Call Add-KrScheduling and Enable-KrConfiguration before Start-KrServer
Heartbeats not running Interval too long or suspended Check Get-KrScheduleSnapshot and adjust intervals/suspension
Report missing fields Depth too shallow Increase ConvertTo-Json -Depth when inspecting
Route 404s Pattern mismatch Verify route paths /visit and /schedule/report

References


Previous / Next

Previous: None Next: Scheduling with CRON