private void AddServiceToRuntime(IInsightRuntime runtime, Type type, object service)
 {
     if (runtime.GetService(type) == null)
     {
         runtime.AddService(type, service);
     }
 }
        public async Task StartAnalysisEngineAsync(IList <InsightType> insightTypes, IInsightRuntime runtime, CancellationToken token)
        {
            await this.singleAccess.WaitAsync(token).ConfigureAwait(false);

            try
            {
                if (this.isRunning)
                {
                    throw new ClusterAnalysisAlreadyStartedException();
                }

                var connectionInfo       = (TraceStoreConnectionInformation)runtime.GetService(typeof(TraceStoreConnectionInformation));
                var traceStoreConnection = new TraceStoreConnection(connectionInfo, runtime.GetLogProvider());

                // Add the connection to Runtime. TODO Get rid of this and pass this explicitly.
                runtime.AddService(typeof(TraceStoreConnection), traceStoreConnection);

                var analysisScheduler = new AnalysisScheduler(
                    runtime.GetLogProvider(),
                    runtime.GetStoreProvider(),
                    runtime.TaskRunner,
                    traceStoreConnection,
                    token);

                runtime.AddService(typeof(AnalysisScheduler), analysisScheduler);

                Assert.IsNotNull(connectionInfo, "connectionInfo != null");

                this.callBackStore = new SimpleCallbackStore(
                    runtime.GetStoreProvider(),
                    runtime.TaskRunner,
                    runtime.GetLogProvider(),
                    traceStoreConnection.EventStoreReader,
                    token);

                AgentDirectory.InitializeSingleInstance(
                    runtime.GetCurrentConfig(),
                    runtime.GetLogProvider(),
                    runtime.GetStoreProvider(),
                    runtime.TaskRunner,
                    traceStoreConnection,
                    (IClusterQuery)runtime.GetService(typeof(IClusterQuery)),
                    token);

                foreach (var oneInsightType in insightTypes)
                {
                    switch (oneInsightType)
                    {
                    case InsightType.PartitionInsight:
                        this.partitionInsightGenerator = new PartitionInsightGenerator(runtime, this.callBackStore, token);
                        await this.partitionInsightGenerator.StartGeneratingAsync().ConfigureAwait(false);

                        break;

                    case InsightType.ReplicaInsight:
                        break;

                    case InsightType.CodePackageInsight:
                        break;

                    case InsightType.NodeInsight:
                        break;

                    case InsightType.ClusterInsight:
                        break;

                    default:
                        throw new NotSupportedException(
                                  string.Format(CultureInfo.InvariantCulture, "Insight Type '{0}' is currently not supported", oneInsightType));
                    }
                }

                this.isRunning = true;
            }
            catch
            {
                AgentDirectory.ReleaseInstance();
                throw;
            }
            finally
            {
                this.singleAccess.Release();
            }
        }