-
Notifications
You must be signed in to change notification settings - Fork 17
/
multi-job-proto.ps1
171 lines (142 loc) · 4.54 KB
/
multi-job-proto.ps1
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
param(
[switch]$start,
[int]$throttle = 20
)
$global:jobs = New-Object Collections.ArrayList
# ----------------------------------------------------------------------------------------------------------------
function main()
{
try
{
get-job | remove-job -Force
$jobInfos = New-Object Collections.ArrayList
# add values here to pass to jobs
$jobInfo = @{}
$jobInfo.jobName = [IO.Path]::GetFileNameWithoutExtension($MyInvocation.Scriptname)
$jobInfo.invocation = $MyInvocation
$JobInfo.backgroundJobFunction = (get-item function:do-backgroundJob)
$jobInfo.action = "test"
$jobInfo.result = $null
$jobInfos.Add($jobInfo)
start-backgroundJobs -jobInfos $jobInfos -throttle $throttle
monitor-backgroundJobs
log-info "finished"
}
finally
{
get-job | remove-job -Force
}
}
# ----------------------------------------------------------------------------------------------------------------
function log-info($data)
{
$dataWritten = $false
$data = "$([System.DateTime]::Now):$($data)`n"
write-host $data
$counter = 0
while (!$dataWritten -and $counter -lt 1000)
{
try
{
out-file -Append -InputObject $data -FilePath $logFile
$dataWritten = $true
}
catch
{
Start-Sleep -Milliseconds 10
$counter++
}
}
}
# ----------------------------------------------------------------------------------------------------------------
function do-backgroundJob($jobInfo)
{
$count = 0
while ($true)
{
log-info "doing background job $($jobInfo.action)"
log-info ($jobInfo.action)
"================"
$jobInfo.result = $count
$jobInfo
Start-Sleep -Seconds 1
$count++
}
}
# ----------------------------------------------------------------------------------------------------------------
function check-backgroundJobs()
{
foreach ($job in get-job)
{
if ($job.State -ine "Running")
{
#log-info ($job | fl * | out-string)
Remove-Job -Id $job.Id -Force
}
else
{
$jobInfo = Receive-Job -Job $job
#log-info ($jobInfo | fl * | out-string)
}
Start-Sleep -Seconds 1
}
return @(get-job).Count
}
# ----------------------------------------------------------------------------------------------------------------
function monitor-backgroundJobs()
{
while ((check-backgroundJobs))
{
Start-Sleep -Seconds 1
}
}
# ----------------------------------------------------------------------------------------------------------------
function remove-backgroundJobs()
{
foreach($job in get-job)
{
write-verbose "removing job"
write-verbose (Receive-Job -Job $Job | fl * | out-string)
Write-Verbose (Remove-Job -Job $job -Force)
}
}
#-------------------------------------------------------------------
function start-backgroundJob($jobInfo)
{
log-info "starting background job"
$job = Start-Job -ScriptBlock `
{
param($jobInfo)
$ctx = $null
#background job for bug https://github.com/Azure/azure-powershell/issues/7110
#Disable-AzureRmContextAutosave -scope Process -ErrorAction SilentlyContinue | Out-Null
. $($jobInfo.invocation.scriptname)
$ctx = Import-AzureRmContext -Path $jobInfo.profileContext
# bug to be fixed 8/2017
# From <https://github.com/Azure/azure-powershell/issues/3954>
[void]$ctx.Context.TokenCache.Deserialize($ctx.Context.TokenCache.CacheData)
log-info ($jobInfo.action)
#do-backgroundJob -jobInfo $jobInfo
& $jobInfo.backgroundJobFunction $jobInfo
} -Name $jobInfo.jobName -ArgumentList $jobInfo
return $job
}
# ----------------------------------------------------------------------------------------------------------------
function start-backgroundJobs($jobInfos, $throttle)
{
log-info "starting background jobs"
foreach ($jobInfo in $jobInfos)
{
while ((check-backgroundJobs) -gt $throttle)
{
Write-Verbose "throttled"
Start-Sleep -Seconds 1
}
[void]$global:jobs.Add((start-backgroundJob -jobInfo $jobInfo))
}
}
# ----------------------------------------------------------------------------------------------------------------
if ($host.Name -ine "ServerRemoteHost")
{
main
}