Skip to content

Instantly share code, notes, and snippets.

@trondhindenes
Created September 16, 2014 09:34
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 trondhindenes/cb5c54ccae6ca7c1f884 to your computer and use it in GitHub Desktop.
Save trondhindenes/cb5c54ccae6ca7c1f884 to your computer and use it in GitHub Desktop.
Description of a situation where a runbook will fail to run, even if the necessary "child runbook" is in place
#Configure the environment
$smawebserviceurl = "https://trondcloud-cs.cloudapp.net"
$smacreds = get-credential
#Set up two test runbooks, one referenced by the other (testwf1 references testwf2)
$TestWF1 = @"
workflow testwf1
{
Param ([switch]$Test)
if ($test)
{
return
}
testwf2
}
"@
$TestWF2 = @"
workflow testwf2
{
Write-output "this is testwf2"
}
"@
#Set the default params since I'm lazy
$PSDefaultParameterValues.Add("*sma*:WebServiceEndpoint",$smawebserviceurl)
$PSDefaultParameterValues.Add("*sma*:credential",$smacreds)
#Upload and publish the first runbook
#SMA doesn't support taking the contents from a var, so stream it to a temp file
$wf1tempfile = join-path ($env:temp) (get-random)
Set-Content -path $wf1tempfile -Value $TestWF1 -Force
$rb1 = Import-SmaRunbook -Path $wf1tempfile
remove-item $wf1tempfile -Confirm:$false
Publish-SmaRunbook -Id $rb1.RunbookID
#Start the first runbook. Execution should fail, as its missing its child workflow
$rb1FirstStart = Start-SmaRunbook -Id $rb1.RunbookID
Do
{
#Skip waiting if its the first run
if ($firstrun -eq $false) {Start-sleep -Seconds 2}
$firstrun = $false
$jobstatus = Get-SmaJob -Id $rb1FirstStart
}
until ("Failed","Completed" -contains $jobstatus.JobStatus)
#We expect this to fail, as we havent upladed the child workflow yet
if ($jobstatus.JobStatus -eq "failed")
{
Write-host $jobstatus.JobException
}
#Okay, it failed because its missing testwf2. Lets upload that and see what happens
#Upload the second (child) runbook
#SMA doesn't support taking the contents from a var, so stream it to a temp file
$wf2tempfile = join-path ($env:temp) (get-random)
Set-Content -path $wf2tempfile -Value $TestWF2 -Force
$rb2 = Import-SmaRunbook -Path $wf2tempfile
remove-item $wf2tempfile -Confirm:$false
#Start the runbook just to see that it runs
Publish-SmaRunbook -Id $rb2.RunbookID
$rb2FirstStart = Start-SmaRunbook -Id $rb2.RunbookID
Do
{
#Skip waiting if its the first run
if ($firstrun -eq $false) {Start-sleep -Seconds 2}
$firstrun = $false
$jobstatus = Get-SmaJob -Id $rb2FirstStart
}
until ("Failed","Completed" -contains $jobstatus.JobStatus)
#We're assuming testwf2 ran fine, since it doesnt have any depencies/children
Write-host "wf2 status: $($jobstatus.JobStatus)"
#Lets try running wf1 again, now that all child runbooks are in place:
$rb1SecondStart = Start-SmaRunbook -Id $rb1.RunbookID
Do
{
#Skip waiting if its the first run
if ($firstrun -eq $false) {Start-sleep -Seconds 2}
$firstrun = $false
$jobstatus = Get-SmaJob -Id $rb1SecondStart
}
until ("Failed","Completed" -contains $jobstatus.JobStatus)
#Testwf1 fails again, although the required child workflow is in place:
write-host $jobstatus.JobException
#This is the bug. The only way to get aroud it, is to delete/republish testwf1, either in the gui or in code:
Remove-SmaRunbook -Id $rb1.RunbookID
#Re-upload testwf1
$wf1tempfile = join-path ($env:temp) (get-random)
Set-Content -path $wf1tempfile -Value $TestWF1 -Force
$rb1 = Import-SmaRunbook -Path $wf1tempfile
remove-item $wf1tempfile -Confirm:$false
Publish-SmaRunbook -Id $rb1.RunbookID
$rb1ThirdStart = Start-SmaRunbook -Id $rb1.RunbookID
Write-host "Starting re-uploaded runbook"
Do
{
#Skip waiting if its the first run
if ($firstrun -eq $false) {Start-sleep -Seconds 2}
$firstrun = $false
$jobstatus = Get-SmaJob -Id $rb1ThirdStart
}
until ("Failed","Completed" -contains $jobstatus.JobStatus)
#This time, the runbook succeeded
write-host "Status of re-uploaded testwf1: $($jobstatus.JobStatus)"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment