Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 50 additions & 2 deletions ParallelKit.Harness.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -967,6 +967,53 @@ $out = Invoke-PkRunspaceBatch -MaxConcurrent 2 -JobParams (New-JobList -Jobs @(1
-WorkerScript { param([int]$Id) if (Test-PkCancelRequested) { return }; return $Id } -ResultHandler { param($r) $results.Add($r) }.GetNewClosure()
Test-Assert (-not $out.Aborted -and $results.Count -eq 4) "after Reset-PkAbortState the next run is normal again"

# An abort requested between the engines' checks (Request-PkAbort with no
# Test-PkAbortSignal after it, or from a callback) starts nothing more.
Reset-Harness
Request-PkAbort
$results = [System.Collections.Generic.List[object]]::new()
$out = Invoke-PkRunspaceBatch -MaxConcurrent 4 -JobParams (New-JobList -Jobs @(1..8 | ForEach-Object { @{ Id = $_ } })) `
-WorkerScript { param([int]$Id) return $Id } -ResultHandler { param($r) $results.Add($r) }.GetNewClosure()
Test-Assert ($out.Aborted -and $results.Count -eq 0) ("runspace jobs: an abort requested before the run starts no worker (ran " + $results.Count + " of 8)")
Reset-Harness
Request-PkAbort
$asked = @{ Count = 0 }
$d = Invoke-PkProcessDispatch -MaxConcurrent 1 -GroupProvider { $asked.Count++; return $null }.GetNewClosure() -JobBuilder { param($g) } -JobFinalize { param($job, $pending) }
Test-Assert ($d.Aborted -and $asked.Count -eq 0) ("dispatch: an abort requested before the run asks -GroupProvider for nothing (asked " + $asked.Count + " times)")

Reset-Harness
$results = [System.Collections.Generic.List[object]]::new()
$out = Invoke-PkRunspaceBatch -MaxConcurrent 2 -JobParams (New-JobList -Jobs @(1..20 | ForEach-Object { @{ Id = $_ } })) `
-WorkerScript { param([int]$Id) [System.Threading.Thread]::Sleep(300); return $Id } `
-ResultHandler { param($r) $results.Add($r); if ($results.Count -eq 1) { Request-PkAbort } }.GetNewClosure()
Test-Assert ($out.Aborted -and $results.Count -eq 2) ("runspace jobs: the slots a -ResultHandler frees after requesting an abort aren't refilled (ran " + $results.Count + " of 20)")

Reset-Harness
$results = [System.Collections.Generic.List[object]]::new()
$feed = @{ Next = 0 }
$out = Invoke-PkRunspaceBatch -MaxConcurrent 4 -WorkerScript { param([int]$Id) [System.Threading.Thread]::Sleep(300); return $Id } `
-NextJobProvider { if ($feed.Next -ge 8) { return $null }; $feed.Next++; if ($feed.Next -eq 2) { Request-PkAbort }; return @{ Id = $feed.Next } }.GetNewClosure() `
-ResultHandler { param($r) $results.Add($r) }.GetNewClosure()
Test-Assert ($out.Aborted -and $feed.Next -eq 2 -and $results.Count -eq 2) ("runspace jobs: -NextJobProvider isn't asked again once it requests an abort (asked " + $feed.Next + " times, ran " + $results.Count + ")")

Reset-Harness
$starts = @{ Count = 0 }; $feed = @{ Given = $false }; $ticks = @{ Count = 0 }
$d = Invoke-PkProcessDispatch -MaxConcurrent 3 -AbortTimeoutSeconds 5 `
-GroupProvider { if ($feed.Given) { return $null }; $feed.Given = $true; return 'g' }.GetNewClosure() `
-JobBuilder { param($g) return @(1..6 | ForEach-Object { @{ FilePath = $cmdExe; Arguments = '/c exit 0' } }) }.GetNewClosure() `
-JobStarter { param($job) $starts.Count++; Start-PkProcess -FilePath $job['FilePath'] -Arguments $job['Arguments'] }.GetNewClosure() `
-JobFinalize { param($job, $pending) $null = Complete-PkProcess -Job $job } `
-PollCallback { param($running) $ticks.Count++; if ($ticks.Count -eq 1) { Request-PkAbort } }.GetNewClosure()
Test-Assert ($d.Aborted -and $starts.Count -eq 0) ("dispatch: a -PollCallback that requests an abort starts nothing more on that pass (started " + $starts.Count + " of 6)")

Reset-Harness
$late = @{ Groups = 0; Aborting = $false; LateBuilds = 0 }
$d = Invoke-PkProcessDispatch -MaxConcurrent 1 -MaxQueueAhead 1 -AbortTimeoutSeconds 5 `
-GroupProvider { if ($late.Groups -ge 5) { return $null }; $late.Groups++; return $late.Groups }.GetNewClosure() `
-JobBuilder { param($g) if ($late.Aborting) { $late.LateBuilds++ }; return @(@{ FilePath = $cmdExe; Arguments = '/c exit 0' }) }.GetNewClosure() `
-JobFinalize { param($job, $pending) $null = Complete-PkProcess -Job $job; if (-not $late.Aborting) { $late.Aborting = $true; Request-PkAbort } }.GetNewClosure()
Test-Assert ($d.Aborted -and $late.Groups -eq 1 -and $late.LateBuilds -eq 0) ("dispatch: a -JobFinalize that requests an abort gets no more groups from -GroupProvider (asked for " + $late.Groups + " group(s), " + $late.LateBuilds + " built after the abort)")

# Your own long step inside Invoke-PkRun has to poll.
Reset-Harness
$own = @{ Steps = 0 }
Expand Down Expand Up @@ -1725,13 +1772,14 @@ Test-Assert $threw "a negative -TimeoutSeconds is refused"

# An aborted group still gets -GroupCompleteCallback (the README warns about
# this - a caller zipping a folder there must check for the abort first).
# The abort comes once the group is built: one already in force means no
# group is asked for at all.
Reset-Harness
Request-PkAbort
$called = @{ Count = 0; SawAbort = $false }
$feed = @{ Given = $false }
$d = Invoke-PkProcessDispatch -MaxConcurrent 1 `
-GroupProvider { if ($feed.Given) { return $null }; $feed.Given = $true; return 'g' }.GetNewClosure() `
-JobBuilder { param($g) return @(@{ FilePath = $cmdExe; Arguments = '/c exit 0' }) }.GetNewClosure() `
-JobBuilder { param($g) Request-PkAbort; return @(@{ FilePath = $cmdExe; Arguments = '/c exit 0' }) }.GetNewClosure() `
-JobFinalize { param($job, $pending) $null = Complete-PkProcess -Job $job } `
-GroupCompleteCallback { param($g) $called.Count++; $called.SawAbort = [bool](Test-PkCancelRequested) }.GetNewClosure()
Test-Assert ($d.Aborted -and $called.Count -eq 1 -and $called.SawAbort) "an aborted group still gets its -GroupCompleteCallback, and Test-PkCancelRequested is true inside it"
Expand Down
24 changes: 23 additions & 1 deletion ParallelKit.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -2735,6 +2735,13 @@ function Invoke-PkProcessDispatchCore {
while ((-not $pkCancelToken.IsCancellationRequested) -and `
(-not $pkProviderExhausted -or $pkActiveGroupCount -gt 0 -or $pkPending.Count -gt 0 -or $pkRunning.Count -gt 0)) {

# An abort only raises a flag; Test-PkAbortSignal is what cancels the
# token. Checked first, so an abort raised since the last pass (by
# -JobFinalize, -GroupCompleteCallback, Ctrl+C during the wait) asks
# -GroupProvider for nothing more. The flag read is cheaper than a
# function call.
if ([PkConsoleCtrl]::AbortRequested) { $null = Test-PkAbortSignal }

for ($pkH = $pkHeldGroups.Count - 1; $pkH -ge 0; $pkH--) {
$pkReleased = Invoke-PkCallback -PkCallback $pkGroupReleaseCheck -PkArgs @(,$pkHeldGroups[$pkH]) -PkName '-GroupReleaseCheck' -PkContext $pkCallbackContext
# Only the check's last value counts, as with -JobPollCompleted: a
Expand Down Expand Up @@ -2802,6 +2809,8 @@ function Invoke-PkProcessDispatchCore {
$pkCbDone = $false
$null = do { & $pkPollCallback @($pkRunning); $pkCbDone = $true } while ($false)
if (-not $pkCbDone) { Write-PkCallbackLoopWarning -PkName '-PollCallback' -PkContext $pkCallbackContext }
# And again, so an abort it raised starts nothing on this pass.
if ([PkConsoleCtrl]::AbortRequested) { $null = Test-PkAbortSignal }
}

while ($pkPending.Count -gt 0 -and (-not $pkCancelToken.IsCancellationRequested) -and `
Expand Down Expand Up @@ -3387,6 +3396,13 @@ function Invoke-PkProcessBatch {
Write-PkMessage ("Invoke-PkProcessBatch: " + $pkFailure.More + " more " + $pkCommandWord + " couldn't start '" + $pkFailure.FilePath + "' (" + $pkFailure.Cause + ").") -Level Error
}
}
# An abort already in force stops the dispatcher before it asks for the
# batch's one group, so no command reached it: each was kept from starting.
if (-not $pkState.Given) {
foreach ($pkC in $pkCommands) {
if ($null -ne $pkC) { $pkResults.Add((& $pkNewBatchResult -Command $pkC -Aborted)) }
}
}

return [pscustomobject][ordered]@{ Results = $pkResults; Aborted = $pkDispatch.Aborted; ForceKilledCount = $pkDispatch.ForceKilledCount }
}
Expand Down Expand Up @@ -3803,7 +3819,13 @@ function Invoke-PkRunspaceBatch {

while ((-not $scanExhausted -or $handles.Count -gt 0) -and -not $Cts.Token.IsCancellationRequested) {

while (-not $scanExhausted -and $handles.Count -lt $MaxConcurrent -and -not $Cts.Token.IsCancellationRequested) {
# An abort only raises a flag; Test-PkAbortSignal is what cancels
# the token. Checked before launching, so an abort raised since the
# last check (before the run, or by a -ResultHandler) starts
# nothing more, and per job, for one -NextJobProvider raises. The
# flag read is cheaper than a function call.
if ([PkConsoleCtrl]::AbortRequested) { $null = Test-PkAbortSignal }
while (-not $scanExhausted -and $handles.Count -lt $MaxConcurrent -and -not $Cts.Token.IsCancellationRequested -and -not [PkConsoleCtrl]::AbortRequested) {
if ($null -ne $JobParams) {
if ($fixedListIndex -lt $JobParams.Count) {
$nextJob = $JobParams[$fixedListIndex]
Expand Down