Kiln » KilnSupportScripts Powershell Scripts to help monitor a Kiln environment. Contact Fog Creek support before using.
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

updated the running tasks to automatically deal with json format variations

Changeset e8792002a292

Parent 1d21f02417e3

by Profile picture of User 1563Quentin Schroeder <quentin@fogcreek.com>

Changes to one file · Browse files at e8792002a292 Showing diff from parent 1d21f02417e3 Diff from another changeset...

 
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
 
136
137
138
139
 
140
141
142
 
145
146
147
148
149
150
 
 
 
151
152
 
153
154
155
 
157
158
159
160
161
 
 
 
 
 
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
 
167
168
169
 
170
171
172
173
 
176
177
178
 
 
 
179
180
181
182
 
183
184
185
186
 
188
189
190
 
 
 
191
192
 
@@ -13,76 +13,107 @@
 # 2.9.x. It simply hits the URLs that are provided via the Queue Stats page to reenqueue all failed tasks  # as well as all currently running tasks (since they can get stuck running sometimes). It should be used  # when the page at http://localhost:56785/ indicates that the queue is choking. -# Requirements: This script must be run on the same server that is hosting the Kiln Queue. +# Requirements: PowerShell v2.0  # Parameters: destroyFailures - A switch to destroy all failures instead of reenqueuing them.   -param( [switch]$destroyFailures ); + +param( [string]$qs_host = "localhost", # This will almost always have to be run directly from the server + [switch]$destroyFailures, # Add this switch parameter to destroy all failed tasks + [switch]$retryFailures, # Add this switch parameter to reenqueue all failed tasks + [switch]$destroyRunning, # Add this switch parameter to destroyed all currently running tasks + [switch]$retryRunning # Add this switch parameter to reenqueue all currently running tasks + )      function main() { - # This will almost always have to be run directly from the server - $qs_host = "localhost"; + if ($destroyFailures -and $retryFailures) + { + write-host "Cannot choose to both destroy AND retry failed tasks, please select only one option." + }   - write-host ("Grabbing queue stats from http://{0}:56785/stats.json" -f $qs_host); - $response = (New-Object System.Net.WebClient).DownloadString("http://{0}:56785/stats.json" -f $qs_host); + if ($destroyRunning -and $retryRunning) + { + write-host "Cannot choose to both destroy AND retry running tasks, please select only one option." + } + + + write-host ("Reading queue stats from http://{0}:56785/stats.json" -f $qs_host) + $response = (New-Object System.Net.WebClient).DownloadString("http://{0}:56785/stats.json" -f $qs_host)   if ($?) {write-host " Stats retrieved successfully.`n"}   - # $stats = $response | ConvertFrom-JSON; # This needs Powershell 3.0, so we parse it to XML instead   $stats = Convert-JsonToXml($response) - $failures = $stats.root.failures; - $runningTasks = $stats.root.runningTasks; - $runningCount = $runningTasks.childnodes.count; - $failCount = $failures.childnodes.count; + $failures = $stats.root.failures + $runningTasks = $stats.root.runningTasks + $runningCount = $runningTasks.childnodes.count + $failCount = $failures.childnodes.count   - write-host "Reenqueueing $failCount failures..."; + write-host "Found $failCount failures..."   $i = 0 - if ($failCount -gt 0) { + if (($failCount -gt 0) -and ($destroyFailures -or $retryFailures)) {   foreach ($failure in $failures.item) {   # Try both formats of the JSON (changed for 2.9.58)   if ($failure."#text")   { - $failure = $failure."#text"; + $failure = $failure."#text"   }   elseif ($failure.id."#text")   { - $failure = $failure.id."#text"; + $failure = $failure.id."#text"   }   else   { - $i += 1; - write-host "`n$i/$failCount (NOT FOUND)..."; + $i += 1 + write-host "`n$i/$failCount (NOT FOUND)..."   continue   } - $i += 1; - write-host "`n$i/$failCount ($failure)..."; - Reenqueue-Task $qs_host $failure $destroyFailures; + $i += 1 + write-host "`n$i/$failCount ($failure)..." + Process-Task $qs_host $failure $destroyFailures   }   }   - write-host "`nReenqueueing $runningCount running tasks..."; + + write-host "`n-------`n" + + + write-host "Found $runningCount running tasks..."   $i = 0 - if ($runningCount -gt 0) { + if (($runningCount -gt 0) -and ($destroyRunning -or $retryRunning)) {   foreach ($runningTask in $runningTasks.item) { - if ($newer) {$runningTask = $runningTask.hash."#text"; } - else { $runningTask = $runningTask.id."#text"; } - $i += 1; - write-host "`n$i/$runningCount ($runningTask)..."; - Reenqueue-Task $qs_host $runningTask; + # Try both formats of the JSON (changed for 2.9.58) + if ($runningTask.id."#text") + { + $runningTask = $runningTask.id."#text" + } + elseif ($runningTask.hash."#text") + { + $runningTask = $runningTask.hash."#text" + } + else + { + $i += 1 + write-host "`n$i/$runningCount (NOT FOUND)..." + continue + } + $i += 1 + write-host "`n$i/$runningCount ($runningTask)..." + Process-Task $qs_host $runningTask $destroyRunning   }   }  }     -function Reenqueue-Task ($qs_host, $task, $destroy = $false) { - - $URI = "http://{0}:56785/reenqueue?id={1}" -f ($qs_host, $task); +function Process-Task ($qs_host, $task, $destroy = $false) +{ + if (!($destroy)) { write-host "Retrying task!" } + $URI = "http://{0}:56785/reenqueue?id={1}" -f ($qs_host, $task)   if ($destroy) { - write-host "DESTROYING TASK $task!"; - $URI = "http://{0}:56785/destroy?id={1}" -f ($qs_host, $task); + write-host "Destroying task!" + $URI = "http://{0}:56785/destroy?id={1}" -f ($qs_host, $task)   } - Execute-HTTPPostCommand $URI "a=1"; + Execute-HTTPPostCommand $URI "a=1"   if (!($?)) { - write-host "$task failed!"; + write-host "Error!"   }  }   @@ -136,7 +167,7 @@
    try   { - [System.Net.WebResponse] $resp = $webRequest.GetResponse(); + [System.Net.WebResponse] $resp = $webRequest.GetResponse()   }   catch [Net.WebException]   { @@ -145,11 +176,11 @@
      - $rs = $resp.GetResponseStream(); - [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs; - [string] $results = $sr.ReadToEnd(); + $rs = $resp.GetResponseStream() + [System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs + [string] $results = $sr.ReadToEnd()   - return $results; + return $results    }   @@ -157,5 +188,5 @@
      -# Do the work of reenqueuing everything -main; \ No newline at end of file
+# Do everything +main \ No newline at end of file