Go--cron定时任务
<p>Cron是Go中用于设置定时任务的一个库,需要注意的是,Cron库分两个大版本,v1.2和v3.0,其功能和go get地址都是不同的,注意区分。</p><p>v1.2官方文档:https://pkg.go.dev/github.com/robfig/cron</p>
<p>v3官方文档:https://pkg.go.dev/github.com/robfig/cron/v3</p>
<h4>区别:</h4>
<ul>
<li>v1.2版本默认支持精确到秒的cron表达式</li>
<li>v3版本默认不再是支持秒级别定时任务,而是从分钟域开始执行,要细化到秒级别时,必须携带cron.WithSeconds()参数</li>
<li>v3支持定时任务的撤销功能</li>
</ul>
<p> </p>
<h4>下载:</h4>
<div class="cnblogs_code">
<pre>go <span style="color: rgba(0, 0, 255, 1)">get</span> -u github.com/robfig/<span style="color: rgba(0, 0, 0, 1)">cron
go get -u github.com/robfig/cron/v3<br></span></pre>
</div>
<p> </p>
<h4>用法介绍:</h4>
<p>用法与linux的crontab类似,不过多了个秒级别</p>
<div class="cnblogs_code">
<pre>* * * * * * //依次是 秒 分 时 日 月 周</pre>
</div>
<p>特殊字符:</p>
<ul>
<li>? 只能在day跟week中使用,标识未说明的值,用以解决day跟week的冲突,比如 <span class="cnblogs_code">* * * <span style="color: rgba(128, 0, 128, 1)">10</span> * ?</span> 表示每月10号触发,而换成 * 则表示不管星期几都可触发,与前者发生冲突</li>
<li>L 表示last,只能在day跟week中使用,每月最后一天或者每周最后一天(星期六)触发</li>
<li>W 只能在day中使用,表示最接近指定天的工作日(周一至周五),例:5W,每月第5天的工作日触发,若当天是星期六,则提前,若是星期天,则往后触发;不跨月份</li>
</ul>
<p> </p>
<h4>预定义模式(用于AddFunc()):</h4>
<table border="0" align="center">
<tbody>
<tr>
<td>模式</td>
<td>描述</td>
<td>等价于</td>
</tr>
<tr>
<td>@yearly (or @annually)</td>
<td>每年一次,一月一日午夜</td>
<td>0 0 0 1 1 *</td>
</tr>
<tr>
<td>@monthly</td>
<td>每月运行一次,每月第一天午夜</td>
<td>0 0 0 1 * *</td>
</tr>
<tr>
<td>@weekly</td>
<td>每周运行一次,周六/周日午夜</td>
<td>0 0 0 * * 0</td>
</tr>
<tr>
<td>@daily (or @midnight)</td>
<td>每天运行一次,当天午夜</td>
<td>0 0 0 * * *</td>
</tr>
<tr>
<td>@hourly</td>
<td>每小时的开始运行一次</td>
<td>0 0 * * * *</td>
</tr>
<tr>
<td>@every</td>
<td>每个持续的时间</td>
<td>例:c.AddFunc("@every 1s",func() {})</td>
</tr>
</tbody>
</table>
<p> </p>
<h4>设置时区:</h4>
<p>默认情况下,所有时间都是基于当前时区的,也可自定义</p>
<ul>
<li>在时间字符串前面添加一个CRON_TZ= + 具体时区
<ul>
<li>东京时区:Asia/Tokyo</li>
<li>纽约时区:America/New_York</li>
<li>上海时区:Asia/Shanghai</li>
<li>香港时区:Asia/Hong_Kong</li>
</ul>
</li>
<li>创建cron对象时增加一个时区选项cron.WithLocation(location),location为time.LoadLocation(zone)加载的时区对象,zone为具体的时区格式。或者调用已创建好的cron对象的SetLocation()方法设置时区</li>
</ul>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">func main() {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">直接配置时区</span>
nyc, _ := time.LoadLocation(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">America/New_York</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
c :</span>=<span style="color: rgba(0, 0, 0, 1)"> cron.New(cron.WithLocation(nyc)) //cron.New(cron.WithLocation(time.UTC))
c.AddFunc(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">0 6 * * ?</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, func() {
fmt.Println(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Every 6 o'clock at New York</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">参数里面配置时区</span>
c.AddFunc(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">CRON_TZ=Asia/Tokyo 0 6 * * ?</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, func() {
fmt.Println(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">Every 6 o'clock at Tokyo</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
})</span></pre>
</div>
<p> </p>
<h4>示例:</h4>
<p>v1.2</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">package main
import (
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">fmt</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">github.com/robfig/cron</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
)
func cronv1() {
fmt.Println(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">starting...</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建一个定时任务对象</span>
c :=<span style="color: rgba(0, 0, 0, 1)"> cron.New()
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">给对象增加定时任务</span>
c.AddFunc(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">*/5 * * * * *</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, func() {
fmt.Println(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">hello world 1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
})
c.AddFunc(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">*/8 * * * * *</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, func() {
fmt.Println(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">hello world 2</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">启动定时任务</span>
<span style="color: rgba(0, 0, 0, 1)"> c.Start()
}
func main() {
cronv1()
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查询语句,阻塞,让main函数不退出,保持程序运行</span>
<span style="color: rgba(0, 0, 255, 1)">select</span><span style="color: rgba(0, 0, 0, 1)"> {}
}</span></pre>
</div>
<p> </p>
<p>v3:秒级</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">package main
import (
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">fmt</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">github.com/robfig/cron/v3</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
)
func cronv3() {
fmt.Println(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">starting...</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建一个定时任务对象</span>
c :=<span style="color: rgba(0, 0, 0, 1)"> cron.New(cron.WithSeconds())
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">给对象增加定时任务</span>
c.AddFunc(<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">*/5 * * * * *</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, func() {
fmt.Println(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">hello world 1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
})
c.AddFunc(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">*/8 * * * * *</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, func() {
fmt.Println(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">hello world 2</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">启动定时任务</span>
<span style="color: rgba(0, 0, 0, 1)"> c.Start()
}
func main() {
cronv3()
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查询语句,阻塞,让main函数不退出,保持程序运行</span>
<span style="color: rgba(0, 0, 255, 1)">select</span><span style="color: rgba(0, 0, 0, 1)"> {}
}</span></pre>
</div>
<p> </p>
<p>v3:分钟级</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">package main
import (
</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">fmt</span><span style="color: rgba(128, 0, 0, 1)">"</span>
<span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">github.com/robfig/cron/v3</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">
)
func main() {
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">创建一个定时任务对象</span>
c :=<span style="color: rgba(0, 0, 0, 1)"> cron.New()
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">给对象增加定时任务</span>
c.AddFunc(<span style="color: rgba(128, 0, 0, 1)">"*/</span><span style="color: rgba(128, 0, 0, 1)">1 * * * *</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">, func() { <span style="color: rgba(51, 153, 102, 1)">//每隔1分钟输出"1"</span>
fmt.Println(</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">1</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)">)
})
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">启动定时任务</span>
<span style="color: rgba(0, 0, 0, 1)"> c.Start()
</span><span style="color: rgba(0, 128, 0, 1)">//</span><span style="color: rgba(0, 128, 0, 1)">查询语句,阻塞,让main函数不退出,保持程序运行</span>
<span style="color: rgba(0, 0, 255, 1)">select</span><span style="color: rgba(0, 0, 0, 1)"> {}
}</span></pre>
</div>
<p> </p><br><br>
来源:https://www.cnblogs.com/Xinenhui/p/16976562.html
頁:
[1]