How can I schedule a task to run each day using NServiceBus
Asked Answered
V

2

5

Is there a elegant way of scheduling tasks using NServiceBus. There is one way I found while searching the net. Does NServiceBus give internal APIs for scheduling.

Velites answered 19/7, 2011 at 20:35 Comment(5)
We use that method and it works well. What kind of processing are you doing? You may require a saga instead.Adduction
I am trying to use the same method. However I am not able to figure out how to get a reference of the Bus in the implementation of the Scheduler.Velites
Funny I had the same issue. I ended up asking the container for an instance of the Bus explicitly: IBus bus = Configure.Instance.Builder.Build<IBus>();. If this answers the question then I'll edit and post as the answer.Adduction
Well actually I needed other ideas for scheduling using NServiceBus...this was just a offshoot question. I am still figuring out the use of saga. :) Appreciate the tip on the getting the Bus Instance thanks :)Velites
To give you ideas we have batch jobs(SSIS pkgs) that drop messages onto the Bus at certain times. We use the Schema Generator tool to create the correct format in the package and send it off.Adduction
K
9

NServiceBus now has this built in

From here http://docs.particular.net/nservicebus/scheduling-with-nservicebus

public class ScheduleStartUpTasks : IWantToRunWhenTheBusStarts
{
    public void Run()
    {
        Schedule.Every(TimeSpan.FromMinutes(5)).Action(() =>
            Console.WriteLine("Another 5 minutes have elapsed."));

        Schedule.Every(TimeSpan.FromMinutes(3)).Action(
                            "MyTaskName",() =>
                            { 
                                Console.WriteLine("This will be logged under MyTaskName’.");
                            });
    }
}

Note the caveat

When not to use it. You can look at a scheduled task as a simple never ending saga. But as soon as your task starts to get some logic (if-/switch-statements) you should consider moving to a saga.

Kagu answered 20/12, 2012 at 3:29 Comment(5)
This worked for me... for a while anyway. Then one day it failed in production, the scheduler was down for 2 days even though the rest of the bus continued working. It didn't log any NSB error messages. So now I'm looking at using Quartz.NET instead. Apparently it's a known issue... https://mcmap.net/q/2029275/-nservicebus-scheduler-stops-executing-job/61697Afield
@Afield a known issue that has been fixed github.com/Particular/NServiceBus/issues/2133 . but Quartz.NET is a perfectly valid alternativeKagu
@Kagu I saw this had been fixed in July 2014, we are using NSB version 4.7.5 which is dated November 2014 so I assumed it contains this fix. If the fix is only in the 5.x branch then we are still out of luck because our license is only for 4.x :( It's a shame because I liked the idea of the Schedule class and didn't really want to reference another package just to schedule the sending of a command every hour.Afield
@Afield AFAIK that change fix was backported to every minor version it was found in. have a look at the bottom of github.com/Particular/NServiceBus/issues/2133 and u can see all the associated commits. so yes you should have that fix if u are using 4.7.5.Kagu
@Afield BTW what is blocking you from upgrading to v5? if u want to speak to an engineer about it feel free to contact me on [email protected]Kagu
W
3

Note: This answer was valid for NServiceBus Version 2.0, but is correct no longer. Version 3 has this functionality. Go read Simon's answer, it is valid for Version 3!

NServiceBus does not have a built-in scheduling system. It is (at a very simple level) a message processor.

You can create a class that implements IWantToRunAtStartup (Run and Stop methods) and from there, create a timer or do whatever logic you need to do to drop messages onto the bus at certain times.

Other people have used Quartz.NET together with NServiceBus to get more granular scheduling functionality.

Wheen answered 21/7, 2011 at 16:33 Comment(3)
I used Quartz for scheduling other services until we started using the NServiceBus for messaging. This is first time I got the need to schedule stuff after our Quartz scheduler went defunct.Velites
I used Quartz to trigger a job every hour and the job just published an event... The service utilized the Run & Stop methods in IWantToRunAtStartup.. Here is a good start stuartcullinan.blogspot.com/2010/12/… , btw, check out the NServiceBusContribHanging
Simon, thanks for reminding me about this! Edited my answer and upvoted yours.Wheen

© 2022 - 2024 — McMap. All rights reserved.