Esempio n. 1
0
        private static async Task <FileNotification> VerifyFileNotification(FileNotificationReceiver <FileNotification> fileNotificationReceiver, string deviceId)
        {
            FileNotification fileNotification = null;

            Stopwatch sw = new Stopwatch();

            sw.Start();
            while (sw.Elapsed.TotalMinutes < 2)
            {
                // Receive the file notification from queue
                fileNotification = await fileNotificationReceiver.ReceiveAsync(TimeSpan.FromSeconds(20)).ConfigureAwait(false);

                if (fileNotification != null)
                {
                    if (fileNotification.DeviceId == deviceId)
                    {
                        await fileNotificationReceiver.CompleteAsync(fileNotification).ConfigureAwait(false);

                        break;
                    }

                    await fileNotificationReceiver.AbandonAsync(fileNotification).ConfigureAwait(false);

                    fileNotification = null;
                }
            }
            sw.Stop();
            return(fileNotification);
        }
        private static async Task StartReceivingLoopAsync()
        {
            s_log.WriteLine("Starting receiving file notification loop...");

            CancellationToken cancellationToken = new CancellationTokenSource(s_duration).Token;

            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    FileNotification fileNotification = await s_fileNotificationReceiver.ReceiveAsync(s_interval).ConfigureAwait(false);

                    if (fileNotification != null)
                    {
                        string key = RetrieveKey(fileNotification.BlobName);
                        s_fileNotifications.TryAdd(key, fileNotification);
                        s_log.WriteLine($"File notification received deviceId={fileNotification.DeviceId}, blobName={fileNotification.BlobName}.");
                        await s_fileNotificationReceiver.AbandonAsync(fileNotification).ConfigureAwait(false);
                    }
                }
                catch (Exception)
                {
                    s_log.WriteLine("Ignore any exception while receiving/abandon file upload notification.");
                }
            }

            s_log.WriteLine("End receiving file notification loop.");
        }
Esempio n. 3
0
        private static async Task <FileNotification> VerifyFileNotification(Tuple <string, string> deviceInfo)
        {
            FileNotification fileNotification = null;

            Stopwatch sw = new Stopwatch();

            sw.Start();
            while (sw.Elapsed.Minutes < 2)
            {
                // Receive the file notification from queue
                fileNotification = await fileNotificationReceiver.ReceiveAsync(TimeSpan.FromSeconds(20));

                if (fileNotification != null)
                {
                    if (fileNotification.DeviceId == deviceInfo.Item1)
                    {
                        await fileNotificationReceiver.CompleteAsync(fileNotification);

                        break;
                    }

                    await fileNotificationReceiver.AbandonAsync(fileNotification);

                    fileNotification = null;
                }
            }
            sw.Stop();
            return(fileNotification);
        }
Esempio n. 4
0
        public async Task StartListenAsync()
        {
            try
            {
                FileNotificationReceiver <FileNotification> fileNotificationReceiver = _serviceClient.GetFileNotificationReceiver();
                while (true)
                {
                    FileNotification fileNotification = null;
                    try
                    {
                        fileNotification = await fileNotificationReceiver.ReceiveAsync();

                        if (fileNotification != null)
                        {
                            await SendToWebAppAsync(fileNotification);

                            await fileNotificationReceiver.CompleteAsync(fileNotification);
                        }
                    }
                    catch (Exception ex)
                    {
                        await fileNotificationReceiver.AbandonAsync(fileNotification);
                    }
                }
            }
            catch (Exception ex)
            {
                Debug.WriteLine(ex.Message);
            }
        }
        private async Task ReceiveFileUploadNotifications(string targetDeviceId, CancellationToken cancellationToken)
        {
            if (!string.IsNullOrWhiteSpace(targetDeviceId))
            {
                _logger.LogInformation($"Target device is specified, will only complete matching notifications.");
            }

            _logger.LogInformation($"Listening for file upload notifications from the service.");

            FileNotificationReceiver <FileNotification> notificationReceiver = _serviceClient.GetFileNotificationReceiver();

            int totalNotificationsReceived  = 0;
            int totalNotificationsCompleted = 0;
            int totalNotificationsAbandoned = 0;

            while (!cancellationToken.IsCancellationRequested)
            {
                try
                {
                    FileNotification fileUploadNotification = await notificationReceiver.ReceiveAsync(s_notificationReceiverTimeout);

                    if (fileUploadNotification == null)
                    {
                        _logger.LogInformation($"Did not receive any notification after {s_notificationReceiverTimeout.TotalSeconds} seconds.");
                        continue;
                    }

                    totalNotificationsReceived++;

                    _logger.LogInformation($"Received file upload notification.");
                    _logger.LogInformation($"\tDeviceId: {fileUploadNotification.DeviceId ?? "N/A"}.");
                    _logger.LogInformation($"\tFileName: {fileUploadNotification.BlobName ?? "N/A"}.");
                    _logger.LogInformation($"\tEnqueueTimeUTC: {fileUploadNotification.EnqueuedTimeUtc}.");
                    _logger.LogInformation($"\tBlobSizeInBytes: {fileUploadNotification.BlobSizeInBytes}.");

                    // If the targetDeviceId is set and does not match the notification's origin, ignore it by abandoning the notification.
                    // Completing a notification will remove that notification from the service's queue so it won't be delivered to any other receiver again.
                    // Abandoning a notification will put it back on the queue to be re-delivered to receivers. This is mostly used when multiple receivers
                    // are configured and each receiver is only interested in notifications from a particular device/s.
                    if (!string.IsNullOrWhiteSpace(targetDeviceId) &&
                        !string.Equals(fileUploadNotification.DeviceId, targetDeviceId, StringComparison.OrdinalIgnoreCase))
                    {
                        _logger.LogInformation($"Marking notification for {fileUploadNotification.DeviceId} as Abandoned.");

                        await notificationReceiver.AbandonAsync(fileUploadNotification);

                        _logger.LogInformation($"Successfully marked the notification for device {fileUploadNotification.DeviceId} as Abandoned.");
                        totalNotificationsAbandoned++;
                    }
                    else
                    {
                        _logger.LogInformation($"Marking notification for {fileUploadNotification.DeviceId} as Completed.");

                        await notificationReceiver.CompleteAsync(fileUploadNotification);

                        _logger.LogInformation($"Successfully marked the notification for device {fileUploadNotification.DeviceId} as Completed.");
                        totalNotificationsCompleted++;
                    }
                }
                catch (Exception e) when((e is IotHubException) || (e is DeviceMessageLockLostException))
                {
                    _logger.LogWarning($"Caught a recoverable exception, will retry: {e.Message} - {e}");
                }
            }

            _logger.LogInformation($"Total Notifications Received: {totalNotificationsReceived}.");
            _logger.LogInformation($"Total Notifications Marked as Completed: {totalNotificationsCompleted}.");
            _logger.LogInformation($"Total Notifications Marked as Abandoned: {totalNotificationsAbandoned}.");
        }