Ejemplo n.º 1
0
        public override void Run()
        {
            // initialize splunk logging (we do this here instead of OnStart so that the Azure logger has time to really start)
            TraceLog.TraceInfo("WorkerRole started");

            // check the database schema versions to make sure there is no version mismatch
            if (!Storage.NewUserContext.CheckSchemaVersion())
            {
                TraceLog.TraceFatal("User database schema is out of sync, unrecoverable error");
                return;
            }
            if (!Storage.NewSuggestionsContext.CheckSchemaVersion())
            {
                TraceLog.TraceFatal("Suggestions database schema is out of sync, unrecoverable error");
                return;
            }

            // (re)create the database constants if the code contains a newer version
            if (!Storage.NewUserContext.VersionConstants(Me))
            {
                TraceLog.TraceFatal("Cannot check and/or update the User database constants, unrecoverable error");
                return;
            }
            if (!Storage.NewSuggestionsContext.VersionConstants(Me))
            {
                TraceLog.TraceFatal("Cannot check and/or update the Suggestions database constants, unrecoverable error");
                return;
            }

            // get the number of workers (default is 0)
            int workflowWorkerCount = ConfigurationSettings.GetAsNullableInt(HostEnvironment.WorkflowWorkerCountConfigKey) ?? 0;
            int mailWorkerCount     = ConfigurationSettings.GetAsNullableInt(HostEnvironment.MailWorkerCountConfigKey) ?? 0;
            int speechWorkerCount   = ConfigurationSettings.GetAsNullableInt(HostEnvironment.SpeechWorkerCountConfigKey) ?? 0;

            speechWorkerCount = speechWorkerCount > 0 ? 1 : 0;  // maximum number of speech worker threads is 1

            var workflowWorkerArray = new WorkflowWorker.WorkflowWorker[workflowWorkerCount];
            var mailWorkerArray     = new MailWorker.MailWorker[mailWorkerCount];
            var speechWorkerArray   = new SpeechWorker.SpeechWorker[speechWorkerCount];

            // run an infinite loop doing the following:
            //   check whether the worker services have stopped (indicated by a null reference)
            //   (re)start the service on a new thread if necessary
            //   sleep for the timeout period
            while (true)
            {
                // start workflow worker in both dev and deployed Azure fabric
                RestartWorkerThreads <WorkflowWorker.WorkflowWorker>(workflowWorkerArray);

                // start mail and speech workers only in deployed Azure fabric
                if (!HostEnvironment.IsAzureDevFabric)
                {
                    RestartWorkerThreads <MailWorker.MailWorker>(mailWorkerArray);
                }
                if (!HostEnvironment.IsAzureDevFabric)
                {
                    RestartWorkerThreads <SpeechWorker.SpeechWorker>(speechWorkerArray);
                }

                // sleep for the timeout period
                Thread.Sleep(timeout);
            }
        }