How to create a scheduler (e.g. schedule tweets or api request)

I have a table with a schedule, they can be scheduled at the same time. I wonder how they all execute at the right time, when:

The problem I see is that the execution of one scheduled item (like a scheduled Twitter message) requires an API request, which can take up to a second or two - possibly longer. If I execute them sequentially + too many scheduled items at the same time, the time at which they will be executed may be after the scheduled time.

How do I start building this "planning" system that will avoid these problems? Any tips, tips?

Thanks!

+4
source share
4 answers

I would use a windows service for this. Then, each of the elements should be scheduled asynchronously using the BackgroundWorker process. This will allow you to start all scheduled processes asynchronously so that they do not collide with each other and are not dependent on the previous completion before starting work.

+4
source

You might want to consider Quartz.NET . Gives you great flexibility in planning and completing tasks.

+3
source

Unless you take steps to use the asynchronous APIs that exist for all I / O, your only approach is to use many threads. Consider .net ThreadPool, as this can increase the number of threads when too many work items are queued. There will be a limit, as ThreadPool will rotate additional threads relatively slowly. With steady overload, your system will moan. As I said, the best way to get closer to this is with asynchronous IO.

+1
source

You can put tasks in threads whenever you want them to run:

public abstract class MyTask { public abstract void DoWork(); } // ... public void SomeTaskStarter() { MyTask task = SomeFactoryMethodToCreateATaskInstance(); new Thread(new ThreadStart(task.DoWork)).Start(); } 

MyTask is an abstract class that represents a task, and defines a DoWork () method that will do what you want. SomeFactoryMethodToCreateATaskInstance () will create a specific instance of the task, and all you have to do is write DoWork () to do what you need to do:

 public class Twitterer : MyTask { private string _tweet; public Twitterer(string tweet) { _tweet = tweet; } public override DoWork() { TwitterApi api = new TwitterApi(); // whatever api.PostTweet(tweet); } } 

You will undoubtedly want to take some action to complete the task. No matter what you do, the task completion routine should be thread safe and probably should be called via BeginInvoke () / EndInvoke () if you need to do any UI-ish work.

SomeTaskStarter () is best called from a Windows service and most likely contains an argument with information about which task should be started, etc.

0
source

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


All Articles