mirror of
https://bitbucket.org/anguist/ntpa
synced 2026-10-05 03:08:18 +00:00
Simplified setup & improved error handling
This commit is contained in:
@@ -1,10 +1,10 @@
|
||||
//
|
||||
// Runner.cs
|
||||
// Initializer.cs
|
||||
//
|
||||
// Author:
|
||||
// Carsten Sonne Larsen <cs@innolan.dk>
|
||||
//
|
||||
// Copyright (c) 2013 Carsten Sonne Larsen
|
||||
// Copyright (c) 2013-2016 Carsten Sonne Larsen
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
// of this software and associated documentation files (the "Software"), to deal
|
||||
@@ -40,6 +40,10 @@ using Ntp.Analyzer.Process.Description;
|
||||
using Ntp.Analyzer.Config.Root;
|
||||
using Ntp.Monitor.Client;
|
||||
using System.Net;
|
||||
using Ntp.System;
|
||||
using Ntp.Analyzer.Objects;
|
||||
using System.Linq;
|
||||
using Ntp.Analyzer.Objects.Live;
|
||||
|
||||
namespace Ntp.Analyzer.Process
|
||||
{
|
||||
@@ -104,6 +108,8 @@ namespace Ntp.Analyzer.Process
|
||||
|
||||
if (proceed) {
|
||||
InitializeState ();
|
||||
InitializeDatabase ();
|
||||
InitializeData ();
|
||||
InitializeListerners ();
|
||||
InitializeCluster ();
|
||||
InitializeScheduler ();
|
||||
@@ -169,9 +175,6 @@ namespace Ntp.Analyzer.Process
|
||||
/// </summary>
|
||||
private void InitializeState ()
|
||||
{
|
||||
// Initialize database connection
|
||||
DataConnection.String = config.Database.ConnectionString;
|
||||
|
||||
// Initialize application state
|
||||
ApplicationState.Version = version;
|
||||
ApplicationState.Pid = pid;
|
||||
@@ -179,8 +182,77 @@ namespace Ntp.Analyzer.Process
|
||||
ApplicationState.Config = config;
|
||||
ApplicationState.ConfigFile = configFile;
|
||||
ApplicationState.Log = log;
|
||||
|
||||
// Initialize system settings
|
||||
ShellCommand.WorkingDirectory = "/tmp";
|
||||
}
|
||||
|
||||
private void InitializeDatabase()
|
||||
{
|
||||
// Initialize database connection
|
||||
DataConnection.DatabaseName = config.Database.Name;
|
||||
DataConnection.String = config.Database.ConnectionString;
|
||||
DataConnection.InitializeString = config.Database.InitializeConnectionString;
|
||||
DataConnection.Initialize = config.Database.Initialize;
|
||||
DataConnection.Log = log;
|
||||
|
||||
// Initialize database tables
|
||||
if (config.Database.Initialize) {
|
||||
try {
|
||||
DataInitializer initializer = new DataInitializer (log);
|
||||
initializer.CreateDatabase ();
|
||||
} catch (Exception e) {
|
||||
log.WriteLine ("Error during initialization of database.", Severity.Error);
|
||||
log.WriteLine (e.Message, Severity.Debug);
|
||||
log.WriteLine (e, Severity.Trace);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void InitializeData()
|
||||
{
|
||||
if (!config.Database.Initialize)
|
||||
return;
|
||||
|
||||
try {
|
||||
// Initialize hosts
|
||||
foreach (ServerConfiguration server in config.Servers) {
|
||||
Host host = DataFace.Instance.Hosts.SingleOrDefault (h => h.Id == server.HostId);
|
||||
if (host == null) {
|
||||
IPAddress ip;
|
||||
try {
|
||||
ip = Dns.GetHostAddresses (server.ServerName) [0];
|
||||
} catch {
|
||||
ip = IPAddress.None;
|
||||
}
|
||||
host = new Host (server.HostId, server.ServerName, ip.ToString (), null);
|
||||
DataFace.Instance.Hosts.Save (host);
|
||||
Log.WriteLine (String.Format (
|
||||
"Created a new host in database ID {0} with name {1} and IP {2}.",
|
||||
server.HostId, server.ServerName, ip),
|
||||
Severity.Info);
|
||||
}
|
||||
|
||||
// Initialize peers
|
||||
foreach (AssociationEntry entry in DataFace.Instance.NtpqCache[server.ServerName]) {
|
||||
IEnumerable<Peer> peerList = DataFace.Instance.Peers.Where (p => p.Ip == entry.Remote);
|
||||
if (peerList.Count () == 0) {
|
||||
Peer peer = new Peer (entry.Remote, entry.Remote, null);
|
||||
DataFace.Instance.Peers.Save (peer);
|
||||
Log.WriteLine (String.Format (
|
||||
"Created a new peer in database with name {0} and IP {1}.",
|
||||
entry.Remote, entry.Remote),
|
||||
Severity.Info);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
log.WriteLine ("Could not populating all data into tables.", Severity.Error);
|
||||
log.WriteLine (e.Message, Severity.Debug);
|
||||
log.WriteLine (e, Severity.Trace);
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Initializes the listerners.
|
||||
/// </summary>
|
||||
|
||||
Reference in New Issue
Block a user