gcron updates

This commit is contained in:
John 2019-01-18 22:02:17 +08:00
parent 5fed6f5681
commit 7d103c4ee8
4 changed files with 33 additions and 21 deletions

View File

@ -9,6 +9,7 @@
package ghttp
import (
"gitee.com/johng/gf/g/util/gregex"
"time"
"bytes"
"strings"
@ -47,6 +48,14 @@ func (c *Client) SetHeader(key, value string) {
c.header[key] = value
}
// 通过字符串设置HTTP Header
func (c *Client) SetHeaderRaw(header string) {
for _, line := range strings.Split(strings.TrimSpace(header), "\n") {
array, _ := gregex.MatchString(`^([\w\-]+):\s*(.+)`, line)
c.header[array[1]] = array[2]
}
}
// 设置请求的URL前缀
func (c *Client) SetPrefix(prefix string) {
c.prefix = prefix

View File

@ -9,7 +9,10 @@
// 定时任务.
package gcron
import "math"
import (
"math"
"time"
)
const (
STATUS_READY = 0
@ -45,23 +48,23 @@ func AddTimes(pattern string, times int, job func(), name ... string) (*Entry, e
return defaultCron.AddTimes(pattern, times, job, name...)
}
// 延迟添加定时任务delay参数单位为秒
func DelayAdd(delay int, pattern string, job func(), name ... string) {
// 延迟添加定时任务
func DelayAdd(delay time.Duration, pattern string, job func(), name ... string) {
defaultCron.DelayAdd(delay, pattern, job, name...)
}
// 延迟添加单例定时任务delay参数单位为秒
func DelayAddSingleton(delay int, pattern string, job func(), name ... string) {
func DelayAddSingleton(delay time.Duration, pattern string, job func(), name ... string) {
defaultCron.DelayAddSingleton(delay, pattern, job, name...)
}
// 延迟添加只运行一次的定时任务delay参数单位为秒
func DelayAddOnce(delay int, pattern string, job func(), name ... string) {
func DelayAddOnce(delay time.Duration, pattern string, job func(), name ... string) {
defaultCron.DelayAddOnce(delay, pattern, job, name...)
}
// 延迟添加运行指定次数的定时任务delay参数单位为秒
func DelayAddTimes(delay int, pattern string, times int, job func(), name ... string) {
func DelayAddTimes(delay time.Duration, pattern string, times int, job func(), name ... string) {
defaultCron.DelayAddTimes(delay, pattern, times, job, name...)
}

View File

@ -92,36 +92,36 @@ func (c *Cron) AddTimes(pattern string, times int, job func(), name ... string)
}
}
// 延迟添加定时任务delay参数单位为秒
func (c *Cron) DelayAdd(delay int, pattern string, job func(), name ... string) {
gtimer.AddOnce(time.Duration(delay)*time.Second, func() {
// 延迟添加定时任务
func (c *Cron) DelayAdd(delay time.Duration, pattern string, job func(), name ... string) {
gtimer.AddOnce(delay, func() {
if _, err := c.Add(pattern, job, name ...); err != nil {
panic(err)
}
})
}
// 延迟添加单例定时任务delay参数单位为秒
func (c *Cron) DelayAddSingleton(delay int, pattern string, job func(), name ... string) {
gtimer.AddOnce(time.Duration(delay)*time.Second, func() {
// 延迟添加单例定时任务
func (c *Cron) DelayAddSingleton(delay time.Duration, pattern string, job func(), name ... string) {
gtimer.AddOnce(delay, func() {
if _, err := c.AddSingleton(pattern, job, name ...); err != nil {
panic(err)
}
})
}
// 延迟添加运行指定次数的定时任务delay参数单位为秒
func (c *Cron) DelayAddOnce(delay int, pattern string, job func(), name ... string) {
gtimer.AddOnce(time.Duration(delay)*time.Second, func() {
// 延迟添加运行指定次数的定时任务
func (c *Cron) DelayAddOnce(delay time.Duration, pattern string, job func(), name ... string) {
gtimer.AddOnce(delay, func() {
if _, err := c.AddOnce(pattern, job, name ...); err != nil {
panic(err)
}
})
}
// 延迟添加只运行一次的定时任务delay参数单位为秒
func (c *Cron) DelayAddTimes(delay int, pattern string, times int, job func(), name ... string) {
gtimer.AddOnce(time.Duration(delay)*time.Second, func() {
// 延迟添加只运行一次的定时任务
func (c *Cron) DelayAddTimes(delay time.Duration, pattern string, times int, job func(), name ... string) {
gtimer.AddOnce(delay, func() {
if _, err := c.AddTimes(pattern, times, job, name ...); err != nil {
panic(err)
}

View File

@ -122,14 +122,14 @@ func TestCron_DelayAdd(t *testing.T) {
gtest.Case(t, func() {
cron := gcron.New()
array := garray.New(0, 0)
cron.DelayAdd(1, "* * * * * *", func() {
cron.DelayAdd(500*time.Millisecond, "* * * * * *", func() {
array.Append(1)
})
gtest.Assert(cron.Size(), 0)
time.Sleep(1200*time.Millisecond)
time.Sleep(800*time.Millisecond)
gtest.Assert(array.Len(), 0)
gtest.Assert(cron.Size(), 1)
time.Sleep(1200*time.Millisecond)
time.Sleep(500*time.Millisecond)
gtest.Assert(array.Len(), 1)
gtest.Assert(cron.Size(), 1)
})