因为线上的一个Stream Analytics job服务运行不是很稳定(Stream Analytics job 目前是public perview版本),所以需要重启让它重新正常工作.由于这是整个服务的数据流中必不可少的一个部分.如果这里阻塞了将导致下游的服务都不能正常运行,所以势必需在出问题后立刻重启(手动是不可能手动的,能自动化为什么要手动).所以就是为什么需要用到Runbook来自动化重启Stream Analytics job这个服务了.
1.编写Runbook
1.我们首先需要一个建立一个 Automation Account, 然后新建一个连接字符串.以后 PowerShell Runbook 登录授权需要用到

2.然后新建一个 PowerShell Runbook,本次重启 Stream Analytics job 的 代码参考 附录

3.这次我们的 Runbook 我们需要操作 Stream Analytics job 所以我们需要导入一个 Stream Analytics job 的包

4.在准备完毕后我们还需要 publish 我们的 Runbook,否则别的地方无法调用未 publish 的 Runbook
2.如何调用Runbook
1.在 portal 中进入Stream Analytics job的页面,创建一个规则

2.添加一个 action group 并且点击 group name 进入详细的设置

3.在 Actions 中添加一个 AutoMation Runbook 然后选择你想要执行的 Runbook

4.如果没找到需要执行的 Runbook, 请检查下 Runbook source 是否选中了正确的 Subscription, 已经 Runbook 是否已经 publish
3.附录
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# <> 中的均为敏感参数,根据自己资源情况输入正确的参数
Import-Module Az.StreamAnalytics
function Get-ASA-State {
try {
$streamAnalyticsJob = Get-AzStreamAnalyticsJob -ResourceGroupName <your ResourceGroupName> -Name <your ResourceName>
$jobState = $streamAnalyticsJob.JobState;
Write-Verbose "$($streamAnalyticsJob.Name) current state: $jobState" -Verbose
return $jobState
} catch {
$ErrorMessage = "Get streamAnalytics info failed."
throw $ErrorMessage
}
}
# connectStr 就是图1-1中添加的 connection 的 name 属性
$connection = Get-AutomationConnection -Name <connectStr>
Write-Verbose "Logging in to Azure..." -Verbose
$connectionResult = Connect-AzAccount `
-ServicePrincipal `
-Tenant $connection.TenantID `
-ApplicationId $connection.ApplicationID `
-CertificateThumbprint $connection.CertificateThumbprint
Write-Verbose "Login successful." -Verbose
# 切换到资源所在的 Subscription
Write-Verbose "---------------- set subscriptionId ------------------" -Verbose
$context = Get-AzSubscription -SubscriptionId <your SubscriptionId>
Set-AzContext $context
Write-Verbose "----------- Set subscriptionId successful ------------" -Verbose
try {
Stop-AzStreamAnalyticsJob -ResourceGroupName <your ResourceGroupName> -Name <your ResourceName>
$curentState = Get-ASA-State
if ($curentState -eq "Stopped") {
Write-Verbose "------------ ASA is stopped, begin restart -------------" -Verbose
Start-AzStreamAnalyticsJob -ResourceGroupName <your ResourceGroupName> -Name <your ResourceName> -OutputStartMode LastOutputEventTime
$curentState = Get-ASA-State
if ($curentState -eq "Running") {
Write-Verbose "------------ ASA restart successful -------------" -Verbose
} else {
Write-Error "Started ASA failed, please started ASA manually."
}
} else {
Write-Error "Stopped ASA failed, please stopped ASA manually."
}
} catch {
throw $_.Exception
}