public static async Task Run([TimerTrigger("0 0 0 1 * *"
#if DEBUG
                                                   , RunOnStartup = true
#endif
                                                   )] TimerInfo myTimer,
                                     [SendGrid(ApiKey = "SendGridKey")] IAsyncCollector <SendGridMessage> messageCollector,
                                     ILogger logger)
        {
            _logger = logger;
            logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            Settings.Init(logger);

            MailHelper.Init(logger, messageCollector);
            await MailHelper.SendEmailForActivityStartedAsync();

            AzureHelper.Init(logger);

            var errors        = new List <(string hostname, string errorMessage)>();
            var appProperties = await BuildAppPropertiesListAsync();

            if (appProperties != null && appProperties.Any())
            {
                await CertificatesHelper.InitAsync(logger, Settings.UseStaging?CertificateMode.Staging : CertificateMode.Production);

                foreach (var appProperty in appProperties)
                {
                    AzureHelper.InitAppProperty(appProperty);
                    CertificatesHelper.InitAppProperty(appProperty);

                    try
                    {
                        //Request certificate and install it if all is ok
                        if (await CertificatesHelper.GetCertificateAsync())
                        {
                            await AzureHelper.AddCertificateAsync();
                        }
                    }
                    catch (Exception ex)
                    {
                        var message = $"Unable to complete the processing for {appProperty.Hostname}";
                        logger.LogError(ex, message);
                        await MailHelper.SendEmailForErrorAsync(ex, message);

                        errors.Add((hostname: appProperty.Hostname, errorMessage: ex.Message));
                    }
                }
            }

            AzureHelper.Dispose();
            await MailHelper.SendEmailForActivityCompletedAsync(errors);
        }
Ejemplo n.º 2
0
        public static async Task Run([TimerTrigger("%WebAppSSLManager-Trigger%"
#if DEBUG
                                                   , RunOnStartup = true
#endif
                                                   )] TimerInfo myTimer,
                                     [SendGrid(ApiKey = "SendGridKey")] IAsyncCollector <SendGridMessage> messageCollector,
                                     ILogger logger)
        {
            _logger = logger;
            logger.LogInformation($"C# Timer trigger function executed at: {DateTime.Now}");

            Settings.Init(logger);

            MailHelper.Init(logger, messageCollector);
            await MailHelper.SendEmailForActivityStartedAsync();

            AzureHelper.Init(logger);

            var details       = new List <(string hostname, string message)>();
            var appProperties = await BuildAppPropertiesListAsync();

            int certsCreated = 0;

            var CertUpdatedMessage    = "Certificate successfully updated";
            var CertNotUpdatedMessage = "Certificate not updated";

            if (appProperties != null && appProperties.Any())
            {
                await CertificatesHelper.InitAsync(logger, Settings.UseStaging?CertificateMode.Staging : CertificateMode.Production);

                foreach (var appProperty in appProperties)
                {
                    AzureHelper.InitAppProperty(appProperty);
                    CertificatesHelper.InitAppProperty(appProperty);

                    try
                    {
                        //Request certificate and install it if all is ok
                        if (await AzureHelper.NeedsNewCertificateAsync() && await CertificatesHelper.GetCertificateAsync())
                        {
                            await AzureHelper.AddCertificateAsync();

                            certsCreated++;
                            details.Add((hostname: appProperty.Hostname, message: CertUpdatedMessage));
                        }
                        else
                        {
                            details.Add((hostname: appProperty.Hostname, message: CertNotUpdatedMessage));
                        }
                    }
                    catch (Exception ex)
                    {
                        var message = $"Unable to complete the processing for {appProperty.Hostname}";
                        logger.LogError(ex, message);
                        details.Add((hostname: appProperty.Hostname, message: ex.Message));
                    }

                    if (Settings.BatchSize > 0 && certsCreated >= Settings.BatchSize)
                    {
                        logger.LogInformation($"Maximum number of certificates ({Settings.BatchSize}) generated this run - exiting.");
                        break;
                    }
                }
            }

            AzureHelper.Dispose();
            await MailHelper.SendEmailForActivityCompletedAsync(details);
        }