Topshelf - manipulators

As a rule, with services, the task that you want to complete is repeated, perhaps in a cycle or, possibly, in a trigger or, possibly, in another.

I use Topshelf to do the second task for me, in particular, I use the Shelf'ing functionality.

The problem I am facing is how to handle the task loop.

When you load a service into Topshelf, you pass it a class (in this case, ScheduleQueueService ) and indicate that it is its Start method and Stop method:

Example:

  public class QueueBootstrapper : Bootstrapper<ScheduledQueueService> { public void InitializeHostedService(IServiceConfigurator<ScheduledQueueService> cfg) { cfg.HowToBuildService(n => new ScheduledQueueService()); cfg.SetServiceName("ScheduledQueueHandler"); cfg.WhenStarted(s => s.StartService()); cfg.WhenStopped(s => s.StopService()); } } 

But in my StartService() method, I use a while loop to repeat the task that I'm starting, but when I try to stop the service through Windows services, it does not stop, and I suspect it because the StartService() method never ended when it was originally called.

Example:

  public class ScheduledQueueService { bool QueueRunning; public ScheduledQueueService() { QueueRunning = false; } public void StartService() { QueueRunning = true; while(QueueRunning){ //do some work } } public void StopService() { QueueRunning = false; } } 

What is the best way to do this?

  • I looked at using the .NET System.Threading.Tasks to start work, and then possibly closing the thread in StopService ()

  • Perhaps using Quartz to repeat the task and then delete it.

Thoughts?

+6
source share
3 answers

Typically, how would I handle this, there is a Timer event that fires a few seconds after calling StartService() . At the end of the event, I would check the stop flag (set to StopService() ), if the flag (for example, your QueueRunning ) does not exist, then I will register one event on the timer so that I repeat in a few moments.

We do something very similar in Topshelf when polling the file system: https://github.com/Topshelf/Topshelf/blob/v2_master/src/Topshelf/FileSystem/PollingFileSystemEventProducer.cs#L80

Now, when the type of internal scheduler is used instead of the Timer object, but in general it is one and the same. fiber is basically this thread to handle the event.

If you have questions about the future, you can also join the Topshelf mailing list. We try to be very responsive. http://groups.google.com/group/topshelf-discuss

+3
source

I worked on some similar code today, I accidentally stumbled upon fooobar.com/questions/36444 / ... and it worked like a charm for me.

+1
source

I don’t know about Topshelf specifically, but when writing a standard Windows service, you want start and stop events to be executed as quickly as possible. If the initial thread takes too much time, it is assumed that it did not start, for example.

To get around this, I usually use System.Timers.Timer. This allows you to call the start method only once with a very short interval (therefore, it starts almost immediately). This does most of the work.

In your case, this may be your method, which loops. Then at the beginning of each cycle we check the global shutdown variable - if its true, you exit the cycle, and then the program may stop.

You may need a little more (or maybe even less) complexity than this, depending on where exactly the error is, but the general principle should be good, I hope.

Once again, although I will refuse that this knowledge is not based on the upper layer, but on the development of a common jsut service.

0
source

Source: https://habr.com/ru/post/904225/


All Articles