Skip to content

Instantly share code, notes, and snippets.

@RazmikDev
Last active August 29, 2015 14:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RazmikDev/f8aab1433190168ee6ab to your computer and use it in GitHub Desktop.
Save RazmikDev/f8aab1433190168ee6ab to your computer and use it in GitHub Desktop.
Sceleton of scheduler that performs tasks with a parallelism level equals to 1
$jobName = "SchedulerJOB"
$schedulePath = Join-Path $PSScriptRoot "Schedule.json"
$epoch = Get-Date -Year 1970 -Day 1 -Month 1
function Read-WorkItems()
{
$json = Get-Content -Path $schedulePath -Raw
$workItems = (ConvertFrom-Json $json)
$workItems
}
function Process-WorkItems()
{
$workItems = Read-WorkItems
foreach($workItem in $workItems)
{
$lastTimeInvoked = $workItem.LastTimeInvoked
$now = (New-TimeSpan -Start $epoch -End (Get-Date)).TotalSeconds
$passed = $now - $lastTimeInvoked
if($passed -gt $workItem.Interval)
{
& $workItem.Script
}
else
{
& $workItem.Script
}
}
}
function Scheduler-Start()
{
$inteval = New-TimeSpan -Minutes 1
$trigger = New-JobTrigger -Once -At ([DateTime]::UtcNow) -RepetitionInterval $inteval -RepeatIndefinitely
Register-ScheduledJob -Name $jobName -ScriptBlock {Process-WorkItems} -Trigger $trigger
}
function Scheduler-Stop()
{
Unregister-ScheduledJob -Name $jobName
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment