DEV Community

Cover image for Building A .NET Core 3 Scheduled Job Worker Service

Building A .NET Core 3 Scheduled Job Worker Service

James Hickey on June 13, 2019

The .NET Core CLI comes with tons of pre-built project templates! One of the new templates that will be included with .NET Core 3 will be for build...
Collapse
 
kaos profile image
Kai Oswald

I really like the concept of Invocables! It keeps the code clean.

For scheduling I've always used hangfire, which works really well, but things can get messy really fast since scheduling in hangfire works with anonymous method invocation

var jobId = BackgroundJob.Schedule(
    () => Console.WriteLine("Delayed!"),
    TimeSpan.FromDays(7));

Is there a way to update/cancel a scheduled task with Coravel (like in the above example where a jobId is returned?
In my side project I'm currently using hangfire, but Coravel looks a lot cleaner and I've already wondered how I can clean up these hangfire invocations.

Collapse
 
jamesmh profile image
James Hickey

Hangfire is def the defacto right now. Coravel was never built as a direct alternative, but many have pointed out that it's much easier to use.

Also, Coravel supports true async jobs, whereas Hangfire doesn't actually support true async jobs. So all that I/O in your background jobs will actually block your threads 🤪.

See here for more

So, the answer to your question is "yes and no". There's an open issue here that I have on the todo list.

I offered a temporary/potential solution for now in that issue. Basically, you would just manage the tasks in a collection yourself. Coravel gives you some lower-level methods to start/stop any jobs you want (although, it's a workaround of sorts until there is an actual feature added 😂).

Collapse
 
jamesmh profile image
James Hickey

You should check out builtwithdot.net/ - it's a showcase of projects built with .NET. You can filter the results by using the first filter on the top left to limit it to .NET Core.

Collapse
 
tfitz237 profile image
tfitz237

Interesting!

We've always used Window's Task Scheduler for our CRON jobs.

  • Let the app not have to worry about it being a scheduled task, and let Windows worry about that instead.
  • Deployment configuration creates the scheduled task and determines time of day / enabled status.

This seems like an interesting alternative, and allows more control at the application level. But comparatively in our current configuration, to change the time of day or enabled/disabled status, we don't have to deploy a new version of the application to PROD, just change it in the task scheduler.

The Pro dashboard seems a lot more user friendly way of dealing with the tasks too, and wouldn't require as many user permissions.

Thanks for the tutorial

Collapse
 
jamesmh profile image
James Hickey

Thanks for the feedback!

One of the issues it seems many devs run into with Window's task scheduler are, as you said, basic user permissions 😂.

Plus, if you want to move all your infrastructure to serverless / container services, etc. then with something like Coravel you literally have nothing to change. Using Windows Task Scheduler def. couples you to using a full VM or bare-metal infra.

And yes, Coravel Pro stores all the schedules in your DB so you can just change schedules on PROD with no deployments 👍

.NET Core 3 will have lots of cool stuff! Looking forward to it.

Collapse
 
codenthusiast profile image
Popoola Gafar Babatunde

Thanks for creating this beautiful solution. This is the future. Mailer, events and queue? This is really amazing!!!

Collapse
 
jamesmh profile image
James Hickey

Thanks!

Collapse
 
lumogox profile image
Luis Molina

Seams like something I can use in my next project!, nice work :)

Do you have any recommendations on how to implement this with logging with a database for .net core 3?

Collapse
 
jamesmh profile image
James Hickey

I think you can inject ILogger<T> into any class you want when using the built-in DI system.

For a database, you could use something like Dapper or integrate EF core into the library (this article might help)?

Collapse
 
gfrgomes profile image
Gustavo Gomes

Great article just what I needed! Thank you!

Collapse
 
jamesmh profile image
James Hickey

Thanks!

Collapse
 
gyurisc profile image
gyurisc

Great article! Thanks for writing it!