dotnet-labs / ServiceWorkerCronJob

Schedule Cron Jobs using HostedService in ASP.NET Core
MIT License
264 stars 72 forks source link

exception handling in child jobs #9

Closed laventnc closed 3 years ago

laventnc commented 3 years ago

For instance, a few of my jobs call internal web services/applications and I want to cleanly handle unlikely exceptions (such as a System.Net.Http.HttpRequestException).

I currently have an abstract base class DomainCronJob : CronJobService, which calls an abstract method Run( CancellationToken cancellationToken );. As expected, uncaught exceptions bubble up to the CrobJobService call to await DoWork( cancellationToken ). I'm a little new to hosted services and was wondering if there was a reason you left out a generic try { await DoWork(...) } catch (Exception) {} from this call / what you think the best course of action would be.

changhuixu commented 3 years ago

You can do that to catch all exceptions at the root level. I don't have strong opinions. My only suggestion is that the DoWork method should be clean which simply creates a DI scope and call a method in a service.

For me, I usually catch exceptions inside each child service. But it's just a preference.

laventnc commented 3 years ago

Awesome, I appreciate the insight!