public void Synch() { if (!this.SynchEnabled) { return; } this.SynchLog = new Classes.LinkSynchLog(); this.IsSynching = true; try { this.ServersSynchStates = new Dictionary <string, bool>(); // Get server state of self. bool serverState = GetServerSynchState(ConfigurationManager.AppSettings["ServerIP"]); if (!serverState) { return; } //if (this.FileStates == null) this.GetFileSynchStates(); InstanceCollection instances = new InstanceCollection( ConfigurationManager.AppSettings["InstanceRoot"] ); SynchDatabases(); SynchFiles(instances); CheckDeletedFiles(); } catch (Exception ex) { Global.Log(ex.ToString(), LogType.Error); try { // configuration values from the web.config file. MailConfiguration mailConfiguration = new MailConfiguration(true); // Create a new mail by the mail configuration. Mail mail = new Mail(mailConfiguration, "_NONE_") { TemplatePath = Path.Combine( ConfigurationManager.AppSettings["ApplicationPath"], "App_Data", "ErrorMailTemplate.html" ), Subject = "SYNCH ERROR" }; mail.Placeholders.Add("Server", ConfigurationManager.AppSettings["ServerIP"]); mail.Placeholders.Add("Exception", ex.ToString()); // Send the mail. mail.Send(ConfigurationManager.AppSettings["ErrorMailReciepent"]); } catch (Exception e) { Global.Log(e.ToString(), LogType.Error); } } StringBuilder log = new StringBuilder(); if (this.SynchLog.Files != 0) { log.Append(string.Format( "{0} files synched successfully. ", this.SynchLog.Files )); } if (this.SynchLog.FilesFailed != 0) { log.Append(string.Format( "{0} files failed to synch. ", this.SynchLog.FilesFailed )); } if (this.SynchLog.Queries != 0) { log.Append(string.Format( "{0} database queries synched successfully. ", this.SynchLog.Queries )); } if (this.SynchLog.QueriesFailed != 0) { log.Append(string.Format( "{0} database queries failed to synch. ", this.SynchLog.QueriesFailed )); } if (log.Length != 0) { Global.Log(log.ToString(), LogType.Information); } log.Clear(); this.IsSynching = false; }
private void SynchFiles(InstanceCollection instances) { string instanceRoot = Path.GetDirectoryName( ConfigurationManager.AppSettings["InstanceRoot"] ); XmlDocument document = new XmlDocument(); document.Load(Path.Combine( HttpRuntime.AppDomainAppPath, "App_Data", "Filesystem.xml" )); XmlNodeList xmlNodesDirectories = document.DocumentElement.SelectNodes("Directory"); XmlNodeList xmlNodesFiles = document.DocumentElement.SelectNodes("File"); // Run through all listed file to check for deleted files. foreach (string relativePath in this.Files.Keys) { string fileName = Path.GetDirectoryName( ConfigurationManager.AppSettings["InstanceRoot"] ) + relativePath; // Check if the file still exists. if (File.Exists(fileName)) { continue; } DeleteFile(relativePath, this.Files[relativePath]); } foreach (Instance instance in instances.Instances.Values) { foreach (Client client in instance.GetClients()) { if (client.SynchServers.Length == 0) { continue; } foreach (string server in client.SynchServers) { // Get server state of target server. bool serverState = GetServerSynchState(server); if (!serverState) { continue; } foreach (XmlNode xmlNode in xmlNodesDirectories) { string directory = Path.Combine( instanceRoot, instance.Name, string.Format(xmlNode.Attributes["Path"].Value, client.Name) ); if (!Directory.Exists(directory)) { continue; } SynchDirectory(server, client.Name, directory); } foreach (XmlNode xmlNode in xmlNodesFiles) { SynchFile(server, client.Name, Path.Combine(instanceRoot, instance.Name, string.Format( xmlNode.Attributes["Path"].Value, client.Name ))); } } } } }
private void GetFileSynchStates() { this.FileStates = new Dictionary <string, string>(); this.Files = new Dictionary <string, string>(); InstanceCollection instances = new InstanceCollection( ConfigurationManager.AppSettings["InstanceRoot"] ); List <string> servers = new List <string>(); foreach (Instance instance in instances.Instances.Values) { foreach (Client client in instance.GetClients()) { if (client.SynchServers.Length == 0) { continue; } foreach (string server in client.SynchServers) { // TEST: //string server = "127.0.0.1"; if (servers.Contains(server)) { continue; } servers.Add(server); } } } foreach (string server in servers) { try { ServiceLink service = new ServiceLink(string.Format( "http://{0}:8080/Handler.ashx", server )); string result = service.Request( service.Address + "?Method=GetFileSynchStates", new byte[0] ); XmlDocument xmlDocument = new XmlDocument(); xmlDocument.LoadXml(result); foreach (XmlNode xmlNode in xmlDocument.DocumentElement.SelectNodes("File")) { string path = xmlNode.Attributes["Path"].Value; if (this.FileStates.ContainsKey(path)) { continue; } this.FileStates.Add(path, xmlNode.Attributes["Value"].Value); this.Files.Add(path, server); } } catch (Exception ex) { } } }
private void CheckDeletedFiles() { return; string fileName; foreach (string file in this.FileStates.Keys) { fileName = Path.Combine( ConfigurationManager.AppSettings["InstanceRoot"], file.Remove(0, 1) ); InstanceCollection instances = new InstanceCollection( ConfigurationManager.AppSettings["InstanceRoot"] ); List <string> servers = new List <string>(); foreach (Instance instance in instances.Instances.Values) { foreach (Client client in instance.GetClients()) { if (client.SynchServers.Length == 0) { continue; } foreach (string server in client.SynchServers) { // TEST: //string server = "127.0.0.1"; if (servers.Contains(server)) { continue; } servers.Add(server); } } } if (!File.Exists(fileName)) { if (long.Parse(DateTime.UtcNow.ToString("yyyyMMddHHmmssfff")) - long.Parse(this.FileStates[file]) > 10000) { foreach (string server in servers) { //File.Delete(fileName); ServiceLink service = new ServiceLink(string.Format( "http://{0}:8080/Handler.ashx", server )); string result = service.Request( service.Address + "?Method=DeleteFile&Path=" + file.Remove(0, 1), new byte[0] ); } } } } }