Last active
August 29, 2015 14:19
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$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