Task scheduling is an important part of many software systems, literally assigning and running some scripts or programs that are usually long in time according to certain requirements. In the crawler management platform Crawlab[1], task scheduling is the core module, I believe many friends will be curious about how to write a task scheduling system. This article will teach you to write a very simple task scheduling system in Go.

Let’s start by clarifying our thinking and developing what the minimized task scheduler requires.

The whole process is as follows:

We create scheduled tasks through the API, and the executor periodically executes scripts according to the scheduled task standard.

First, let’s build a shelf. Create a main.go file under the project directory and enter the following. Among them, gin is a very popular Go language API engine.

Then add the api.go file and enter the following, noting that there is no code implementation here, just the addition of a placeholder area.

Then there’s the core of task scheduling, timing tasks. Here we use robfig/cron[2], the more popular scheduled task library in Go.

Now create the cron.go file and enter the following. where Cron is an instance generated by the Cron class in the robfig/cron library.

Now that the main timing task instance is created, you can add the core logic to the API placeholder area just now.

Again, api.go, which adds the core code.

In this code, we implemented most of the logic, only in the second parameter of Cron.AddFunc in AddJob, leaving the last part of the code that executes the task. Let’s do it next.

Now that we need to add the code logic for task execution, let’s create an exec.go file and enter the following. Here we use the shell management library os/exec, which is built into the Go language, and can execute any shell command.

Okay, now let’s put this part of the execution code logic into the previous placeholder area.

OK, that’s it! Now we can try running this minimalist task scheduler.

Type go run on the command line and the API engine starts.

Now open another command-line window, enter curl -X POST -d ‘{“cron”:”*****”,”exec”:”touch /tmp/hello.txt”}’ http://localhost:9092/jobs and you will get the following return result. Indicates that a corresponding scheduled task has been generated, the task ID is 1, and the /tmp/hello .txt is updated once every minute.

Enter curl http://localhost:9092/jobs in this command-line window.

This means that the next execution is after 1 minute.

Wait one minute, execute ls -l /tmp/hello.txt and get the following result.

In other words, the execution is successful and the work is complete!

This article develops a minimalist task scheduling system by simply combining several libraries in the Go language. Core libraries used:

The entire code sample repository is on GitHub: https://github.com/tikazyq/codao-code/tree/main/2022-10/go-task-scheduler

If you are interested in the author’s article, you can add the author WeChat tikazyq1 and indicate “the way of the code”, the author will pull you into the “way of the code” exchange group.

The English version of this article was published simultaneously on dev.to[5], technology sharing without borders, welcome the guidance of the big guys.

Crawlab: https://github.com/crawlab-team/crawlab

robfig/cron: https://github.com/robfig/cron

gin: https://github.com/gin-gonic/gin

robfig/cron: https://github.com/robfig/cron

dev.to: https://dev.to/tikazyq/golang-in-action-how-to-quickly-implement-a-minimal-task-scheduling-system-fel