如何创建调度程序(例如调度推文或 API 请求)

发布于 2024-08-19 00:12:04 字数 211 浏览 9 评论 0原文

我有一个日程项目表,它们可能被安排在同一时间。我想知道如何让它们在正确的时间执行:

我看到的问题是执行一个计划项目(如计划的 Twitter 帖子)需要一个 API 请求,该请求可能需要一两秒 - 可能更长。如果我按顺序执行它们+同时有太多计划项目,则它们的执行时间可能会晚于计划时间。

我将如何构建这个避免这些问题的“调度”系统?有什么提示、建议吗?

谢谢!

I have a table of schedule items, they may be scheduled for the same time. I'm wondering how to have them all execute at the correct time when:

The problem I see is that executing one scheduled item (like a scheduled twitter post) requires an API request which may take up to a second or two - probably longer. If I execute them sequentially + there are too many scheduled items at the same time, the time they get executed at could be after the scheduled time.

How would I go about building this "scheduling" system that avoids these problems? Any tips, advice?

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

终难愈 2024-08-26 00:12:04

我将使用 Windows 服务来完成此任务。然后应使用BackgroundWorker 进程异步安排每个项目。这将允许所有计划的进程快速异步启动,这样它们就不会发生冲突,也不依赖于启动前完成的前一个进程。

I would use a Windows service to accomplish this. Then each of the items should be scheduled asynchronously using the BackgroundWorker process. This would allow all of the scheduled processes to be launched rapidly asynchronously so they don't collide and aren't depending on the previous one finishing before kicking off.

客…行舟 2024-08-26 00:12:04

您可能需要考虑 Quartz.NET。在调度和任务执行方面为您提供了很大的灵活性。

You might want to consider Quartz.NET. Gives you much flexibility in terms of scheduling and task execution.

第几種人 2024-08-26 00:12:04

除非您采取措施利用所有 IO 操作都存在的异步 API,否则唯一的方法就是使用多个线程。请考虑 .net ThreadPool,因为当排队的工作项过多时,这会增加线程数量。这里会有一个限制,因为线程池旋转额外线程的速度相对较慢。在持续过载的情况下,您的系统会发出呻吟声。正如我所说,解决此问题的最佳方法是使用异步 IO。

Unless you take steps to take advantage of the asynchronous APIs that exist for all IO operations, your only approach is to use many threads. Consider the .net ThreadPool as this can increase the number of threads when too many work items are queued. There will be a limit here, as the ThreadPool spins up extra threads relatively slowly. Under sustained overload, your system will groan. Like I said, the best way to approach this is with asynchronous IO.

风和你 2024-08-26 00:12:04

当您希望任务运行时,您可以将它们放入线程中:

public abstract class MyTask {
    public abstract void DoWork();
}


// ...

public void SomeTaskStarter()
{
    MyTask task = SomeFactoryMethodToCreateATaskInstance();

    new Thread(new ThreadStart(task.DoWork)).Start();
}

MyTask 是一个抽象类,它代表要执行的任务,它定义了一个方法 DoWork() 来执行您想要的操作。 SomeFactoryMethodToCreateATaskInstance() 将构造一个任务的具体实例,您所需要做的就是编写 DoWork() 来完成您需要做的事情:

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

您肯定会想要某种任务完成操作。无论您做什么,任务完成例程都应该是线程安全的,并且如果您需要执行任何 UI 风格的工作,则可能应该通过 BeginInvoke()/EndInvoke() 调用。

SomeTaskStarter() 最好从 Windows 服务调用,并且很可能包含一个参数,其中包含有关应启动哪个任务等信息。

You can put the tasks in threads when you get 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 to do and it defines a method, DoWork() that will do what you want. SomeFactoryMethodToCreateATaskInstance() will construct a concrete instance of a task and all you need 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 most assuredly want some kind of action of task completion. Whatever you do, the task completion routine should be threadsafe, and should probably be called via BeginInvoke()/EndInvoke() if you need to do any UI-ish work.

SomeTaskStarter() is best called from a windows service, and will most likely contain an argument with information about what task should be started, etc.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文