Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is a good way to design and build a task scheduling system with lots of recurring tasks?

Imagine you're building something like a monitoring service, which has thousands of tasks that need to be executed in given time interval, independent of each other. This could be individual servers that need to be checked, or backups that need to be verified, or just anything at all that could be scheduled to run at a given interval.

You can't just schedule the tasks via cron though, because when a task is run it needs to determine when it's supposed to run the next time. For example:

  • schedule server uptime check every 1 minute
  • first time it's checked the server is down, schedule next check in 5 seconds
  • 5 seconds later the server is available again, check again in 5 seconds
  • 5 seconds later the server is still available, continue checking at 1 minute interval

A naive solution that came to mind is to simply have a worker that runs every second or so, checks all the pending jobs and executes the ones that need to be executed. But how would this work if the number of jobs is something like 100 000? It might take longer to check them all than it is the ticking interval of the worker, and the more tasks there will be, the higher the poll interval.

Is there a better way to design a system like this? Are there any hidden challenges in implementing this, or any algorithms that deal with this sort of a problem?

like image 864
Jakub Arnold Avatar asked Aug 04 '14 13:08

Jakub Arnold


People also ask

What is the best way to schedule tasks?

Estimate how long each task will take, and schedule your top priorities during times that you’re most alert and free of distractions. To call attention to high-priority tasks, draw asterisks, underline them, or highlight them. [6] For example, if you’re most productive in the morning, schedule high-priority projects before lunchtime.

What are the different project scheduling techniques?

There are several project scheduling techniques that can help you break down your project into small, manageable tasks. Some of these techniques include 1. Task lists This is the simplest scheduling technique and works for small projects without a lot of interdependencies.

Why do you need a project scheduling tool?

This tool makes sure you don’t leave out anything when devising your schedule. A workback schedule is another technique that allows you to break down the project scope and identify all the tasks, activities, deliverables and milestones. Regardless of your preferred scheduling methods, you need a tool designed to accommodate them equally.

How to organize your project schedules?

In the case of complex projects, it may be helpful to organize these tasks in the form of at, a chart visualizing project tasks and their sub-tasks and to stay organized at work. One challenge in this part of the project scheduling process is knowing how to divide activities.


1 Answers

Use a priority queue (with the priority based on the next execution time) to hold the tasks to execute. When you're done executing a task, you sleep until the time for the task at the front of the queue. When a task comes due, you remove and execute it, then (if its recurring) compute the next time it needs to run, and insert it back into the priority queue based on its next run time.

This way you have one sleep active at any given time. Insertions and removals have logarithmic complexity, so it remains efficient even if you have millions of tasks (e.g., inserting into a priority queue that has a million tasks should take about 20 comparisons in the worst case).

There is one point that can be a little tricky: if the execution thread is waiting until a particular time to execute the item at the head of the queue, and you insert a new item that goes at the head of the queue, ahead of the item that was previously there, you need to wake up the thread so it can re-adjust its sleep time for the item that's now at the head of the queue.

like image 141
Jerry Coffin Avatar answered Sep 19 '22 07:09

Jerry Coffin