Last active
August 29, 2015 14:02
-
-
Save peaeater/2d985fa25e575fffd2c6 to your computer and use it in GitHub Desktop.
Triggers Solr DIH update and monitors its status. Writes exceptions or final success message to Windows Application Event Log.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<# | |
Trigger Solr update and poll for status. | |
- Writes events to Application Event Log; log source must already have been added | |
Peter Tyrrell | |
#> | |
param( | |
[Parameter(Mandatory=$false,Position=0)] | |
[string]$logsrc = "Andi Solr Update" | |
) | |
$triggerUrl = "http://localhost:8983/solr/core1/dataimport?command=full-import&clean=true&optimize=true&wt=xml" | |
$statusUrl = "http://localhost:8983/solr/core1/dataimport?command=status&wt=xml&indent=on" | |
[int]$interval = 5 | |
function logError([string]$msg) { | |
# write error msg to Application EventLog | |
Write-EventLog -LogName Application -Source $logsrc -EventId 500 -EntryType Error -Message $msg -Category 0 | |
Write-Host ("ERROR {0}" -f $msg) | |
} | |
function logInfo([string]$msg) { | |
# write info msg to Application EventLog | |
Write-EventLog -LogName Application -Source $logsrc -EventId 200 -EntryType Information -Message $msg -Category 0 | |
Write-Host ("INFO {0}" -f $msg) | |
} | |
function logWarning([string]$msg) { | |
Write-EventLog -LogName Application -Source $logsrc -EventId 400 -EntryType Warning -Message $msg -Category 0 | |
Write-Host ("WARNING {0}" -f $msg) | |
} | |
<# | |
Trigger Solr ingest and poll status. | |
#> | |
try { | |
$triggerReply = (new-object System.Net.WebClient).DownloadString($triggerUrl) | |
Write-Host ("Triggered Solr [{0}]:" -f $triggerUrl) | |
# Force at least one ping after interval. Immediate reply to trigger may relate to a former job status. | |
$status = "busy" | |
$statusReply | |
$xml | |
while ($status -eq "busy") { | |
# wait | |
Start-Sleep -Seconds $interval | |
# poll status | |
$statusReply = (new-object System.Net.WebClient).DownloadString($statusUrl) | |
# convert to XmlDocument and read status message: (busy/idle) | |
$xml = [xml]$statusReply | |
$status = ($xml.response.str | where { $_.name -eq "status"}).InnerText | |
# provide some feedback | |
if ($status -eq "busy") { | |
$docsFetched = $xml.SelectSingleNode("response/lst[@name = ""statusMessages""]/str[@name = ""Total Rows Fetched""]").InnerText | |
$docsProcessed = $xml.SelectSingleNode("response/lst[@name = ""statusMessages""]/str[@name = ""Total Documents Processed""]").InnerText | |
$docsSkipped = $xml.SelectSingleNode("response/lst[@name = ""statusMessages""]/str[@name = ""Total Documents Skipped""]").InnerText | |
Write-Host ("Reply from Solr: fetched={0} processed={1} skipped={2}" -f $docsFetched, $docsProcessed, $docsSkipped) | |
} | |
} | |
# Solr is done. Success or failure? | |
if ($status -eq "idle") { | |
$statusMsg = $xml.SelectSingleNode("response/lst[@name = ""statusMessages""]/str[@name = """"]").InnerText | |
if ($statusMsg.StartsWith("Indexing failed")) { | |
logError(("{0}`r`n`r`n{1}" -f $statusMsg, $statusReply)) | |
} | |
elseif ($statusMsg.StartsWith("Indexing completed")) { | |
[int]$docsFailed = $xml.SelectSingleNode("response/lst[@name = ""statusMessages""]/str[@name = ""Total Documents Failed""]").InnerText | |
if ($docsFailed -eq 0) { | |
logInfo(("{0}`r`n`r`n{1}" -f $statusMsg, $statusReply)) | |
} | |
else { | |
logWarning(("{0}`r`n`r`n{1}" -f $statusMsg, $statusReply)) | |
} | |
} | |
else { | |
logWarning(("Unable to read Solr status message. Indexing success is unknown. Review the included reply for more info.`r`n`r`n{0}" -f $statusReply)) | |
} | |
} | |
else { | |
logWarning(("Unable to read Solr status. Indexing may or may not have finished. Review the included reply for more info.`r`n`r`n{0}" -f $statusReply)) | |
} | |
exit 0 | |
} | |
catch [Exception] { | |
logError($_.Exception) | |
exit 1 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment