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

Simplify continuous webjob by using background service #1

Merged
merged 14 commits into from
Apr 25, 2019
Merged
Show file tree
Hide file tree
Changes from 2 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
13 changes: 4 additions & 9 deletions AppInsightsWithWebJob/ContinuousJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

namespace AppInsightsWithWebJob
{
public class ContinuousJob : IHostedService
public class ContinuousJob : BackgroundService
{
private readonly ILogger<ContinuousJob> logger;

Expand All @@ -15,13 +15,13 @@ public ContinuousJob(ILogger<ContinuousJob> logger)
this.logger = logger;
}

public async Task StartAsync(CancellationToken cancellationToken)
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
{
var counter = 0;
while (!cancellationToken.IsCancellationRequested)
while (!stoppingToken.IsCancellationRequested)
SeanFeldman marked this conversation as resolved.
Show resolved Hide resolved
{
Console.Write(".");
await Task.Delay(100, cancellationToken);
await Task.Delay(100, stoppingToken);

if (counter++ > 20)
{
Expand All @@ -30,10 +30,5 @@ public async Task StartAsync(CancellationToken cancellationToken)
}
}
}

public Task StopAsync(CancellationToken cancellationToken)
{
return Task.CompletedTask;
}
}
}
19 changes: 11 additions & 8 deletions AppInsightsWithWebJob/Program.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Threading.Tasks;
using System.Threading.Tasks;
using Microsoft.ApplicationInsights.Extensibility;
using Microsoft.Azure.WebJobs;
using Microsoft.Extensions.DependencyInjection;
Expand All @@ -8,6 +7,10 @@

namespace AppInsightsWithWebJob
{
using System;
SeanFeldman marked this conversation as resolved.
Show resolved Hide resolved
using System.Threading;
using Microsoft.ApplicationInsights;

class Program
{
static async Task Main(string[] args)
Expand Down Expand Up @@ -35,16 +38,16 @@ static async Task Main(string[] args)
});

var host = builder.Build();
var cancellationToken = new WebJobsShutdownWatcher().Token;

var telemetryClient = host.Services.GetService<TelemetryClient>();
SeanFeldman marked this conversation as resolved.
Show resolved Hide resolved
AppDomain.CurrentDomain.UnhandledException += (sender, eventArgs) => telemetryClient.TrackException((Exception)eventArgs.ExceptionObject);
TaskScheduler.UnobservedTaskException += (sender, eventArgs) => telemetryClient.TrackException((Exception)eventArgs.Exception);

using (cancellationToken.Register(() => host.Services.GetService<IJobHost>().StopAsync().GetAwaiter().GetResult()))
using (host)
try
{
await host.RunAsync();
}
catch (Exception exception)
{
await host.RunAsync(cancellationToken);
telemetryClient.TrackException(exception);
SeanFeldman marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down