Skip to content

Commit

Permalink
feat: 封装定时认为类
Browse files Browse the repository at this point in the history
  • Loading branch information
zionjxyu committed May 11, 2022
1 parent 3a5ab47 commit 2de772d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 1 deletion.
4 changes: 3 additions & 1 deletion client/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"fmt"
"log"

"github.com/shootz-developer/gtool/constant"

_ "github.com/go-sql-driver/mysql"
)

Expand All @@ -21,7 +23,7 @@ type MysqlProxy struct {
func NewMysqlClient(proxy MysqlProxy) (*sql.DB, error) {
sourceName := fmt.Sprintf("%s:%s@tcp(%s:%s)/%s", proxy.Account, proxy.Password,
proxy.IP, proxy.Port, proxy.DBName)
db, err := sql.Open("mysql", sourceName)
db, err := sql.Open(constant.MySQLDrive, sourceName)
if err != nil {
log.Fatalf("New Mysql Client Error %+v", err)
return nil, err
Expand Down
4 changes: 4 additions & 0 deletions constant/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,7 @@ const (
TrueFlag = 1 // 真值
FalseFlag = 0 // 假值
)

const (
MySQLDrive = "mysql" // mysql驱动
)
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,7 @@ require (
github.com/olivere/elastic/v7 v7.0.32
github.com/onsi/ginkgo v1.16.5 // indirect
github.com/onsi/gomega v1.19.0 // indirect
github.com/robfig/cron v1.2.0
go.etcd.io/etcd/client/v3 v3.5.3
golang.org/x/text v0.3.7
)
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ github.com/prometheus/procfs v0.0.0-20181005140218-185b4288413d/go.mod h1:c3At6R
github.com/prometheus/procfs v0.0.2/go.mod h1:TjEm7ze935MbeOT/UhFTIMYKhuLP4wbCsTZCD3I8kEA=
github.com/prometheus/procfs v0.1.3/go.mod h1:lV6e/gmhEcM9IjHGsFOCxxuZ+z1YqCvr4OA4YeYWdaU=
github.com/prometheus/procfs v0.6.0/go.mod h1:cz+aTbrPOrUb4q7XlbU9ygM+/jj0fzG6c1xBZuNvfVA=
github.com/robfig/cron v1.2.0 h1:ZjScXvvxeQ63Dbyxy76Fj3AT3Ut0aKsyd2/tl3DTMuQ=
github.com/robfig/cron v1.2.0/go.mod h1:JGuDeoQd7Z6yL4zQhZ3OPEVHB7fL6Ka6skscFHfmt2k=
github.com/rogpeppe/fastuuid v1.2.0/go.mod h1:jVj6XXZzXRy/MSR5jhDC/2q6DgLz+nrA6LYCDYWNEvQ=
github.com/sirupsen/logrus v1.2.0/go.mod h1:LxeOpSwHxABJmUn/MG1IvRgCAasNZTLOkJPxbbu5VWo=
github.com/sirupsen/logrus v1.4.2/go.mod h1:tLMulIdttU9McNUspp0xgXVQah82FyeX6MwdIuYE2rE=
Expand Down
15 changes: 15 additions & 0 deletions strings/string.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ package strings

import (
"crypto/md5"
crand "crypto/rand"
"database/sql"
"encoding/base64"
"encoding/hex"
"fmt"
"math/big"
"math/rand"
"strings"
"time"
Expand Down Expand Up @@ -68,3 +71,15 @@ func GetRandomString(n int) string {
}
return string(b)
}

// GetUniqueID 生成UniqueID方法
func GetUniqueID() string {
// 当前毫秒时间戳
timestamp := time.Now().UnixNano() / 1000000
s1, _ := crand.Int(crand.Reader, big.NewInt(10))
s2, _ := crand.Int(crand.Reader, big.NewInt(10))

seqNo := timestamp * (s1.Int64() + 1) * (s2.Int64() + 1)
uniqueID := fmt.Sprintf("%d", seqNo)
return uniqueID
}
54 changes: 54 additions & 0 deletions system/system.go
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)
}

0 comments on commit 2de772d

Please sign in to comment.