private void TryToStartThreads()
        {
            this.devicesStateTask   = this.factory.Resolve <IDeviceStateTask>();
            this.devicesStateThread = new Thread(
                () => this.devicesStateTask.Run(this.deviceStateActors, this.runningToken.Token));

            this.devicesConnectionTask   = this.factory.Resolve <IDeviceConnectionTask>();
            this.devicesConnectionThread = new Thread(
                () => this.devicesConnectionTask.RunAsync(
                    this.simulationManagers,
                    this.deviceConnectionActors,
                    this.runningToken.Token));

            // Create task and thread only if the device twin integration is enabled
            if (this.deviceTwinEnabled)
            {
                this.devicesPropertiesTask   = this.factory.Resolve <IUpdatePropertiesTask>();
                this.devicesPropertiesThread = new Thread(
                    () => this.devicesPropertiesTask.RunAsync(
                        this.simulationManagers,
                        this.devicePropertiesActors,
                        this.runningToken.Token));
            }

            // State
            try
            {
                this.devicesStateThread.Start();
            }
            catch (Exception e)
            {
                var msg = "Unable to start the device-state thread";
                this.log.Error(msg, e);
                this.logDiagnostics.LogServiceError(msg, e);
                throw new Exception("Unable to start the device-state thread", e);
            }

            // Connection
            try
            {
                this.devicesConnectionThread.Start();
            }
            catch (Exception e)
            {
                var msg = "Unable to start the device-connection thread";
                this.log.Error(msg, e);
                this.logDiagnostics.LogServiceError(msg, e);
                throw new Exception("Unable to start the device-connection thread", e);
            }

            // Properties
            if (this.deviceTwinEnabled)
            {
                try
                {
                    this.devicesPropertiesThread.Start();
                }
                catch (Exception e)
                {
                    var msg = "Unable to start the device-properties thread";
                    this.log.Error(msg, e);
                    this.logDiagnostics.LogServiceError(msg, e);
                    throw new Exception("Unable to start the device-properties thread", e);
                }
            }
            else
            {
                this.log.Info("The device properties thread will not start because it is disabled in the global configuration");
            }

            // Telemetry
            try
            {
                var telemetryThreadCount = this.appConcurrencyConfig.TelemetryThreads;

                this.devicesTelemetryThreads = new Thread[telemetryThreadCount];
                this.devicesTelemetryTasks   = new List <IDeviceTelemetryTask>();
                for (int i = 0; i < telemetryThreadCount; i++)
                {
                    var task = this.factory.Resolve <IDeviceTelemetryTask>();
                    this.devicesTelemetryTasks.Add(task);

                    // Thread position must be calculated outside of the thread-execution lambda. Otherwise,
                    // the thread index passed to the execution method will be off by one.
                    var telemetryThreadPosition = i + 1;
                    this.devicesTelemetryThreads[i] = new Thread(
                        () => task.RunAsync(this.deviceTelemetryActors, telemetryThreadPosition, telemetryThreadCount, this.runningToken.Token));
                    this.devicesTelemetryThreads[i].Start();
                }
            }
            catch (Exception e)
            {
                var msg = "Unable to start the device-telemetry threads";
                this.log.Error(msg, e);
                this.logDiagnostics.LogServiceError(msg, e);
                throw new Exception("Unable to start the device-telemetry threads", e);
            }
        }
Example #2
0
        private void TryToStartThreads()
        {
            this.devicesStateTask   = this.factory.Resolve <IDeviceStateTask>();
            this.devicesStateThread = new Thread(
                () => this.devicesStateTask.Run(this.deviceStateActors, this.runningToken.Token));

            this.devicesConnectionTask   = this.factory.Resolve <IDeviceConnectionTask>();
            this.devicesConnectionThread = new Thread(
                () => this.devicesConnectionTask.RunAsync(
                    this.simulationManagers,
                    this.deviceConnectionActors,
                    this.runningToken.Token));

            this.devicesPropertiesTask   = this.factory.Resolve <IUpdatePropertiesTask>();
            this.devicesPropertiesThread = new Thread(
                () => this.devicesPropertiesTask.RunAsync(
                    this.simulationManagers,
                    this.devicePropertiesActors,
                    this.runningToken.Token));

            // State
            try
            {
                this.devicesStateThread.Start();
            }
            catch (Exception e)
            {
                var msg = "Unable to start the device-state thread";
                this.log.Error(msg, e);
                this.logDiagnostics.LogServiceError(msg, e);
                throw new Exception("Unable to start the device-state thread", e);
            }

            // Connection
            try
            {
                this.devicesConnectionThread.Start();
            }
            catch (Exception e)
            {
                var msg = "Unable to start the device-connection thread";
                this.log.Error(msg, e);
                this.logDiagnostics.LogServiceError(msg, e);
                throw new Exception("Unable to start the device-connection thread", e);
            }

            // Properties
            try
            {
                this.devicesPropertiesThread.Start();
            }
            catch (Exception e)
            {
                var msg = "Unable to start the device-properties thread";
                this.log.Error(msg, e);
                this.logDiagnostics.LogServiceError(msg, e);
                throw new Exception("Unable to start the device-properties thread", e);
            }

            // Telemetry
            try
            {
                var count = this.appConcurrencyConfig.TelemetryThreads;

                this.devicesTelemetryThreads = new Thread[count];
                this.devicesTelemetryTasks   = new List <IDeviceTelemetryTask>();
                for (int i = 0; i < count; i++)
                {
                    var task = this.factory.Resolve <IDeviceTelemetryTask>();
                    this.devicesTelemetryTasks.Add(task);

                    this.devicesTelemetryThreads[i] = new Thread(
                        () => task.RunAsync(this.deviceTelemetryActors, i + 1, count, this.runningToken.Token));
                    this.devicesTelemetryThreads[i].Start();
                }
            }
            catch (Exception e)
            {
                var msg = "Unable to start the device-telemetry threads";
                this.log.Error(msg, e);
                this.logDiagnostics.LogServiceError(msg, e);
                throw new Exception("Unable to start the device-telemetry threads", e);
            }
        }