An example of using a Saga to run a scheduled job with NServiceBus
This codebase assumes you have a named SQL Server instance called SqlExpress and have created a database called NsbSamplesSql.
The connection string looks like this:
Data Source=.\\SqlExpress;Database=NsbSamplesSql;Integrated Security=True
If you already have a version of SQLServer installed or wish to use a different database you will need to change the connection string in
the Program.cs
of both the Sender and NServiceBus.SagaScheduler projects.
NServiceBus.SagaScheduler is both a Send and Receive endpoint. Upon startup it will send a message to itself to start the scheduled job. Each time it receives this start scheduled job message it will make sure it hasn't been started previously to avoid mutiple versions of the same job from being scheduled.
Actions related to the start a scheduled job command will display as green text in the console window(s).
Sender is a send only endpoint. This endpoint allows start scheduled job commands to be fired manually for the purpose of demonstrating that the job will not be scheduled multiple times.
We will begin by starting up multiple instance of the scheduler to demonstrate that the scheduled job can be scaled out without worrying about multiple instances of the job being created. Then we will start the send-only Sender endpoint to manually fire off more start scheduled job commands to again demonstrate that only one instance of the job is running.
- Run the NServiceBus.SagaScheduler from Visual Studio 2019
- Wait for scheduler to begin running the job
- Next start a second instance of NServiceBus.SagaScheduler endpoint by right-clicking the NServiceBus.SagaScheduler project in the solution explorer and selecting Debug > Start New Instance
- Repeat step 3 if desired
- Wait and watch the command consoles compete to pick up the job and begin running it
- Next start an instance of the Sender project.
- From the Sender console window hit the enter button to manually fire off a start scheduled job command