-
Notifications
You must be signed in to change notification settings - Fork 55
/
Copy pathRunQueue.ps1
138 lines (111 loc) · 4.85 KB
/
RunQueue.ps1
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# Run this every x minutes from Task Scheduler
$ErrorActionPreference = "Stop"
. (Join-Path $PsScriptRoot "settings.ps1")
if (Test-Path -Path "C:\demo\*\BcContainerHelper.psm1") {
$module = Get-Item -Path "C:\demo\*\BcContainerHelper.psm1"
Import-module $module.FullName -DisableNameChecking
} else {
Import-Module -name bccontainerhelper -DisableNameChecking
}
$storageContext = New-AzStorageContext -ConnectionString $storageConnectionString
$queuename = $publicDnsName.Split('.')[0]
# Create Queue (if it doesn't exist)
$storageQueue = Get-AzStorageQueue -Context $storageContext -Name $queuename -ErrorAction Ignore
if (!($storageQueue)) {
New-AzStorageQueue -Name $queuename -Context $storageContext -ErrorAction Ignore | Out-Null
}
$storageQueue = Get-AzStorageQueue -Name $queuename -Context $storageContext
# Create table for replies (if it doesn't exist)
$storageTable = Get-AzStorageTable –Name "QueueStatus" –Context $storageContext -ErrorAction Ignore
if (!($storageTable)) {
New-AzStorageTable –Name "QueueStatus" –Context $storageContext -ErrorAction Ignore | Out-Null
}
$storageTable = Get-AzStorageTable –Name "QueueStatus" –Context $storageContext
# Transcript container on azure storage
New-AzStorageContainer -Context $storageContext -Name "transcript" -Permission Blob -ErrorAction SilentlyContinue | Out-Null
# Folder where command scripts are placed
$commandFolder = (Join-Path $PsScriptRoot "request").ToLowerInvariant()
# Folder for temp storage of transcripts
$transcriptFolder = Join-Path $PsScriptRoot "transcript"
if (!(Test-Path -Path $transcriptFolder -PathType Container)) {
New-Item -Path $transcriptFolder -ItemType Directory | Out-Null
}
while ($true) {
# Get message
$timeUntilMessageReappearsInQueue = [TimeSpan]::FromMinutes(10)
$message = $storageQueue.CloudQueue.GetMessage($timeUntilMessageReappearsInQueue)
if (!($message)) {
break
}
$transcriptname = ([Guid]::NewGuid().ToString()+".txt")
$transcriptfilename = Join-Path $transcriptFolder $transcriptname
$transcripting = $false
$maxAttempts = 1
$cmd = ""
$ht = @{ "Queue" = $queuename
"Cmd" = ""
"Id" = ""
"Logstr" = ""
"Dequeue" = $message.DequeueCount
"Status" = "Begin"
"transcript" = ""
}
Start-Transcript -Path $transcriptfilename
$transcripting = $true
Write-Host '---------------------------'
Write-Host $message.AsString
Write-Host '---------------------------'
try {
$json = $message.AsString | ConvertFrom-Json
$cmd = $json.cmd
$id = ""
$LogStr = "$cmd"
$Parameters = @{}
$json | Get-Member -MemberType NoteProperty | ForEach-Object {
$key = $_.Name
$value = $json."$key"
if ($key -eq "id") {
$id = $value
}
elseif ($key -eq "maxAttempts") {
$maxAttempts = $value
}
elseif ($key -ne "cmd") {
$LogStr += " -$key '$value'"
$Parameters += @{ $key = $value }
}
}
$ht.Cmd = $cmd
$ht.id = $id
$ht.Logstr = $LogStr
if ($message.DequeueCount -gt $maxAttempts) {
throw "No more attempts"
}
Add-AzTableRow -table $storageTable.CloudTable -partitionKey $queuename -rowKey ([string]::Format("{0:D19}", [DateTime]::MaxValue.Ticks - [DateTime]::UtcNow.Ticks)) -property $ht | Out-Null
$script = Get-Item -Path (Join-Path $commandFolder "$cmd.ps1") -ErrorAction Ignore
if (($script) -and ($script.FullName.ToLowerInvariant().StartsWith($commandFolder))) {
. $script @Parameters
$ht.Status = "Success"
} else {
$ht.Status = "Error"
Write-Host "Illegal request"
}
$storageQueue.CloudQueue.DeleteMessage($message)
} catch {
$ht.Status = "Exception"
Write-Host "Exception performing RunQueue (Cmd=$cmd)"
Write-Host $_.Exception.Message
Write-Host $_.ScriptStackTrace
if ($message.DequeueCount -ge $maxAttempts) {
$storageQueue.CloudQueue.DeleteMessage($message)
}
} finally {
}
if ($transcripting) {
$transcripting = $false
Stop-Transcript
Set-AzStorageBlobContent -File $transcriptfilename -Context $storageContext -Container "transcript" -Blob $transcriptname -Force | Out-Null
$ht.transcript = "$($StorageContext.BlobEndPoint)transcript/$transcriptname"
}
Add-AzTableRow -table $storageTable.CloudTable -partitionKey $queuename -rowKey ([string]::Format("{0:D19}", [DateTime]::MaxValue.Ticks - [DateTime]::UtcNow.Ticks)) -property $ht | Out-Null
}