Home > Software design >  While loop not closing after defined in lambda
While loop not closing after defined in lambda

Time:08-04

On occasion... I have an issue where the following while code does not close it's loop after it should close:

$cfn_stk_status = $null
$count = 0
while ($cfn_stk_status.StackStatus.Value -ne "CREATE_COMPLETE" -or $count -ne 10) {
    $cfn_stk_status = Get-CFNStack -StackName $cuurent_deployment -region $region 
    Write-Host "Deploying Stack $($cuurent_deployment) for Task: $($new_task) $($task_id) $count" $cfn_stk_status.StackStatus.Value
    Start-Sleep 10
    $count  
}

This does not happen every time. Correct me if I'm wrong, there's no reason that after the count hits 10 this should continue, correct? I cannot reproduce this so far on the command line. The $count will continue to iterate every 10 seconds until the lambda times out. If this is indeed a bug I'm wondering where the best place to post this issue would be.

CodePudding user response:

There's a logic flaw in your while conditional; use -and instead of -or:

while (
  $cfn_stk_status.StackStatus.Value -ne "CREATE_COMPLETE" -and $count -ne 10
) { # ...

That way, the loop is exited once $count hits 10 even if the status indicates that creation hasn't completed yet.

With -or, the first -ne operation alone is sufficient to keep the loop going, so it will run for as long as the creation hasn't completed yet, irrespective of the $count value.

  • Related