-
-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathrunspaceWorkerCode.ps1
More file actions
95 lines (82 loc) · 2.58 KB
/
Copy pathrunspaceWorkerCode.ps1
File metadata and controls
95 lines (82 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
[PSFramework.Runspace.RSWorker]::WorkerCode = {
param (
$__PSF_WorkerID
)
# $__PSF_Workflow --> Workload Workflow provided by worker
# $__PSF_Worker --> Current Worker Definition
$ErrorActionPreference = 'Stop'
trap {
Write-PSFMessage -Level Error -Message "Runspace Worker Code failed" -ErrorRecord $_
$__PSF_Worker.State = 'Failed'
$__PSF_Worker.SignalEnd()
throw $_
}
try {
[runspace]::DefaultRunspace.Name = 'PSF-{0}-{1}-{2}' -f $__PSF_Workflow.Name, $__PSF_Worker.Name, $__PSF_WorkerID
}
catch {
# No action - name is desirable, but cosmetic
}
# Generally, Constants are to be avoided. To guarantee no childcode can override the code to be executed, this is made constant.
Set-Variable -Name __PSF_Begin -Value $__PSF_Worker.GetBegin() -Option Constant
Set-Variable -Name __PSF_ScriptBlock -Value $__PSF_Worker.ScriptBlock -Option Constant
Set-Variable -Name __PSF_End -Value $__PSF_Worker.GetEnd() -Option Constant
# Consume per-Runspace Values as variables
foreach ($key in $__PSF_Worker.PerRSValues.Keys) {
Set-Variable -Name $key -Value $__PSF_Worker.PerRSValues[$key].Dequeue()
}
if ($__PSF_Begin) {
try { $null = & $__PSF_Begin }
catch {
$__PSF_Worker.ErrorCount++
$__PSF_Worker.LastError = $_
# End worker right away to ensure the error is not drowned in subsequent errors
return
}
}
$validStates = 'Starting', 'Running'
while ($validStates -contains $__PSF_Worker.State) {
# Inqueue is closed and all items processed?
if ($__PSF_Worker.IsDone) { break }
if ($__PSF_Worker.MaxItems -and $__PSF_Worker.MaxItems -le $__PSF_Worker.CountInputCompleted) { break }
if ($__PSF_Worker.Throttle) {
$__PSF_Worker.Throttle.GetSlot()
}
$inputData = $null
$success = $__PSF_Worker.GetNext([ref]$inputData)
if (-not $success) {
Start-Sleep -Milliseconds 250
continue
}
try {
$results = $__PSF_ScriptBlock.InvokeGlobal($inputData)
foreach ($result in $results) {
if ($__PSF_Worker.NoOutput) { break }
$__PSF_Workflow.Queues.$($__PSF_Worker.OutQueue).Enqueue($result)
$__PSF_Worker.IncrementOutput()
}
$__PSF_Worker.IncrementInputCompleted()
}
catch {
$__PSF_Worker.IncrementInputCompleted()
$__PSF_Worker.ErrorCount++
$__PSF_Worker.AddError($_, $inputData)
}
}
if ($__PSF_End) {
try { $null = & $__PSF_End }
catch {
$__PSF_Worker.ErrorCount++
$__PSF_Worker.LastError = $_
}
}
$__PSF_Worker.SignalEnd()
}
[PSFramework.Runspace.RSWorker]::WorkerBeginCode = {
param ($Code)
& $Code
}
[PSFramework.Runspace.RSWorker]::WorkerProcessCode = {
param ($Code, $Data)
$Code.InvokeGlobal($Data)
}