private static bool InstallNewVersion(string directory, ServiceProgramVersion program) { if (program != null) { var installDir = program.SaveToDisk(); program.InstallToDisk(); return(true); } return(false); }
private static void Main(string[] args) { Assembly thisAssem = typeof(Program).Assembly; AssemblyName thisAssemName = thisAssem.GetName(); Version ver = thisAssemName.Version; Console.WriteLine(thisAssemName.Name); Console.WriteLine("ver: " + ver.ToString()); // start diagnostics for logevent and heatbeats.... Common.MessageQueue.Diagnostics.Initialize(GlobalValues.ServiceName(), ver.ToString()); var settings = ZooKeeperDefinition.LoadFromDisk(); try { while (true) { var server = new Common.MessageQueue.RequestServer( new System.Collections.Generic.List <MessageQueueConfiguration>() { ZookeeperConfig, Monosoft.Common.TokenHandler.TokenEventHandler.InvalidateConfig(GlobalValues.ServiceName()) }, 10); foreach (var s in settings.Services) { string directory = Path.Combine(Environment.CurrentDirectory, $"services/{s.ServiceInfo.Id}"); ProgamVersion version = new ProgamVersion("0.0.0.0"); var versions = ServiceProgramVersion.FindLocalVersions(s.ServiceInfo.Id); if (versions.Any()) { version = versions.FirstOrDefault(); } ServiceProgramVersion program = DownloadNewVersion(s, version); bool restartNeeded = InstallNewVersion(directory, program); if (program != null) {// New version installed! use the new version version = program.Version; } string executeableDir = string.Format($"./services/{s.ServiceInfo.Id}/{version.Version}/"); string executeableName = string.Empty; if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { executeableName = string.Format($"{s.ServiceInfo.Name}"); } else { executeableName = string.Format($"{s.ServiceInfo.Name}.exe"); } if (restartNeeded && RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { // give access to the executable on linux... Console.WriteLine("Execute: sudo chmod 777 on " + executeableDir + executeableName); System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo("sudo", $"chmod 777 {executeableDir+executeableName}") { UseShellExecute = false }); } // start if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { var runningProcess = manager.Where(p => p.Service.Id == s.ServiceInfo.Id).FirstOrDefault(); if (runningProcess == null) { Console.WriteLine($"{s.ServiceInfo.Name} isnt running, starting"); Console.WriteLine("Execute: " + executeableDir + executeableName); var process = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(executeableDir + executeableName) { WorkingDirectory = executeableDir, UseShellExecute = false }); manager.Add(new ProcessManager() { Process = process, Service = s.ServiceInfo }); } else if (runningProcess.Process.HasExited == true) { Console.WriteLine($"{s.ServiceInfo.Name} has exited, restarting"); // TODO: report exitcode!!! Console.WriteLine("Execute: " + executeableDir + executeableName); // RESTART runningProcess.Process = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(executeableDir + executeableName) { WorkingDirectory = executeableDir, UseShellExecute = false }); } else if (restartNeeded) { Console.WriteLine("RESTART NEEDED"); Console.WriteLine("Execute: " + executeableDir + executeableName); runningProcess.Process.Kill(); // TODO: AUCH: close er nok bedre - men så skal vi sikre at alle services faktisk tillader dette... runningProcess.Process = System.Diagnostics.Process.Start(new System.Diagnostics.ProcessStartInfo(executeableDir + executeableName) { WorkingDirectory = executeableDir, UseShellExecute = false }); } } } int seconds = 1000; int minutes = 60 * seconds; int hours = 60 * minutes; Console.WriteLine("Sleep for 1 hour - before recheck"); System.Threading.Thread.Sleep(1 * hours); } } finally { foreach (var s in settings.Services) { var runningProcess = manager.Where(p => p.Service.Id == s.ServiceInfo.Id).FirstOrDefault(); Console.WriteLine("Killing: " + s.ServiceInfo.Name); runningProcess.Process.Kill(); } } }
/// <summary> /// Handle an incomming message /// </summary> /// <param name="topicparts">The topic/route as a list of strings</param> /// <param name="wrapper">Message wrapper</param> /// <returns>NULL</returns> public static ReturnMessageWrapper HandleMessage(string[] topicparts, Common.DTO.MessageWrapper wrapper) { CallContext cc = new CallContext( wrapper.OrgContext, new Common.DTO.Token() { Tokenid = wrapper.UserContextToken, Scope = GlobalValues.Scope }, wrapper.IssuedDate); var operation = topicparts[1]; switch (operation) { case "download": // BEMÆRK RPC !!! ServiceDownloadDefinition downloadDef = Common.DTO.MessageWrapperHelper <ServiceDownloadDefinition> .GetData(wrapper); var foundupdate = downloadDef.FindUpdate(); Common.MessageQueue.EventClient.Instance.RaiseEvent("servicestore.newversion." + downloadDef.ServiceId, new Common.DTO.EventDTO(foundupdate, wrapper.Clientid, wrapper.Messageid)); return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>() { new LocalizedString() { Lang = "en", Text = "OK" } }, foundupdate)); // break; case "upload": if (cc.IsServiceStoreAdmin) { ServiceProgramVersion program = Common.DTO.MessageWrapperHelper <ServiceProgramVersion> .GetData(wrapper); program.SaveToDisk(); Common.MessageQueue.EventClient.Instance.RaiseEvent("servicestore.newserviceupdate", new Common.DTO.EventDTO(program.Version, string.Empty, string.Empty)); return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>() { new LocalizedString() { Lang = "en", Text = "OK" } }, program.Version)); } break; case "create": if (cc.IsServiceStoreAdmin) { Monosoft.ServiceStore.DTO.Service createservice = Common.DTO.MessageWrapperHelper <Monosoft.ServiceStore.DTO.Service> .GetData(wrapper); createservice.SaveToDisk(false); Common.MessageQueue.EventClient.Instance.RaiseEvent("servicestore.newservice", new Common.DTO.EventDTO(createservice, string.Empty, string.Empty)); return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>() { new LocalizedString() { Lang = "en", Text = "OK" } }, createservice)); } break; case "update": if (cc.IsServiceStoreAdmin) { Monosoft.ServiceStore.DTO.Service updateservice = Common.DTO.MessageWrapperHelper <Monosoft.ServiceStore.DTO.Service> .GetData(wrapper); updateservice.SaveToDisk(true); Common.MessageQueue.EventClient.Instance.RaiseEvent("servicestore.serviceupdate", new Common.DTO.EventDTO(updateservice, string.Empty, string.Empty)); return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>() { new LocalizedString() { Lang = "en", Text = "OK" } }, updateservice)); } break; case "delete": if (cc.IsServiceStoreAdmin) { Monosoft.ServiceStore.DTO.Service deleteservice = Common.DTO.MessageWrapperHelper <Monosoft.ServiceStore.DTO.Service> .GetData(wrapper); deleteservice.Delete(); Common.MessageQueue.EventClient.Instance.RaiseEvent("servicestore.servicedelete", new Common.DTO.EventDTO(deleteservice, string.Empty, string.Empty)); return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>() { new LocalizedString() { Lang = "en", Text = "OK" } }, deleteservice)); } break; case "get": if (cc.IsServiceStoreAdmin) { var res = ServicesInformation.ServiceInformation(); Common.MessageQueue.EventClient.Instance.RaiseEvent("servicestore.servicelist", new Common.DTO.EventDTO(res, wrapper.Clientid, wrapper.Messageid)); return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>() { new LocalizedString() { Lang = "en", Text = "OK" } }, res)); } break; default: /*log error event*/ Common.MessageQueue.Diagnostics.Instance.LogEvent( "Unknow topic for ServiceStore.", operation + " is unknown", Common.DTO.Severity.Information, wrapper.OrgContext); break; } return(ReturnMessageWrapper.CreateResult(true, wrapper, new System.Collections.Generic.List <LocalizedString>() { new LocalizedString() { Lang = "en", Text = "missing credentials" } }, null)); }