Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove custom scheduling implementation and migrate to CronScheduler.AspNetCore #408

Merged
merged 3 commits into from
Mar 18, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 0 additions & 6 deletions src/Promitor.Scraper.Host/Docs/Open-Api.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
using System;
using System.IO;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.DependencyInjection;
using Promitor.Core.Telemetry.Interfaces;
using Promitor.Scraper.Host.Scheduling;
using Promitor.Scraper.Host.Scheduling.Infrastructure.Extensions;
using Promitor.Scraper.Host.Scheduling.Interfaces;
using Swashbuckle.AspNetCore.Swagger;

// ReSharper disable once CheckNamespace
Expand All @@ -20,11 +19,10 @@ public static class IServiceCollectionExtensions
/// <param name="services">Collections of services in application</param>
public static void UseCronScheduler(this IServiceCollection services)
{
services.AddSingleton<IScheduledTask, AzureMonitorScrapingTask>();
services.AddScheduler((sender, args) =>
services.AddScheduler(builder =>
{
Console.Write(args.Exception.Message);
args.SetObserved();
builder.AddJob<MetricScrapingJob>();
builder.UnobservedTaskExceptionHandler = (sender, exceptionEventArgs) => UnobservedJobHandlerHandler(sender, exceptionEventArgs, services);
});
}

Expand Down Expand Up @@ -81,5 +79,14 @@ private static string GetXmlDocumentationPath(IServiceCollection services)

return File.Exists(xmlDocumentationPath) ? xmlDocumentationPath : string.Empty;
}

private static void UnobservedJobHandlerHandler(object sender, UnobservedTaskExceptionEventArgs e, IServiceCollection services)
{
var exceptionTrackerService = services.FirstOrDefault(service => service.ServiceType == typeof(IExceptionTracker));
var exceptionTracker = (IExceptionTracker)exceptionTrackerService?.ImplementationInstance;
exceptionTracker?.Track(e.Exception);

e.SetObserved();
}
}
}
2 changes: 1 addition & 1 deletion src/Promitor.Scraper.Host/Promitor.Scraper.Host.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,9 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="CronScheduler.AspNetCore" Version="1.0.11" />
<PackageReference Include="Microsoft.AspNetCore.App" Version="2.2.3" />
<PackageReference Include="Prometheus.Client.AspNetCore" Version="2.1.0" />
<PackageReference Include="Shuttle.Core.Cron" Version="10.0.6" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="4.0.1" />
<PackageReference Include="Swashbuckle.AspNetCore.Annotations" Version="4.0.1" />
<PackageReference Include="System.Net.Http" Version="4.3.4" />
Expand Down

This file was deleted.

22 changes: 0 additions & 22 deletions src/Promitor.Scraper.Host/Scheduling/Cron/CronSchedule.cs

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

78 changes: 78 additions & 0 deletions src/Promitor.Scraper.Host/Scheduling/MetricScrapingJob.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
using System;
using System.Collections.Generic;
using System.Threading;
using System.Threading.Tasks;
using CronScheduler.AspNetCore;
using GuardNet;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Promitor.Core;
using Promitor.Core.Scraping.Configuration.Model;
using Promitor.Core.Scraping.Configuration.Model.Metrics;
using Promitor.Core.Scraping.Configuration.Providers.Interfaces;
using Promitor.Core.Scraping.Factories;
using Promitor.Core.Telemetry.Interfaces;

namespace Promitor.Scraper.Host.Scheduling
{
public class MetricScrapingJob : IScheduledJob
{
private readonly IMetricsDeclarationProvider _metricsDeclarationProvider;
private readonly IExceptionTracker _exceptionTracker;
private readonly ILogger _logger;

public MetricScrapingJob(IMetricsDeclarationProvider metricsDeclarationProvider, ILogger logger, IExceptionTracker exceptionTracker)
{
Guard.NotNull(metricsDeclarationProvider, nameof(metricsDeclarationProvider));
Guard.NotNull(exceptionTracker, nameof(exceptionTracker));
Guard.NotNull(logger, nameof(logger));

_metricsDeclarationProvider = metricsDeclarationProvider;
_exceptionTracker = exceptionTracker;
_logger = logger;

ConfigureJob();
}

public string CronSchedule { get; set; }
public string CronTimeZone { get; }
public bool RunImmediately { get; set; }

private void ConfigureJob()
{
CronSchedule = Environment.GetEnvironmentVariable(EnvironmentVariables.Scraping.CronSchedule);
RunImmediately = false;
}

public async Task ExecuteAsync(CancellationToken cancellationToken)
{
_logger.LogInformation("Scraping Azure Monitor - {timestamp}", DateTimeOffset.Now);

try
{
var scrapeConfiguration = _metricsDeclarationProvider.Get();

var scrapingTasks = new List<Task>();

foreach (var metricDefinition in scrapeConfiguration.Metrics)
{
var scrapingTask = ScrapeMetric(scrapeConfiguration.AzureMetadata, scrapeConfiguration.MetricDefaults, metricDefinition);
scrapingTasks.Add(scrapingTask);
}
await Task.WhenAll(scrapingTasks);
}
catch (Exception exception)
{
_exceptionTracker.Track(exception);
}
}

private async Task ScrapeMetric(AzureMetadata azureMetadata, MetricDefaults metricDefaults, MetricDefinition metricDefinitionDefinition)
{
_logger.LogInformation("Scraping '{MetricName}' for resource type '{ResourceType}'", metricDefinitionDefinition.Name, metricDefinitionDefinition.ResourceType);

var scraper = MetricScraperFactory.CreateScraper(metricDefinitionDefinition.ResourceType, azureMetadata, metricDefaults, _logger, _exceptionTracker);
await scraper.ScrapeAsync(metricDefinitionDefinition);
}
}
}
1 change: 1 addition & 0 deletions src/Promitor.Scraper.Host/Startup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ public void ConfigureServices(IServiceCollection services)
jsonOptions.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter());
jsonOptions.SerializerSettings.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
});

services.UseCronScheduler();
services.UseOpenApiSpecifications(ScrapeEndpointBasePath, apiVersion: 1);
}
Expand Down
Loading