private void timer_Tick(object state) { try { DaemonEntities entityContext = new DaemonEntities(); List <LocalProcess> localProcessEntities = ( from lp in entityContext.LocalProcess select lp).ToList <LocalProcess>(); foreach (LocalProcess localProcessEntity in localProcessEntities) { LocalProcessState localProcessState = QueryUtil.First <LocalProcessState>( from lps in entityContext.LocalProcessState where lps.LocalProcess.LocalProcessId == localProcessEntity.LocalProcessId select lps); if (localProcessEntity.Enabled) { if (localProcessState == null) { Process process = Process.Start(Directory.GetParent(this.GetType().Assembly.Location).FullName + "\\DaemonProcess.exe", localProcessEntity.LocalProcessId.ToString() + " --db-log"); } } if (localProcessState != null) { if (DateTime.Now - localProcessState.Modified > new TimeSpan(0, 0, 60)) { entityContext.DeleteObject(localProcessState); entityContext.SaveChanges(); } } } } catch (Exception e) { Trace.TraceError("Error in process guard loop: " + e.ToString() + " : " + e.StackTrace); Thread.Sleep(1000); } }
public void Startup() { lock (this) { Thread.CurrentThread.Name = LocalProcessId.ToString() + "-main"; MxpOptions.ThreadNamePrefix = LocalProcessId.ToString(); LogUtil.Info("Daemon Process startup..."); if (service != null) { throw new Exception("DaemonProcess already started."); } // Preparing database entity context. entityContext = new DaemonEntities(); // Loading configuration from database. localProcessEntity = QueryUtil.First <LocalProcess>( from lp in entityContext.LocalProcess where lp.LocalProcessId == LocalProcessId select lp); participantEntity = QueryUtil.First <DaemonLogic.Participant>( from lp in entityContext.LocalProcess where lp.LocalProcessId == LocalProcessId select lp.Participant); remoteProcessEntities = (from rp in entityContext.RemoteProcess where rp.LocalProcess.LocalProcessId == LocalProcessId select rp).ToList <RemoteProcess>(); bubbleEntities = (from b in entityContext.Bubble where b.LocalProcess.LocalProcessId == LocalProcessId select b).ToList <Bubble>(); LogUtil.Info("Loaded local process configuration: " + localProcessEntity.Address + ":" + localProcessEntity.ServerPort + "/" + localProcessEntity.HubPort); // Creating service, bubbles and bubble links. service = new CloudService(ConfigurationManager.AppSettings["DaemonMemberWeb"], localProcessEntity.Address, localProcessEntity.HubPort, localProcessEntity.ServerPort, localProcessEntity.Name, DaemonProcessConstants.ProgramMajorVersion, DaemonProcessConstants.ProgramMinorVersion); foreach (Bubble bubble in bubbleEntities) { CloudBubble cloudBubble = new CloudBubble(bubble.BubbleId, bubble.Name, (float)bubble.Range, (float)bubble.PerceptionRange); cloudBubble.ParticipantConnectAuthorize += OnParticipantConnectAuthorize; cloudBubble.CloudParticipantDisconnected += OnCloudParticipantDisconnected; service.AddBubble(cloudBubble); foreach (RemoteProcess remoteProcess in remoteProcessEntities) { cloudBubble.AddAllowedRemoteHubAddress(remoteProcess.Address); } // Loading bubble link configuration from database. List <DaemonLogic.BubbleLink> bubbleLinkConfigurations = (from b in entityContext.BubbleLink where b.Bubble.BubbleId == bubble.BubbleId select b).ToList <DaemonLogic.BubbleLink>(); foreach (DaemonLogic.BubbleLink bubbleLink in bubbleLinkConfigurations) { service.AddBubbleLink(bubble.BubbleId, bubbleLink.RemoteBubbleId, bubbleLink.Address, bubbleLink.Port, (float)-bubbleLink.X, (float)-bubbleLink.Y, (float)-bubbleLink.Z, true, true); } } // Saving process state info to database. localProcessStateEntity = new LocalProcessState { LocalProcessStateId = Guid.NewGuid(), LocalProcess = localProcessEntity, Participant = participantEntity, Created = DateTime.Now, Modified = DateTime.Now, Cpu = OnGetProcessingTime(), Mem = (System.Diagnostics.Process.GetCurrentProcess().WorkingSet64) / 1024 }; entityContext.AddToLocalProcessState(localProcessStateEntity); entityContext.SaveChanges(); service.Startup(false); LogUtil.Info("Daemon Process startup done."); } }