private Task OnFileWatcher(IFileItem file, object obj)
        {
            ApiHubFile apiHubFile = new ApiHubFile(file);

            TriggeredFunctionData input = new TriggeredFunctionData
            {
                TriggerValue = apiHubFile
            };

            return(_executor.TryExecuteAsync(input, CancellationToken.None));
        }
        private async Task MoveToPoisonQueueAsync(ApiHubFile apiHubFile)
        {
            await this._poisonQueue.CreateIfNotExistsAsync();

            var fileStatus = new ApiHubFileInfo
            {
                FilePath     = apiHubFile.Path,
                FunctionName = this._functionName,
                Connection   = this._connectionStringSetting
            };

            string content;

            using (StringWriter stringWriter = new StringWriter())
            {
                _serializer.Serialize(stringWriter, fileStatus);
                content = stringWriter.ToString();
            }

            var queueMessage = new CloudQueueMessage(content);

            await this._poisonQueue.AddMessageAsync(queueMessage);
        }
        private async Task OnFileWatcher(IFileItem file, object obj)
        {
            ApiHubFile apiHubFile = new ApiHubFile(file);

            TriggeredFunctionData input = new TriggeredFunctionData
            {
                TriggerValue = apiHubFile
            };

            var functionResult = await _executor.TryExecuteAsync(input, CancellationToken.None);

            string pollStatus = null;
            var    uri        = obj as Uri;

            if (uri != null)
            {
                pollStatus = uri.AbsoluteUri;
            }
            else
            {
                pollStatus = obj.ToString();
            }

            if (functionResult.Succeeded)
            {
                // If function successfully completes then the next poll Uri will be logged in an Azure Blob.
                var status = new ApiHubStatus
                {
                    PollUrl    = pollStatus,
                    FilePath   = Path.GetDirectoryName(apiHubFile.Path),
                    Connection = this._connectionStringSetting
                };

                await SetNextPollStatusAsync(status);
            }
            else
            {
                var status = await GetLastPollStatusAsync();

                if (status == null)
                {
                    status = new ApiHubStatus
                    {
                        FilePath   = Path.GetDirectoryName(apiHubFile.Path),
                        Connection = this._connectionStringSetting
                    };
                }

                if (status.RetryCount < this._apiHubConfig.MaxFunctionExecutionRetryCount)
                {
                    status.RetryCount++;
                    _trace.Error(string.Format("Function {0} failed to successfully process file {1}. Number of retries: {2}. ", _functionName, apiHubFile.Path, status.RetryCount));

                    await SetNextPollStatusAsync(status);
                    await OnFileWatcher(file, obj);
                }
                else
                {
                    // The maximum retries for the function execution has reached. The file info will be added to the poison queue and the next poll status will be updated to skip the file.
                    status.PollUrl    = pollStatus;
                    status.RetryCount = 0;
                    _trace.Error(string.Format("Function {0} failed to successfully process file {1} after max allowed retries of {2}. The file info will be moved to queue {3}.", _functionName, apiHubFile.Path, this._apiHubConfig.MaxFunctionExecutionRetryCount, PoisonQueueName));

                    await MoveToPoisonQueueAsync(apiHubFile);
                    await SetNextPollStatusAsync(status);
                }
            }
        }