Created
April 19, 2021 14:16
-
-
Save royashbrook/1bf9beda5e587d00c848e22af7fa83d0 to your computer and use it in GitHub Desktop.
Retrieve async job data using a web server
This file contains 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
$sb = { | |
do{ #pause while server is starting up | |
$error.Clear() | |
try{ | |
[Net.Sockets.TcpClient]::new('localhost',8080) | |
}catch{ | |
Start-Sleep 1 | |
} | |
}while($error) | |
for ($i = 0; $i -lt $args[1]; $i++) { | |
$waitfor = (get-random 7) | |
$body = "{0} here. I'm on loop $i of {1}. Pausing for $waitfor seconds" -f $args[0],$args[1] | |
irm http://localhost:8080 -body $body | |
Start-Sleep $waitfor | |
} | |
} | |
0..(get-random -min 2 -max 6) | %{ | |
$null = Start-Job -ScriptBlock $sb -ArgumentList "Job $_",(get-random -min 5 -max 35) | |
} | |
$http = [System.Net.HttpListener]::new() | |
$http.Prefixes.Add("http://localhost:8080/") | |
$http.Start() | |
"started" | |
while ($http.IsListening){ | |
$cx = $http.GetContext() | |
[System.IO.StreamReader]::new($cx.Request.InputStream).ReadToEnd() | |
$cx.Response.OutputStream.Close() | |
if($cx.Request.RawUrl -contains '/kill'){break;} | |
} | |
$http.Dispose() | |
get-job | remove-job -force |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment