Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use cron to create scheduled tasks in Golang

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article introduces how to use cron to create scheduled tasks in Golang. The content is very detailed. Interested friends can use it for reference. I hope it will be helpful to you.

1. The basic format of cron expression

The basic syntax of cron expressions implemented by Go is basically similar to that of crontab in linux. Cron (scheduled tasks), that is, according to the agreed time, regular execution of specific tasks (job).

The cron expression represents a collection of times, represented by fields separated by six spaces. Each subexpression describes a separate schedule detail

Does the field force the allowed values to allow special characters SecondsYES0-59,-* / MinutesYES0-59,-* / HoursYES0-23,-* / Day of monthYES1-31,-*? / L WMonthYES1-12 or JAN-DEC,-* / Day of weekYES1-7 or SUN-SAT,-*? / L # YearNOempty, 1970-2099,-* /

So, for example, an cron expression string "00 10? * MON", which means "10:00 every Monday".

If you are not clear about cron expressions, you can take a look at my previous article introducing quartz.net, Quartz.NET summary (II) CronTrigger and Cron expressions.

2. The package github.com/robfig/cron used

3. Example

Create the simplest cron task

Package main

Import ("github.com/robfig/cron"fmt")

Func main () {I: = 0c: = cron.New () spec: = "* / 5 *?" C.AddFunc (spec, func () {ionization + fmt.Println ("cron running:", I)}) c.Start ()

Select {}}

The output after startup is as follows:

D:\ Go_Path\ go\ src\ cronjob > go run singlejob.gocron running: 1cron running: 2cron running: 3cron running: 4cron running: 5

Multiple scheduled cron tasks

Package main

Import ("github.com/robfig/cron"fmt")

Type TestJob struct {}

Func (this TestJob) Run () {fmt.Println ("testJob1...")}

Type Test2Job struct {}

Func (this Test2Job) Run () {fmt.Println ("testJob2...")}

/ / start multiple tasks func main () {I: = 0 c: = cron.New ()

/ / AddFunc spec: = "* / 5 *?" C.AddFunc (spec, func () {ionization + fmt.Println ("cron running:", I)})

/ / AddJob method c.AddJob (spec, TestJob {}) c.AddJob (spec, Test2Job {})

/ / start the scheduled task c.Start ()

/ / scheduled tasks are closed, but tasks that are already in progress cannot be closed. Defer c.Stop ()

Select {}}

The output after startup is as follows:

D:\ Go_Path\ go\ src\ cronjob > go run multijob.gocron running: 1testJob1...testJob2...testJob1...cron running: 2testJob2...testJob1...testJob2...cron running: 3cron running: 4testJob1.. testJob2. On how to use cron in Golang to create scheduled tasks to share here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report