Exemplo n.º 1
0
        private bool Build()
        {
            var allChunksComplete = false;

            if (!Settings.Current.Builder.IsLead && !Building.LookupCreated)
            {
                return(false);
            }
            if (Building.BuildingComplete)
            {
                return(false);
            }
            if (Builder.State == BuilderState.Error)
            {
                return(false);
            }

            using (
                var vocabHost = ServicesManager.CreateServiceHost <VocabularyService>(typeof(IVocabularyService),
                                                                                      "VocabularyServiceEndpoint"))
                using (
                    var redshiftHost = ServicesManager.CreateServiceHost <RedshiftService>(typeof(IRedshiftService),
                                                                                           "RedshiftServiceEndpoint"))
                {
                    ServicesManager.StartSaver();
                    if (!Building.LookupCreated)
                    {
                        UpdateDate("CreateLookupStart");
                    }

                    builderController.CreateLookup(Building.LookupCreated);

                    if (!Building.LookupCreated)
                    {
                        UpdateDate("CreateLookupEnd");
                    }

                    if (Building.LookupCreated)
                    {
                        if (!Building.BuildingStarted)
                        {
                            var start = UpdateDate("BuildingStart");
                            AddMetric(Metrics.BuildTimestamp, "", 0, start);
                        }

                        builderController.Build();

                        while ((Settings.Current.Builder.IsLead && !builderController.AllChunksComplete) ||
                               (!Settings.Current.Builder.IsLead && !builderController.AllChunksStarted))
                        {
                            if (Builder.State == BuilderState.Error)
                            {
                                break;
                            }

                            Thread.Sleep(1000);
                        }

                        if (Builder.State != BuilderState.Error)
                        {
                            if (builderController.AllChunksComplete)
                            {
                                allChunksComplete = true;
                                UpdateDate("BuildingEnd");
                                builderController.ClenupChunks();
                                //AddMetric(Metrics.BuildTimestamp, Building.BuildingStart, Building.BuildingEnd, null);

                                AddMetric(Metrics.BuildTimestamp, "",
                                          Building.BuildingEnd.Value.Subtract(Building.BuildingStart.Value).TotalMinutes,
                                          Building.BuildingEnd);

                                if (builderController.CompleteChunksCount > 0 && Settings.Current.Building.BatchSize > 0)
                                {
                                    var elapsedMilliseconds =
                                        Building.BuildingEnd.Value.Subtract(Building.BuildingStart.Value).TotalMilliseconds;
                                    var timePerPatient = elapsedMilliseconds /
                                                         (builderController.CompleteChunksCount * Settings.Current.Building.BatchSize);

                                    AddMetric(Metrics.TimePerPatient, null, null, timePerPatient);
                                }
                            }
                        }

                        ServicesManager.StopSaver();

                        vocabHost.Close();
                        redshiftHost.Close();
                    }
                }

            return(allChunksComplete);
        }