public void RecoverExistingDroplets() { if (!File.Exists(this.droplets.AppStateFile)) { this.droplets.RecoveredDroplets = true; return; } object[] instances = JsonConvertibleObject.DeserializeFromJsonArray(File.ReadAllText(this.droplets.AppStateFile)); foreach (object obj in instances) { DropletInstance instance = null; try { instance = new DropletInstance(); instance.Properties.FromJsonIntermediateObject(obj); instance.Properties.Orphaned = true; instance.Properties.ResourcesTracked = false; this.monitoring.AddInstanceResources(instance); instance.Properties.StopProcessed = false; instance.JobObject.JobMemoryLimit = instance.Properties.MemoryQuotaBytes; try { instance.LoadPlugin(); instance.Properties.EnvironmentVariables[VcapAppPidVariable] = instance.Properties.ProcessId.ToString(CultureInfo.InvariantCulture); List<ApplicationVariable> appVariables = new List<ApplicationVariable>(); foreach (KeyValuePair<string, string> appEnv in instance.Properties.EnvironmentVariables) { ApplicationVariable appVariable = new ApplicationVariable(); appVariable.Name = appEnv.Key; appVariable.Value = appEnv.Value; appVariables.Add(appVariable); } instance.Plugin.RecoverApplication(appVariables.ToArray()); } catch (Exception ex) { instance.ErrorLog.Error(ex.ToString()); } if (instance.Properties.State == DropletInstanceState.Starting) { this.DetectAppReady(instance); } this.droplets.AddDropletInstance(instance); instance = null; } catch (Exception ex) { Logger.Warning(Strings.ErrorRecoveringDropletWarningMessage, instance.Properties.InstanceId, ex.ToString()); } finally { if (instance != null) { instance.Dispose(); } } } this.droplets.RecoveredDroplets = true; if (this.monitoring.Clients > 0) { Logger.Info(Strings.DeaRecoveredApplications, this.monitoring.Clients); } this.MonitorApps(); this.droplets.ForEach(delegate(DropletInstance instance) { this.RegisterInstanceWithRouter(instance); }); this.SendHeartbeat(); this.droplets.ScheduleSnapshotAppState(); }
private void StartDropletInstance(DropletInstance instance, string sha1, string executableFile, Uri executableUri) { try { string tgzFile = Path.Combine(this.stager.StagedDir, sha1 + ".tgz"); this.stager.StageAppDirectory(executableFile, executableUri, sha1, tgzFile, instance); Logger.Debug(Strings.Downloadcompleate); string starting = string.Format(CultureInfo.InvariantCulture, Strings.StartingUpInstanceOnPort, instance.Properties.LoggingId, instance.Properties.Port); if (!string.IsNullOrEmpty(instance.Properties.DebugMode)) { Logger.Info(starting + Strings.WithDebuggerPort, instance.Properties.DebugPort); } else { Logger.Info(starting); } Logger.Debug(Strings.Clients, this.monitoring.Clients); Logger.Debug(Strings.ReservedMemoryUsageMb, this.monitoring.MemoryReservedMbytes, this.monitoring.MaxMemoryMbytes); List<ApplicationVariable> appVariables = new List<ApplicationVariable>(); try { instance.Lock.EnterWriteLock(); instance.Properties.WindowsPassword = "******" + Credentials.GenerateCredential(); instance.Properties.WindowsUserName = WindowsVCAPUsers.CreateUser(instance.Properties.InstanceId, instance.Properties.WindowsPassword); instance.Properties.EnvironmentVariables.Add(VcapWindowsUserVariable, instance.Properties.WindowsUserName); instance.Properties.EnvironmentVariables.Add(VcapWindowsUserPasswordVariable, instance.Properties.WindowsPassword); instance.Properties.EnvironmentVariables.Add(VcapPluginStagingInfoVariable, File.ReadAllText(Path.Combine(instance.Properties.Directory, "startup"))); foreach (KeyValuePair<string, string> appEnv in instance.Properties.EnvironmentVariables) { ApplicationVariable appVariable = new ApplicationVariable(); appVariable.Name = appEnv.Key; appVariable.Value = appEnv.Value; appVariables.Add(appVariable); } } finally { instance.Lock.ExitWriteLock(); } DateTime start = DateTime.Now; instance.LoadPlugin(); instance.Plugin.ConfigureApplication(appVariables.ToArray()); instance.Plugin.StartApplication(); int pid = instance.Plugin.GetApplicationProcessId(); Logger.Debug(Strings.TookXTimeToLoadConfigureAndStartDebugMessage, (DateTime.Now - start).TotalSeconds); try { instance.Lock.EnterWriteLock(); if (!instance.Properties.StopProcessed) { Logger.Info(Strings.PidAssignedToDroplet, pid, instance.Properties.LoggingId); instance.Properties.ProcessId = pid; this.droplets.ScheduleSnapshotAppState(); } } finally { instance.Lock.ExitWriteLock(); } this.DetectAppReady(instance); } catch (Exception ex) { Logger.Warning(Strings.FailedStagingAppDir, instance.Properties.Directory, instance.Properties.LoggingId, ex.ToString()); try { instance.Lock.EnterWriteLock(); instance.Properties.State = DropletInstanceState.Crashed; instance.Properties.ExitReason = DropletExitReason.Crashed; instance.Properties.StateTimestamp = DateTime.Now; this.StopDroplet(instance); } finally { instance.Lock.ExitWriteLock(); } } }