在Runbook中添加Checkpoint-workflow

本文說明的是使用Checkpoint-workflow的一種場景(固然還有其餘場景須要Checkpoint-workflow)。session

原由:Windows Azure對Automation帳戶中的Runbook運行時長限制了30minutes。app

機制:就是若是你的腳本在30minutes內沒有運行完成, 那麼執行腳本的Worker會暫定你的腳本,去處理其餘帳戶的任務。ide

問題:當下一個Worker再來執行您的腳本時,是從最近一次的Checkpoint開始的。若是你未能在腳本執行過程當中設置Checkpoint而你的腳本執行時長又大於30minutes,那邊你的腳本每次都會從頭開始。oop

這樣,永無止境的執行下去,直到系統檢測到這種狀況並給你拋出異常。this

The job cannot continue running because it was repeatedly evicted from the same checkpoint. Please make sure your Runbook does not perform lengthy operations without persisting its state.spa

 

Checkpoint Workflowscala

<Activity1>
Checkpoint-Workflow
<Activity2>
<Error>
<Activity3>

 

注意的是Checkpoint-Workflow不能放在Inlinescript當中。3d

 

關於Checkpoint,參考 http://azure.microsoft.com/en-us/blog/azure-automation-reliable-fault-tolerant-runbook-execution-using-checkpoints/code

 

some good points about checkpoint-workflow from Runbook concepts(https://technet.microsoft.com/en-us/library/Dn469257.aspx)orm

You can set a checkpoint in a workflow with the Checkpoint-Workflow activity. When you include this activity in a runbook, a checkpoint is immediately taken. If the runbook is suspended by an error, when the job is resumed, it will resume from the point of the last checkpoint set.

Some good points about InlineScript 

The InlineScript activity runs a block of commands in a separate, non-workflow session and returns its output to the workflow. While commands in a workflow are sent to Windows Workflow Foundation for processing, commands in an InlineScript block are processed by Windows PowerShell. The activity uses the standard workflow common parameters including PSComputerName and PSCredential which allow you to specify that the code block be run on another computer or using alternate credentials.

While InlineScript activities may be critical in certain runbooks, they should only be used when necessary for the following reasons:

  • You cannot use checkpoints from within an InlineScript block. If a failure occurs within the block, it must be resumed from the beginning.

  • InlineScript affects scalability of the runbook since it holds the Windows PowerShell session for the entire length of the InlineScript block.

  • Activities such as Get-AutomationVariable and Get-AutomationPSCredential are not available in an InlineScript block.

you do need to use an InlineScript, you should minimize its scope. For example, if your runbook iterates over a collection while applying the same operation to each item, the loop should occur outside of the InlineScript. This will provide the following advantages:

  • You can checkpoint the workflow after each iteration. If the job is suspended or interrupted and resumed, the loop will be able to resume.

  • You can use ForEach –Parallel to handle collection items concurrently.

Keep the following recommendations in mind if you do use an InlineScript in your runbook:

  • You can pass values into the script though with the $Using scope modifier. For example, a variable called $abc that has been set outside of the InlineScript would become $using:abc inside an InlineScript.

  • To return output from an InlineScript, assign the output to a variable and output any data to be returned to the output stream. The following example assigns the string 「hi」 to a variable called $output.

     
    $output = InlineScript { Write-Output "hi" }
    
  • Avoid defining workflows within InlineScript scope. Even though some workflows may appear to operate correctly, this is not a tested scenario. As a result, you may encounter confusing error messages or unexpected behavior.

相關文章
相關標籤/搜索