From 44b804ec7d16e62cb99ca5eca9e94cbcbb6afa2d Mon Sep 17 00:00:00 2001 From: John Date: Mon, 5 Nov 2018 10:29:58 +0800 Subject: [PATCH] =?UTF-8?q?gcron=E5=A2=9E=E5=8A=A0DelayAdd=E6=96=B9?= =?UTF-8?q?=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- g/os/gcron/gcron.go | 5 +++++ g/os/gcron/gcron_cron.go | 12 +++++++++++- g/os/gtime/gtime.go | 4 ++-- geg/other/test.go | 25 ++++++------------------- 4 files changed, 24 insertions(+), 22 deletions(-) diff --git a/g/os/gcron/gcron.go b/g/os/gcron/gcron.go index ef61dfd2e..eb7555583 100644 --- a/g/os/gcron/gcron.go +++ b/g/os/gcron/gcron.go @@ -50,6 +50,11 @@ func Add(spec string, f func(), name ... string) error { return defaultCron.Add(spec, f, name...) } +// 延迟添加定时任务,delay参数单位为秒 +func DelayAdd(delay int, spec string, f func(), name ... string) { + defaultCron.DelayAdd(delay, spec, f, name...) +} + // 检索指定名称的定时任务 func Search(name string) *Entry { return defaultCron.Search(name) diff --git a/g/os/gcron/gcron_cron.go b/g/os/gcron/gcron_cron.go index 96c5ef175..321bf2906 100644 --- a/g/os/gcron/gcron_cron.go +++ b/g/os/gcron/gcron_cron.go @@ -14,6 +14,7 @@ import ( "gitee.com/johng/gf/third/github.com/robfig/cron" "reflect" "runtime" + "time" ) // 添加定时任务 @@ -39,7 +40,7 @@ func (c *Cron) Add(spec string, f func(), name ... string) error { } } else { if err := c.cron.AddFunc(spec, f); err == nil { - entry := &Entry{ + entry := &Entry { Spec : spec, Cmd : runtime.FuncForPC(reflect.ValueOf(f).Pointer()).Name(), Time : gtime.Now(), @@ -55,6 +56,15 @@ func (c *Cron) Add(spec string, f func(), name ... string) error { return nil } +// 延迟添加定时任务,delay参数单位为秒 +func (c *Cron) DelayAdd(delay int, spec string, f func(), name ... string) { + gtime.SetTimeout(time.Duration(delay)*time.Second, func() { + if err := c.Add(spec, f, name ...); err != nil { + panic(err) + } + }) +} + // 检索指定名称的定时任务 func (c *Cron) Search(name string) *Entry { entry, _ := c.searchEntry(name) diff --git a/g/os/gtime/gtime.go b/g/os/gtime/gtime.go index a6f9fa528..ee4d121d5 100644 --- a/g/os/gtime/gtime.go +++ b/g/os/gtime/gtime.go @@ -29,12 +29,12 @@ const ( // "2018/10/31 - 16:38:46" // "2018-02-09", // 日期连接符号支持'-'或者'/' - TIME_REAGEX_PATTERN1 = `(\d{2,4}[-/]\d{2}[-/]\d{2})[:\sT-]*(\d{0,2}:{0,1}\d{0,2}:{0,1}\d{0,2}){0,1}\.{0,1}(\d{0,9})([\sZ]*)([\+-]{0,1})([:\d]*)` + TIME_REAGEX_PATTERN1 = `(\d{2,4}[-/]\d{2}[-/]\d{2})[:\sT-]*(\d{0,2}:{0,1}\d{0,2}:{0,1}\d{0,2}){0,1}\.{0,1}(\d{0,9})([\sZ]{0,1})([\+-]{0,1})([:\d]*)` // 01-Nov-2018 11:50:28 // 01/Nov/2018 11:50:28 // 01/Nov/2018:11:50:28 // 01/Nov/18 11:50:28 - TIME_REAGEX_PATTERN2 = `(\d{1,2}[-/][A-Za-z]{3,}[-/]\d{2,4})[:\sT-]*(\d{0,2}:{0,1}\d{0,2}:{0,1}\d{0,2}){0,1}\.{0,1}(\d{0,9})([\sZ]*)([\+-]{0,1})([:\d]*)` + TIME_REAGEX_PATTERN2 = `(\d{1,2}[-/][A-Za-z]{3,}[-/]\d{2,4})[:\sT-]*(\d{0,2}:{0,1}\d{0,2}:{0,1}\d{0,2}){0,1}\.{0,1}(\d{0,9})([\sZ]{0,1})([\+-]{0,1})([:\d]*)` ) var ( diff --git a/geg/other/test.go b/geg/other/test.go index 7dab9d6fe..875b100a4 100644 --- a/geg/other/test.go +++ b/geg/other/test.go @@ -2,27 +2,14 @@ package main import ( "fmt" - "gitee.com/johng/gf/g/os/gfile" - "gitee.com/johng/gf/third/github.com/fsnotify/fsnotify" - "os" + "gitee.com/johng/gf/g/os/gtime" ) func main() { - if w, err := fsnotify.NewWatcher(); err != nil { - fmt.Println(err) - } else { - index := 0 - if array, err := gfile.ScanDir("/home/john", "*", true); err == nil { - for _, path := range array { - index++ - if err := w.Add(path); err != nil { - fmt.Println(err) - os.Exit(1) - } else { - fmt.Println(index) - } - } - } - } + s := ` +[INFO] 2018-11-04 12:48:01 2018-11-04 12:48:01 ["handle eventMedlinker\\Med3Svr\\App\\Events\\Task\\InviteUserVerifyEvent::__set_state(array(\n 'deviceNum' => '25a715ff8d4835197299d7dab067841e',\n 'userIdList' => \n array (\n 0 => '62506063',\n ),\n 'dataId' => '',\n 'eventTime' => NULL,\n))"] [med3-svr-6c8c9b9f4f-fl6g9] +` + t := gtime.ParseTimeFromContent(s) + fmt.Println(t.String()) } \ No newline at end of file