Created
December 21, 2011 14:32
-
-
Save possan/1506235 to your computer and use it in GitHub Desktop.
Background web worker with colorizing output
This file contains hidden or 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
param ( | |
$mode = $null, | |
$num = 1, | |
$url = $null | |
) | |
if( $mode -eq $null -or $url -eq $null ) { | |
write-host "Syntax: background.ps1 [start|loop] [1-100] [url]" | |
exit 1 | |
} | |
$name = "BgWeb" | |
function getonce($url) { | |
write-host "Sending request: $url" | |
$request = [System.Net.WebRequest]::Create( $url ) | |
$response = $request.GetResponse() | |
$requestStream = $response.GetResponseStream() | |
$readStream = new-object System.IO.StreamReader $requestStream | |
new-variable db | |
while( $db = $readStream.ReadLine() ) { | |
if( $db.StartsWith( "DEBUG" ) ) { | |
write-host -foregroundcolor darkcyan $db | |
} elseif( $db.StartsWith( "WARN" ) ) { | |
write-host -foregroundcolor yellow $db | |
} elseif( $db.StartsWith( "ERROR" ) ) { | |
write-host -foregroundcolor red $db | |
} elseif( $db.StartsWith( "INFO" ) ) { | |
write-host -foregroundcolor cyan $db | |
} else { | |
write-host $db | |
} | |
} | |
write-host "Done."; | |
$readStream.Close() | |
$response.Close() | |
} | |
if( $mode -eq "start" ) { | |
Write-Host "Starting $num workerloops..." | |
$path = $MyInvocation.MyCommand.Definition | |
$i = 1; | |
do { | |
Start-process powershell "$path loop 0 $url" | |
$i++ | |
} | |
while ($i -le $num) | |
} | |
elseif( $mode -eq "loop" ) { | |
$rn = New-Object System.Random | |
while( 1 ) { | |
$d = $rn.next( 500, 4000 ); | |
write-host "Delaying $d ms..." | |
Start-Sleep -m $d | |
getonce( $url ) | |
} | |
} | |
else { | |
write-host "Unknown mode $mode " | |
exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment