public static async Task <bool> CreateExternalDeviceActivity([ActivityTrigger] CreateExternalDeviceInput externalDevice, TraceWriter log)
        {
            var externalDeviceRegistry = Utils.ResolveExternalDeviceRegistry();

            try
            {
                await externalDeviceRegistry.CreateExternalDevice(externalDevice.DeviceId, externalDevice.Properties);

                log.Info($"Device {externalDevice.DeviceId} created");

                Utils.TelemetryClient?.TrackEvent(Utils.Event_ExternalDeviceCreated, new Dictionary <string, string>(externalDevice.Properties)
                {
                    { "deviceId", externalDevice.DeviceId },
                    { "iothubname", externalDevice.IotHubName }
                });

                return(true);
            }
            catch (Exception ex)
            {
                Utils.TelemetryClient?.TrackEvent(Utils.Event_ExternalDeviceCreationError, new Dictionary <string, string>(externalDevice.Properties)
                {
                    { "deviceId", externalDevice.DeviceId },
                    { "iothubname", externalDevice.IotHubName },
                    { "error", ex.Message },
                });

                throw;
            }
        }
Esempio n. 2
0
        public static async Task ExternalRegistrySynchronizer_CreateDeviceOrchestration(
            [OrchestrationTrigger] DurableOrchestrationContext context, TraceWriter log)
        {
            var input = context.GetInput <DeviceCreateOrchestrationInput>();

            if (!context.IsReplaying)
            {
                log.Info($"{nameof(ExternalRegistrySynchronizer_CreateDeviceOrchestration)} started for {input.DeviceId} / {input.IoTHubName}. {nameof(Settings.Instance.TwinCheckIntervalInSeconds)}: {Settings.Instance.TwinCheckIntervalInSeconds} secs, {nameof(Settings.Instance.TwinCheckMaxRetryCount)}: {Settings.Instance.TwinCheckMaxRetryCount}, {nameof(Settings.Instance.TwinCheckMaxIntervalInSeconds)}: {Settings.Instance.TwinCheckMaxIntervalInSeconds} secs, {nameof(Settings.Instance.TwinCheckRetryTimeoutInMinutes)}: {Settings.Instance.TwinCheckRetryTimeoutInMinutes} minutes");
            }

            // 1. If it is the start wait a bit until the device twin was updated
            VerifyDeviceTwinResult deviceTwinCheckResult = null;

            try
            {
                deviceTwinCheckResult = await context.CallActivityWithRetryAsync <VerifyDeviceTwinResult>(nameof(VerifyDeviceTwinActivity),
                                                                                                          new RetryOptions(TimeSpan.FromSeconds(Settings.Instance.TwinCheckIntervalInSeconds), Settings.Instance.TwinCheckMaxRetryCount)
                {
                    BackoffCoefficient = 2,                                                                     // backoff coefficient ^2
                    MaxRetryInterval   = TimeSpan.FromSeconds(Settings.Instance.TwinCheckMaxIntervalInSeconds), // will wait for a new retry up to x seconds
                    RetryTimeout       = TimeSpan.FromMinutes(Settings.Instance.TwinCheckRetryTimeoutInMinutes) // will try up to x minutes
                },
                                                                                                          input);
            }
            catch (FunctionFailedException)
            {
                // no more retries for the device twin properties to exist, log a customEvent
                Utils.TelemetryClient?.TrackEvent(Utils.Event_DeviceTwinCheckFailed, new Dictionary <string, string>()
                {
                    { "deviceId", input.DeviceId },
                    { "iothubname", input.IoTHubName }
                });
            }

            var createExternalDeviceSucceeded = false;

            if (deviceTwinCheckResult != null)
            {
                // Will only reach here once it succeededs (required properties were found)
                var createExternalDeviceInput = new CreateExternalDeviceInput
                {
                    DeviceId   = input.DeviceId,
                    IotHubName = input.IoTHubName,
                    Properties = deviceTwinCheckResult.Properties,
                };


                try
                {
                    createExternalDeviceSucceeded = await context.CallActivityWithRetryAsync <bool>(nameof(CreateExternalDeviceActivity),
                                                                                                    new RetryOptions(TimeSpan.FromSeconds(Settings.Instance.ExternalSystemCallRetryIntervalInSeconds), Settings.Instance.ExternalSystemCallMaxRetryCount)
                    {
                        BackoffCoefficient = 2,                                                                              // backoff coefficient ^2
                        MaxRetryInterval   = TimeSpan.FromSeconds(Settings.Instance.ExternalSystemCallMaxIntervalInSeconds), // will wait for a new retry up to x seconds
                        RetryTimeout       = TimeSpan.FromMinutes(Settings.Instance.ExternalSystemCallRetryTimeoutInMinutes) // will try up to x minutes
                    },
                                                                                                    createExternalDeviceInput);
                }
                catch (FunctionFailedException)
                {
                    // no more retries for the device twin properties to exist, log a customEvent
                    Utils.TelemetryClient?.TrackEvent(Utils.Event_ExternalDeviceCreationFailed, new Dictionary <string, string>()
                    {
                        { "deviceId", input.DeviceId },
                        { "iothubname", input.IoTHubName }
                    });

                    createExternalDeviceSucceeded = false;
                }
            }

            log.Info($"{nameof(ExternalRegistrySynchronizer_CreateDeviceOrchestration)} finished for {input.DeviceId} / {input.IoTHubName}. Succeeded: {createExternalDeviceSucceeded.ToString()}");
        }