Friday, June 27, 2014

Powershell - Adding Concurrency in Powershell 3 and 4

Our scheduler was throwing errors when evoking a decompression routine I wrote in Powershell. The requirement was to evoke the decompression script many times concurrently with different file name parameter values. Due to limitations in the command field length in the scheduler, we had to call my script from a bat file, which was swallowing any of my errors and output.

I had a few scenarios I had to test.
Can a simple Powershell script be called a second time while is still executing.  I had assumed it could but needed to prove this to move to more relevant scenarios.

I was unsure if my code was written to run concurrently. Rather than confuse my code with concurrency, I wrote a simple test script and a simple concurrency test. This proved I could evoke a standard PS script at least 6 times in the same nano second using  different parameters, without any issue.

So calling the script itself was not an issue. I was unsure whether my code was thread safe. I was also unsure whether the scheduled jobs were written correctly. I saved the thread safety test for last, assuming the issue was with the way my code was being called in the scheduler.



Create a file named ConcurrencyTest.ps1
param(
[INT]$WAITER=$(throw "You did not provide a value for WAITER parameter.")
)

$time=Get-Date -format o| foreach {$_ -replace ":", "."}
$fileName=".\OutPut$time.txt"
New-Item $fileName -type file
Write-output "start $WAITER $time" > $fileName
Start-Sleep $Waiter
$time=Get-Date -format o| foreach {$_ -replace ":", "."}
Write-output "end attempt $WAITER $time" >> $fileName

Create a file named ConcurrencyWorkflow.ps1

workflow RunStuffParallel
{
    $executionAttempts=@(1..100)

    ForEach -Parallel ($Attempt in $ExecutionAttempts)
    {
        Invoke-Expression ".\ConcurrencyTest.ps1 $Attempt"  #adds some variability      
#        Invoke-Expression ".\ConcurrencyTest.ps1 5" #hard coded adds linearity       
    }
}

RunStuffParallel

No comments:

Post a Comment