1
0
mirror of https://bitbucket.org/anguist/ntpa synced 2025-10-06 11:04:40 +00:00

Improved error handling

This commit is contained in:
2016-03-25 00:39:15 +01:00
parent 86cf01ad8c
commit fd76147eed
8 changed files with 60 additions and 27 deletions

View File

@ -72,6 +72,7 @@ namespace Ntp.Analyzer.Log
if (severity < treshold) if (severity < treshold)
return; return;
string pad = String.Empty;
string severityText; string severityText;
switch (severity) { switch (severity) {
@ -80,12 +81,14 @@ namespace Ntp.Analyzer.Log
break; break;
case Severity.Warn: case Severity.Warn:
severityText = "WARN"; severityText = "WARN";
pad = " ";
break; break;
case Severity.Notice: case Severity.Notice:
severityText = "NOTICE"; severityText = "NOTICE";
break; break;
case Severity.Info: case Severity.Info:
severityText = "INFO"; severityText = "INFO";
pad = " ";
break; break;
case Severity.Debug: case Severity.Debug:
severityText = "DEBUG"; severityText = "DEBUG";
@ -102,7 +105,7 @@ namespace Ntp.Analyzer.Log
string entry = String.Concat( string entry = String.Concat(
now.ToString(timeFormat), now.ToString(timeFormat),
" [", severityText, "] ", " [", severityText, "] ",
text); pad, text);
lock (locker) lock (locker)
{ {

View File

@ -60,17 +60,12 @@ namespace Ntp.Analyzer.Process.Description
protected override void InternalExecute() protected override void InternalExecute()
{ {
// Find host in database. Host host = DataFace.Instance.Hosts.SingleOrDefault (h => h.Id == config.HostId);
Host host;
try if (host == null) {
{ Log.WriteLine (String.Format (
host = DataFace.Instance.Hosts.Single(h => h.Name == config.ServerName); "Host with ID {0} was not found in database. ", config.HostId),
} Severity.Warn);
catch (Exception e)
{
Log.WriteLine("Hostname not found in database: " + config.ServerName, Severity.Warn);
Log.WriteLine (e, Severity.Trace);
return; return;
} }

View File

@ -24,6 +24,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
using System;
using System.Linq; using System.Linq;
using Ntp.Analyzer.Config.Destination; using Ntp.Analyzer.Config.Destination;
using Ntp.Analyzer.Config.Graph; using Ntp.Analyzer.Config.Graph;
@ -61,7 +62,14 @@ namespace Ntp.Analyzer.Process.Description
protected override void InternalExecute() protected override void InternalExecute()
{ {
Host host = DataFace.Instance.Hosts.Single(h => h.Id == config.HostId); Host host = DataFace.Instance.Hosts.SingleOrDefault (h => h.Id == config.HostId);
if (host == null) {
Log.WriteLine (String.Format (
"Host with ID {0} was not found in database. ", config.HostId),
Severity.Warn);
return;
}
foreach (GraphSetConfiguration graphSet in config.GraphSets) foreach (GraphSetConfiguration graphSet in config.GraphSets)
{ {

View File

@ -24,6 +24,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Ntp.Analyzer.Config.Graph; using Ntp.Analyzer.Config.Graph;
@ -60,7 +61,14 @@ namespace Ntp.Analyzer.Process.Description
protected override void InternalExecute() protected override void InternalExecute()
{ {
Host host = DataFace.Instance.Hosts.Single(h => h.Id == config.HostId); Host host = DataFace.Instance.Hosts.SingleOrDefault (h => h.Id == config.HostId);
if (host == null) {
Log.WriteLine (String.Format (
"Host with ID {0} was not found in database. ", config.HostId),
Severity.Warn);
return;
}
foreach (NtpConfEntry entry in DataFace.Instance.NtpConfigCache[config.ConfigFile]) foreach (NtpConfEntry entry in DataFace.Instance.NtpConfigCache[config.ConfigFile])
{ {

View File

@ -24,6 +24,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using Ntp.Analyzer.Config.Destination; using Ntp.Analyzer.Config.Destination;
@ -62,7 +63,14 @@ namespace Ntp.Analyzer.Process.Description
protected override void InternalExecute() protected override void InternalExecute()
{ {
Host host = DataFace.Instance.Hosts.Single(h => h.Id == config.HostId); Host host = DataFace.Instance.Hosts.SingleOrDefault (h => h.Id == config.HostId);
if (host == null) {
Log.WriteLine (String.Format (
"Host with ID {0} was not found in database. ", config.HostId),
Severity.Warn);
return;
}
foreach (GraphSetConfiguration graphSet in config.GraphSets) foreach (GraphSetConfiguration graphSet in config.GraphSets)
{ {

View File

@ -24,6 +24,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE. // THE SOFTWARE.
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Globalization; using System.Globalization;
using System.Linq; using System.Linq;
@ -67,7 +68,14 @@ namespace Ntp.Analyzer.Process.Description
protected override void InternalExecute() protected override void InternalExecute()
{ {
Host host = DataFace.Instance.Hosts.Single(h => h.Id == page.HostId); Host host = DataFace.Instance.Hosts.SingleOrDefault (h => h.Id == page.HostId);
if (host == null) {
Log.WriteLine (String.Format (
"Host with ID {0} was not found in database. ", page.HostId),
Severity.Warn);
return;
}
foreach (NtpConfEntry entry in DataFace.Instance.NtpConfigCache[page.ConfigFile]) foreach (NtpConfEntry entry in DataFace.Instance.NtpConfigCache[page.ConfigFile])
{ {

View File

@ -60,16 +60,12 @@ namespace Ntp.Analyzer.Process.Description
protected override void InternalExecute() protected override void InternalExecute()
{ {
Host host; Host host = DataFace.Instance.Hosts.SingleOrDefault (h => h.Id == config.HostId);
try if (host == null) {
{ Log.WriteLine (String.Format (
host = DataFace.Instance.Hosts.Single(h => h.Name == config.ServerName); "Host with ID {0} was not found in database. ", config.HostId),
} Severity.Warn);
catch (Exception e)
{
Log.WriteLine("Hostname not found in database: " + config.ServerName, Severity.Warn);
Log.WriteLine (e, Severity.Trace);
return; return;
} }
@ -77,4 +73,4 @@ namespace Ntp.Analyzer.Process.Description
SaveStream(graph); SaveStream(graph);
} }
} }
} }

View File

@ -162,19 +162,26 @@ namespace Ntp.Process
Running = true; Running = true;
runCount++; runCount++;
bool error = false;
log.WriteLine (String.Format ("{0} -> Executing", this), Severity.Debug); log.WriteLine (String.Format ("{0} -> Executing", this), Severity.Debug);
try { try {
description.Execute (); description.Execute ();
} catch (Exception ex) { } catch (Exception ex) {
error = true;
log.WriteLine ("Error while executing " + ToString (), Severity.Error); log.WriteLine ("Error while executing " + ToString (), Severity.Error);
log.WriteLine (ex.Message, Severity.Debug); log.WriteLine (String.Format ("Error in {0} -> {1}", this, ex.Message), Severity.Debug);
log.WriteLine (ex, Severity.Trace);
} finally { } finally {
Running = false; Running = false;
} }
time += DateTime.Now.Subtract (started); time += DateTime.Now.Subtract (started);
log.WriteLine (String.Format ("{0} -> Done", this), Severity.Debug); if (error) {
log.WriteLine (String.Format ("{0} -> Failed", this), Severity.Debug);
} else {
log.WriteLine (String.Format ("{0} -> Done", this), Severity.Debug);
}
} }
/// <summary> /// <summary>