Basic tutorial for using Go timer cron

  • 2020-06-15 09:14:32
  • OfStack

What is cron

cron means to plan tasks, or to schedule tasks. I make an appointment with the system, and you run one task (job) every few minutes or minutes, and that's it.

preface

cron is a regular homework library developed by robfig. robfig always thinks earlier than others, which gives us a lot of things we need, and the appearance of revel is the same. Looking at the use of cron, it is still as simple and clear as ever. In the world of Go, some products still have distinct personal characteristics. Is that the so-called personal charm? !

In a word, the products developed by robfig are all ones with definite advance, relatively theoretical basis and very simple to use.

Here's how to use cron:


c := cron.New()
c.AddFunc("0 30 * * * *", func() { fmt.Println("Every hour on the half hour") })
c.AddFunc("@hourly", func() { fmt.Println("Every hour") })
c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty") })
c.Start()

First instantiate 1 cron object c, then call the AddFunc function of instance c, AddFunc function receives 1 timing expression and 1 normal function, finally start to execute the instance, timing job is good.

The first normal function we receive is something that we do business with, like we want to print Hello World! 1 line string, let's write a function like this:


func PrintSomeWord(){
fmt.Sprint("Hello World!")
}

Now that you have PrintSomeWord, and you want to print it once a second, cron comes in handy:


c := cron.New()
c.AddFunc("@every 1s",PrintSomeWord)
c.Start()

In this sense, ordinary functions are determined by our business, so let's leave aside for a moment, and the timing expressions are what we're going to learn. From the name of cron, we can easily associate cron timer of Linux system. Is there a definite relationship between them? According to the following usage, the answer is yes. If you look closely at their timing expressions, they are very similar, which makes it easier to use cron, which is one standard.

Let's look at the common expression for cron:


c.AddFunc("@every 1h30m", func() { fmt.Println("Every hour thirty") })
c.AddFunc("@daily", func() { fmt.Println("Every day") })
c.AddFunc("@hourly", func() { fmt.Println("Every hour") })

The timing expressions of these three lines of code respectively represent: 1 execution every hour and 30 minutes, 1 execution every day, and 1 execution every hour.

@every is very broad. Change the suffix to indicate different USES, such as seconds, minutes, hours and days. Look at the code:


@every 1s
@every 1m
@every 1h
@every 1d

How regular?

Further down the line, it may be that @every isn't simple enough, as in:


@every 1h
@every 1d
@every 30d

Every hour, every day, every month, no suffixes at all:


@hourly
@daily
@monthly

Of course, simplicity is good, but at the same time, there are also some cases where simplicity cannot be achieved, such as the above mentioned:


@every 1h30m

Is it ok not to use suffixes?

The most confusing one is probably the following expression, because looking at this expression is like looking at a bunch of regular expressions:


c.AddFunc("0 30 * * * *", func() { fmt.Println("Every hour on the half hour") })

What exactly does the timed expression 0 30 * * * * mean? I don't know what the meaning is. If you translate the English phrase Every hour on the half hour, it means "once every one and a half hours".

Well, seeing 0, 30, and 6, inference 1: is it seconds, days, months, and years? Seems to have eyebrows, I also want to know the specific use of this expression, only after the next serious study, 1 said.

Go timer the use of cron expressions

If you want to be more flexible with timing, it may involve using more complex 6-bit expressions, such as executing once every half hour:


0 30 * * * *

A six-bit expression looks a little confusing, like a regular expression 1, and we need to follow the steps to understand it;

Understand the meaning of the six scopes


func PrintSomeWord(){
fmt.Sprint("Hello World!")
}
0

The above expressions, both 6-bit placeholders, can be Numbers and characters, where characters are specific characters including: [* /, - ? ES102en-ES103en ES104en-ES105en]

Let's call it the 6 scopes, from left to right, meaning the second domain (1) the minute domain (2) the small time domain (3) the date domain (4) the month domain (5) the week domain (6)

The month field can use ES110en-DEC and the week field can use ES112en-ES113en, respectively, to represent the English abbreviation of January to December and the English abbreviation of Monday to Sunday. If you do not know the English abbreviation of the month and the week, it is recommended to use Numbers, because this requires memory and case-sensitive, and is prone to errors.

The following list is easy to understand:

From January to December: Jan Feb Apr Jun Jul Oct Nov Dec

Mon Tues Wed Thurs Sat Sun

The six scopes can also use specific characters:

Specific character asterisk (*)

An asterisk means that all reasonable values are matched and that any field can exist. For example, the location of the month field (5) USES the asterisk * to indicate that it is executed every month. Similarly, an asterisk is used in the position of week field (6) to indicate that it is performed every week; Other placeholders use it and so on.

Slant underscore for specific characters (/)

Diagonal lines are usually used for 1 range increment and can exist in any field. For example, in the position of the minute field (2), use 3-59/15 to represent a time range of 3 to 59 minutes per hour, and execute once every 15 minutes.

Specific character comma (,)

Commas are used to separate multiple points and can exist in any field. For example, MON,WED and FRI are used in the position of the week domain (6) to indicate that the three points of Monday, Monday, Wednesday and Friday meet the conditions.

Horizontal bar (-) for a particular character

The middle bar is usually used to define a scope and can exist in any domain. For example, the position of small time domain (3) USES 9-17 to represent the time range from the beginning of 9 to the end of 17.

Specific character question mark (?)

The question mark means that one point is uncertain. It can only be used in the position of the date field (4) and the week field (6). It means that the specified value is uncertain. If used in the date field (4), it cannot be used in the Week field (6). It and there is a difference between an asterisk, an asterisk matches all points, that is to say which one day a month, which can be every week, and a question mark, said a month of uncertainty which 1 day or 1 week not sure which one day, it is difficult to understand, need to say 1 to explain some concrete conditions, such as February 18th day, just right is the second day of 3 weeks, so if in the date field definition 18 (4), week (6) define the 2 this overlap defines the cron is not allowed.

conclusion


Related articles: