An .NET6 sample code using Quartz.NET and Spectre.Console.
- Self-hosted Scheduler to trigger a job automatically.
- Console Interactive Mode to trigger a job manually.
I followed Andrew Lock's practice: Using Quartz.NET with ASP.NET Core and worker services and here is the quick guide to add a new Quartz Job and Trigger.
Create a new class (e.q. NewDailyJob
) to implement Quartz.IJob
in "src/Quartz.Net.Sample/Jobs/".
Open "src/Quartz.Net.Sample/Utils/Extensions/IServiceCollectionQuartzConfiguratorExtensions.cs", add your new Quartz.IJob
implementation in UseQuartzJobs
function like following:
quartz.AddJobAndTrigger<NewDailyJob>(configuration);
Open "src/Quartz.Net.Sample/appsettings.jon" and add the new Job's cron schedule:
{
"Quartz": {
"Job": {
"NewDailyJob": "0 0 0 ? * *"
}
}
}
To test the trigger, mock the SystemTime.UtcNow like this:
SystemTime.UtcNow = () => new DateTime(2023, 08, 31, 02, 0, 0);
I put the mocking options in "appsettings.Development.json" and IServiceCollectionQuartzConfiguratorExtensions.UseQuartzJobs()
shows how to mock the SystemTime.UtcNow
.
Run the command to publish the executable for Ubuntu:
dotnet publish -c release -r ubuntu.18.04-x64 --self-contained
The executable binary Quartz.Net.Sample
will be located in "src/Quartz.Net.Sample/bin/Release/net6.0/ubuntu.16.04-x64/publish/".
You can use Docker to run the executable.
docker run --name quartz-ubuntu -it ubuntu:18.04
docker start
cd "src/Quartz.Net.Sample/bin/Release/net6.0/ubuntu.16.04-x64/"
docker cp ./publish/ quartz-ubuntu:/tmp/
docker exec -it jb-ubuntu bash
While we're in the container, we can either run the executable by
/tmp/publish/Quartz.Net.Sample
: start the self-hosted Quartz scheduler./tmp/publish/Quartz.Net.Sample -i
: start the interactive mode.
Error: Process terminated. Couldn't find a valid ICU package installed on the system. Please install libicu using your package manager and try again
Solution:
Set the environment variable before running the executable. (See Runtime configuration options for globalization)
export DOTNET_SYSTEM_GLOBALIZATION_INVARIANT=1
Error: No usable version of libssl was found
Solution:
- First try
apt-get update
. - If you still get the error, see No usable version of libssl was found #4749.