Starting Situation
- JobScheduler executes jobs asynchronously, i.e. the completion of a job or job chain takes place independently from the caller that launched a job or order.
- There are use cases when the calling PowerShell script wants to receive execution results synchronously:
- A job is launched and the calling script should wait for completion of the job.
- An order is launched and the calling script should wait for completion of the job chain.
Use Cases
Wait for completion of a temporary order
The following sample explains how to wait for completion of an order:
Import-Module JobScheduler Connect-JobScheduler http://localhost:4446 echo "begin of test script" $order = Add-JobSchedulerOrder -JobChain /some_path/some_job_chain While ( ( $order | Get-JobSchedulerOrder ).volatile.nextStartTime ) { Start-Sleep -Seconds 10 } $orderHistory = $order | Get-JobSchedulerOrder -WithHistory echo "order $($orderHistory.OrderId) completed with state '$($orderHistory.volatile.processingState._text)'" echo "end of test script"
Explanations
- Line 1: The
Import-Module
statement is used if the JobScheduler CLI module is not loaded from a profile - Line 6: Adds a temporary ad hoc order to the specified job chain, see
Add-JobSchedulerOrder
.- The order identification is not specified but is generated by the JobScheduler Master.
- The resulting
$order
object contains the order identification.
- Line 7 - 10: Repeated checks are executed in a loop to verify if the order is completed.
- The newly added order is piped to the
Get-JobSchedulerOrder
cmdlet that returns an object with anextStartTime
property should the order still be running. - A sleep interval reduces the frequency of checks for completion of the current order.
- The newly added order is piped to the
- Line 12: Permanent orders are permanently available from the JobScheduler memory. Temporary ad hoc orders are removed from the JobScheduler memory after completion. Therefore the temporary ad hoc order is retrieved from the order history instead of the JobScheduler memory.
- Line 13: The order history provides information about the order's end time and end state that can be used to identify successful or failed execution.
Use a native PowerShell job to wait for completion of a temporary order
The following sample shows how to use native PowerShell jobs to run a number of JobScheduler orders in parallel and to wait for completion of all orders. As orders are executed in parallel by JobScheduler it is more effective to use asynchronous native PowerShell jobs than to wait for each native PowerShell job individually as from the above sample:
$jobScript = { Param ( [Uri] $url, [string] $jobChain, [int] $pollInterval=10 ) Import-Module JobScheduler if ( $url ) { $js = Connect-JobScheduler $url } $order = Add-JobSchedulerOrder -JobChain $jobChain While ( ( $order | Get-JobSchedulerOrder ).nextStartTime ) { Start-Sleep -Seconds $pollInterval } $order | Get-JobSchedulerOrder -WithHistory } echo "begin of test script" $jobs = @() $jobs += Start-Job -ScriptBlock $jobScript -Argumentlist localhost:4446,/some_path/some_job_chain $jobs += Start-Job -ScriptBlock $jobScript -Argumentlist localhost:4446,/some_path/some_other_job_chain $orderHistory = $jobs | Wait-Job | Receive-Job for( $i=0; $i -lt $orderHistory.length; $i++ ) { echo "order '$($orderHistory[$i].orderId)' completed with state '$($orderHistory[$i].volatile.processingState._text)'" } echo "end of test script"
Explanations
- Line 1 - 18: defines the native PowerShell job.
- The cmdlets used are the same as for the previous sample.
- The native PowerShell job returns the history for the newly created order.
- Line 23 - 24: Starts the native PowerShell job
- Line 26: Pipes the native PowerShell job to the native
Wait-Job
cmdlet that forces the script to wait for completion. The result is then piped to theReceive-Job
cmdlet that returns the output of the native PowerShell job. - Line 28 - 31: The job output is retrieved per native PowerShell job.