Beispiel #1
0
        public static async Task Run([TimerTrigger("0 */2 * * * *")] TimerInfo myTimer, ILogger log)
        {
            if (log == null)
            {
                throw new ArgumentNullException(nameof(log));
            }

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

            var startDateTime = DateTime.UtcNow;

            log.LogInformation($"C# Timer trigger function v3 executed at: {startDateTime}. Next occurrence on {myTimer.Schedule.GetNextOccurrence(startDateTime)}.");

            if (!string.IsNullOrEmpty(StartTranscriptionEnvironmentVariables.SecondaryLocale) && !StartTranscriptionEnvironmentVariables.SecondaryLocale.Equals("None", StringComparison.OrdinalIgnoreCase))
            {
                log.LogWarning($"Language identification is not supported for timer-based transcription creation.");
            }

            var receiver = new MessageReceiver(StartTranscriptionEnvironmentVariables.AzureServiceBus, "start_transcription_queue");
            var validServiceBusMessages = new List <Message>();
            var transcriptionHelper     = new StartTranscriptionHelper(log);

            var receiveFromServiceBus = true;

            log.LogInformation("Pulling messages from queue...");
            do
            {
                var messages = await receiver.ReceiveAsync(MaxMessagesPerRequest, TimeSpan.FromSeconds(30)).ConfigureAwait(false);

                if (messages == null)
                {
                    log.LogInformation("No remaining messages found in queue.");
                    break;
                }

                foreach (var message in messages)
                {
                    await receiver.CompleteAsync(message.SystemProperties.LockToken).ConfigureAwait(false);

                    if (transcriptionHelper.IsValidServiceBusMessage(message))
                    {
                        validServiceBusMessages.Add(message);
                    }
                }

                if (startDateTime.AddSeconds(MessageReceiveTimeoutInSeconds) > DateTime.UtcNow)
                {
                    log.LogInformation("Receiving messages from queue timed out.");
                    receiveFromServiceBus = false;
                }
                else if (messages.Count < MaxMessagesPerRequest)
                {
                    log.LogInformation("Received all remaining messages from the queue.");
                    receiveFromServiceBus = false;
                }
            }while (receiveFromServiceBus);

            if (!validServiceBusMessages.Any())
            {
                log.LogInformation("No valid messages were found in this function execution.");
                return;
            }

            log.LogInformation($"Received {validServiceBusMessages.Count} valid messages in total from queue.");

            await transcriptionHelper.StartTranscriptionsAsync(validServiceBusMessages).ConfigureAwait(false);
        }
Beispiel #2
0
        public static async Task Run([TimerTrigger("0 */2 * * * *")] TimerInfo myTimer, ILogger log)
        {
            if (log == null)
            {
                throw new ArgumentNullException(nameof(log));
            }

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

            var startDateTime = DateTime.UtcNow;

            log.LogInformation($"C# Timer trigger function v3 executed at: {startDateTime}. Next occurrence on {myTimer.Schedule.GetNextOccurrence(startDateTime)}.");

            if (!string.IsNullOrEmpty(StartTranscriptionEnvironmentVariables.SecondaryLocale) && !StartTranscriptionEnvironmentVariables.SecondaryLocale.Equals("None", StringComparison.OrdinalIgnoreCase))
            {
                log.LogWarning($"Language identification is not supported for timer-based transcription creation.");
            }

            var validServiceBusMessages = new List <Message>();
            var transcriptionHelper     = new StartTranscriptionHelper(log);

            log.LogInformation("Pulling messages from queue...");
            var messages = await MessageReceiverInstance.ReceiveAsync(StartTranscriptionEnvironmentVariables.MessagesPerFunctionExecution, TimeSpan.FromSeconds(MessageReceiveTimeoutInSeconds)).ConfigureAwait(false);

            if (messages == null || !messages.Any())
            {
                log.LogInformation($"Got no messages in this iteration.");
                return;
            }

            log.LogInformation($"Got {messages.Count} in this iteration.");
            foreach (var message in messages)
            {
                if (message.SystemProperties.LockedUntilUtc > DateTime.UtcNow.AddSeconds(5))
                {
                    try
                    {
                        if (transcriptionHelper.IsValidServiceBusMessage(message))
                        {
                            await MessageReceiverInstance.RenewLockAsync(message.SystemProperties.LockToken).ConfigureAwait(false);

                            validServiceBusMessages.Add(message);
                        }
                        else
                        {
                            await MessageReceiverInstance.CompleteAsync(message.SystemProperties.LockToken).ConfigureAwait(false);
                        }
                    }
                    catch (MessageLockLostException)
                    {
                        log.LogInformation($"Message lock expired for message. Ignore message in this iteration.");
                    }
                }
            }

            if (!validServiceBusMessages.Any())
            {
                log.LogInformation("No valid messages were found in this function execution.");
                return;
            }

            log.LogInformation($"Pulled {validServiceBusMessages.Count} valid messages from queue.");

            await transcriptionHelper.StartTranscriptionsAsync(validServiceBusMessages, MessageReceiverInstance, startDateTime).ConfigureAwait(false);
        }
Beispiel #3
0
        public static async Task Run([TimerTrigger("0 */2 * * * *")] TimerInfo myTimer, ILogger log)
        {
            if (log == null)
            {
                throw new ArgumentNullException(nameof(log));
            }

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

            var startDateTime = DateTime.UtcNow;

            log.LogInformation($"C# Timer trigger function v3 executed at: {startDateTime}. Next occurrence on {myTimer.Schedule.GetNextOccurrence(startDateTime)}.");

            var validServiceBusMessages = new List <ServiceBusReceivedMessage>();
            var transcriptionHelper     = new StartTranscriptionHelper(log);

            log.LogInformation("Pulling messages from queue...");
            var messages = await ServiceBusReceiver.ReceiveMessagesAsync(StartTranscriptionEnvironmentVariables.MessagesPerFunctionExecution, TimeSpan.FromSeconds(MessageReceiveTimeoutInSeconds)).ConfigureAwait(false);

            if (messages == null || !messages.Any())
            {
                log.LogInformation($"Got no messages in this iteration.");
                return;
            }

            log.LogInformation($"Got {messages.Count} in this iteration.");
            foreach (var message in messages)
            {
                if (message.LockedUntil > DateTime.UtcNow.AddSeconds(5))
                {
                    try
                    {
                        if (transcriptionHelper.IsValidServiceBusMessage(message))
                        {
                            await ServiceBusReceiver.RenewMessageLockAsync(message).ConfigureAwait(false);

                            validServiceBusMessages.Add(message);
                        }
                        else
                        {
                            await ServiceBusReceiver.CompleteMessageAsync(message).ConfigureAwait(false);
                        }
                    }
                    catch (ServiceBusException ex) when(ex.Reason == ServiceBusFailureReason.MessageLockLost)
                    {
                        log.LogInformation($"Message lock expired for message. Ignore message in this iteration.");
                    }
                }
            }

            if (!validServiceBusMessages.Any())
            {
                log.LogInformation("No valid messages were found in this function execution.");
                return;
            }

            log.LogInformation($"Pulled {validServiceBusMessages.Count} valid messages from queue.");

            await transcriptionHelper.StartTranscriptionsAsync(validServiceBusMessages, ServiceBusReceiver, startDateTime).ConfigureAwait(false);
        }