Skip to content

Instantly share code, notes, and snippets.

@royashbrook
Created April 19, 2021 14:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save royashbrook/1bf9beda5e587d00c848e22af7fa83d0 to your computer and use it in GitHub Desktop.
Save royashbrook/1bf9beda5e587d00c848e22af7fa83d0 to your computer and use it in GitHub Desktop.
Retrieve async job data using a web server
$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