public EventGridLifeCycleNotificationHelper()

in src/WebJobs.Extensions.DurableTask/EventGridLifeCycleNotificationHelper.cs [29:129]


        public EventGridLifeCycleNotificationHelper(
            DurableTaskOptions options,
            INameResolver nameResolver,
            EndToEndTraceHelper traceHelper)
        {
            this.options = options ?? throw new ArgumentNullException(nameof(options));
            this.traceHelper = traceHelper ?? throw new ArgumentNullException(nameof(traceHelper));

            if (nameResolver == null)
            {
                throw new ArgumentNullException(nameof(nameResolver));
            }

            if (options.Notifications == null)
            {
                throw new ArgumentNullException(nameof(options.Notifications));
            }

            var eventGridNotificationsConfig = options.Notifications.EventGrid ?? throw new ArgumentNullException(nameof(options.Notifications.EventGrid));

            this.eventGridKeyValue = nameResolver.Resolve(eventGridNotificationsConfig.KeySettingName);
            this.eventGridTopicEndpoint = eventGridNotificationsConfig.TopicEndpoint;

            if (nameResolver.TryResolveWholeString(eventGridNotificationsConfig.TopicEndpoint, out var endpoint))
            {
                this.eventGridTopicEndpoint = endpoint;
            }

            if (!string.IsNullOrEmpty(this.eventGridTopicEndpoint))
            {
                if (!string.IsNullOrEmpty(eventGridNotificationsConfig.KeySettingName))
                {
                    this.useTrace = true;

                    var retryStatusCode = eventGridNotificationsConfig.PublishRetryHttpStatus?
                                              .Where(x => Enum.IsDefined(typeof(HttpStatusCode), x))
                                              .Select(x => (HttpStatusCode)x)
                                              .ToArray()
                                          ?? Array.Empty<HttpStatusCode>();

                    if (eventGridNotificationsConfig.PublishEventTypes == null || eventGridNotificationsConfig.PublishEventTypes.Length == 0)
                    {
                        this.eventGridPublishEventTypes = (OrchestrationRuntimeStatus[])Enum.GetValues(typeof(OrchestrationRuntimeStatus));
                    }
                    else
                    {
                        var startedIndex = Array.FindIndex(eventGridNotificationsConfig.PublishEventTypes, x => x == "Started");
                        if (startedIndex > -1)
                        {
                            eventGridNotificationsConfig.PublishEventTypes[startedIndex] = OrchestrationRuntimeStatus.Running.ToString();
                        }

                        OrchestrationRuntimeStatus ParseAndvalidateEvents(string @event)
                        {
                            var success = Enum.TryParse(@event, out OrchestrationRuntimeStatus @enum);
                            if (success)
                            {
                                switch (@enum)
                                {
                                    case OrchestrationRuntimeStatus.Canceled:
                                    case OrchestrationRuntimeStatus.ContinuedAsNew:
                                    case OrchestrationRuntimeStatus.Pending:
                                        success = false;
                                        break;
                                    default:
                                        break;
                                }
                            }

                            if (!success)
                            {
                                throw new ArgumentException("Failed to start lifecycle notification feature. Unsupported event types detected in 'EventGridPublishEventTypes'. You may only specify one or more of the following 'Started', 'Completed', 'Failed', 'Terminated'.");
                            }

                            return @enum;
                        }

                        this.eventGridPublishEventTypes = eventGridNotificationsConfig.PublishEventTypes.Select(x => ParseAndvalidateEvents(x)).ToArray();
                    }

                    // Currently, we support Event Grid Custom Topic for notify the lifecycle event of an orchestrator.
                    // For more detail about the Event Grid, please refer this document.
                    // Post to custom topic for Azure Event Grid
                    // https://docs.microsoft.com/en-us/azure/event-grid/post-to-custom-topic
                    this.HttpMessageHandler = options.NotificationHandler ?? new HttpRetryMessageHandler(
                        new HttpClientHandler(),
                        eventGridNotificationsConfig.PublishRetryCount,
                        eventGridNotificationsConfig.PublishRetryInterval,
                        retryStatusCode);

                    if (string.IsNullOrEmpty(this.eventGridKeyValue))
                    {
                        throw new ArgumentException($"Failed to start lifecycle notification feature. Please check the configuration values for {eventGridNotificationsConfig.KeySettingName} on AppSettings.");
                    }
                }
                else
                {
                    throw new ArgumentException($"Failed to start lifecycle notification feature. Please check the configuration values for {eventGridNotificationsConfig.TopicEndpoint} and {eventGridNotificationsConfig.KeySettingName}.");
                }
            }
        }