|
# TODO:
# - Add switch "reenqueueFailures" to do that (mutually exclusive with "destoryFailures")
# - Add switch "reenqueueRunning" to do that
# - Display usage informaiton when run with no parameters
# This script will reenqueue all failed and all currently running tasks in a Kiln Queue
#
# Name: Reenqueue all Tasks
# Author: Quentin Schroeder
# Version: 1.0.0
# Description: This script is similar to our old "churn the queue" script that we had for versions of Kiln prior to
# 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.
# Parameters: destroyFailures - A switch to destroy all failures instead of reenqueuing them.
param( [switch]$destroyFailures );
function main() {
# This will almost always have to be run directly from the server
$qs_host = "localhost";
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 ($?) {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;
write-host "Reenqueueing $failCount failures...";
$i = 0
if ($failCount -gt 0) {
foreach ($failure in $failures.item) {
# Try both formats of the JSON (changed for 2.9.58)
if ($failure."#text")
{
$failure = $failure."#text";
}
elseif ($failure.id."#text")
{
$failure = $failure.id."#text";
}
else
{
$i += 1;
write-host "`n$i/$failCount (NOT FOUND)...";
continue
}
$i += 1;
write-host "`n$i/$failCount ($failure)...";
Reenqueue-Task $qs_host $failure $destroyFailures;
}
}
write-host "`nReenqueueing $runningCount running tasks...";
$i = 0
if ($runningCount -gt 0) {
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;
}
}
}
function Reenqueue-Task ($qs_host, $task, $destroy = $false) {
$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);
}
Execute-HTTPPostCommand $URI "a=1";
if (!($?)) {
write-host "$task failed!";
}
}
# This is provided as an alternative to ConvertFrom-JSON, which requires Powershell V3
# which is not on most servers by default and requires a reboot to install.
# Source: (https://www.cogmotive.com/blog/powershell/parsing-json-in-powershell-xml-the-member-item-is-already-present)
Add-Type -Assembly System.ServiceModel.Web,System.Runtime.Serialization
function Convert-JsonToXml([string]$json)
{
$bytes = [byte[]][char[]]$json
$quotas = [System.Xml.XmlDictionaryReaderQuotas]::Max
$jsonReader = [System.Runtime.Serialization.Json.JsonReaderWriterFactory]::CreateJsonReader($bytes,$quotas)
try
{
$xml = new-object System.Xml.XmlDocument
$xml.Load($jsonReader)
$xml
}
finally
{
$jsonReader.Close()
}
}
# Source: (http://stackoverflow.com/questions/5246836/user-powershell-script-to-post-to-url)
function Execute-HTTPPostCommand() {
param(
[string] $target = $null,
[string] $post = $null
)
$webRequest = [System.Net.WebRequest]::Create($target)
$webRequest.ContentType = "text/html"
$PostStr = [System.Text.Encoding]::UTF8.GetBytes($post)
$webrequest.ContentLength = $PostStr.Length
$webRequest.ServicePoint.Expect100Continue = $false
$webRequest.Method = "POST"
$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($PostStr, 0,$PostStr.length)
$requestStream.Close()
try
{
[System.Net.WebResponse] $resp = $webRequest.GetResponse();
}
catch [Net.WebException]
{
write-host $_.Exception.ToString()
}
$rs = $resp.GetResponseStream();
[System.IO.StreamReader] $sr = New-Object System.IO.StreamReader -argumentList $rs;
[string] $results = $sr.ReadToEnd();
return $results;
}
# Do the work of reenqueuing everything
main;
|
Loading...