public async Task StartAsync()

in src/WebJobs.Extensions.CosmosDB/Trigger/CosmosDBTriggerListener.cs [85:123]


        public async Task StartAsync(CancellationToken cancellationToken)
        {
            int previousStatus = Interlocked.CompareExchange(ref this._listenerStatus, ListenerRegistering, ListenerNotRegistered);

            if (previousStatus == ListenerRegistering)
            {
                throw new InvalidOperationException("The listener is already starting.");
            }
            else if (previousStatus == ListenerRegistered)
            {
                throw new InvalidOperationException("The listener has already started.");
            }

            this.InitializeBuilder();

            try
            {
                await this.StartProcessorAsync();
                Interlocked.CompareExchange(ref this._listenerStatus, ListenerRegistered, ListenerRegistering);
                this._logger.LogDebug(Events.OnListenerStarted, "Started the listener for {Details}.", this._listenerLogDetails);
            }
            catch (Exception ex)
            {
                // Reset to NotRegistered
                this._listenerStatus = ListenerNotRegistered;
                this._logger.LogError(Events.OnListenerStartError, "Starting the listener for {Details} failed. Exception: {Exception}.", this._listenerLogDetails, ex);

                // Throw a custom error if NotFound.
                if (ex is CosmosException docEx && docEx.StatusCode == HttpStatusCode.NotFound)
                {
                    // Throw a custom error so that it's easier to decipher.
                    string message = $"Either the source container '{this._cosmosDBAttribute.ContainerName}' (in database '{this._cosmosDBAttribute.DatabaseName}')  or the lease container '{this._cosmosDBAttribute.LeaseContainerName}' (in database '{this._cosmosDBAttribute.LeaseDatabaseName}') does not exist. Both containers must exist before the listener starts. To automatically create the lease container, set '{nameof(CosmosDBTriggerAttribute.CreateLeaseContainerIfNotExists)}' to 'true'.";
                    this._host = null;
                    throw new InvalidOperationException(message, ex);
                }

                throw;
            }
        }