-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
zionjxyu
committed
May 11, 2022
1 parent
3a5ab47
commit 2de772d
Showing
6 changed files
with
80 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,3 +12,7 @@ const ( | |
TrueFlag = 1 // 真值 | ||
FalseFlag = 0 // 假值 | ||
) | ||
|
||
const ( | ||
MySQLDrive = "mysql" // mysql驱动 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
package system | ||
|
||
import ( | ||
"log" | ||
"runtime" | ||
"time" | ||
|
||
"github.com/robfig/cron" | ||
) | ||
|
||
// StartTask 封装cron包,启动定时任务 6个字段 [second minute hour day month weekday] | ||
// 如 "0 */5 * * * ?" 每隔5分钟 | ||
func StartTask(spec string, cmd func()) { | ||
f := func() { | ||
defer func() { | ||
if e := recover(); e != nil { | ||
buf := make([]byte, 16*1024*1024) | ||
buf = buf[:runtime.Stack(buf, false)] | ||
log.Printf("[PANIC]%v\n%s\n", e, buf) | ||
} | ||
}() | ||
log.Printf("start cron task") | ||
cmd() | ||
log.Printf("end cron task") | ||
} | ||
|
||
c := cron.New() | ||
e := c.AddFunc(spec, f) | ||
if e != nil { | ||
panic(e) | ||
} | ||
|
||
c.Start() | ||
} | ||
|
||
// StartTaskAtOnce 启动时先执行一次,然后调用 StartTask | ||
func StartTaskAtOnce(spec string, timeout time.Duration, cmd func()) { | ||
timeStart := time.Now().UnixNano() | ||
defer func() { | ||
if e := recover(); e != nil { | ||
buf := make([]byte, 16*1024*1024) | ||
buf = buf[:runtime.Stack(buf, false)] | ||
log.Printf("[PANIC]%v\n%s", e, buf) | ||
} | ||
if time.Now().UnixNano() > timeout.Nanoseconds()+timeStart { // 超时 | ||
log.Printf("process exceed timeLimit %v", timeout) | ||
} | ||
}() | ||
|
||
log.Printf("start cron task") | ||
cmd() | ||
log.Printf("end cron task") | ||
StartTask(spec, cmd) | ||
} |