https://github.com/dotnet-labs/serviceworkercronjob
Schedule Cron Jobs using HostedService in ASP.NET Core
https://github.com/dotnet-labs/serviceworkercronjob
aspnetcore cron cronjob cronjob-scheduler dotnet dotnetcore hostedservices
Last synced: 9 months ago
JSON representation
Schedule Cron Jobs using HostedService in ASP.NET Core
- Host: GitHub
- URL: https://github.com/dotnet-labs/serviceworkercronjob
- Owner: dotnet-labs
- License: mit
- Created: 2020-07-09T03:20:04.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2024-09-28T12:50:56.000Z (almost 2 years ago)
- Last Synced: 2025-05-26T01:08:47.733Z (about 1 year ago)
- Topics: aspnetcore, cron, cronjob, cronjob-scheduler, dotnet, dotnetcore, hostedservices
- Language: C#
- Homepage:
- Size: 104 KB
- Stars: 264
- Watchers: 10
- Forks: 71
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Service Worker Cron Jobs Demo
You may want to ask, "_What's the difference between a hosted service and a background thread?_" The difference is that the hosted service will be started and stopped with the application. While, when starting a background thread, the thread cannot gracefully handle clean-up actions and will be killed when the application is stopped. The `HostedServiceExecutor` will handle the starting and stopping of a hosted service, which allows for graceful cleanup when the application is stopped.
Starts from version 2.1, .NET Core provides a native abstract base class `BackgroundService`, which includes common operations for background jobs such as `CancellationToken` and housekeeping works.
## [Medium Article: Schedule Cron Jobs using HostedService in ASP.NET Core](https://codeburst.io/schedule-cron-jobs-using-hostedservice-in-asp-net-core-e17c47ba06)
In this blog post, we will go over the use case of scheduling cron jobs using a customized `HostedService` in an ASP.NET Core Web API project. The following screen recording shows the runtime logging for the final application, which has three background tasks running at every 5 minutes, every 1 minute, and every day at 12:50 PM.

## License
Feel free to use the code in this repository as it is under MIT license.