From 8c92ad68666b5db83617aa47a6f3f1bcdd5e9d29 Mon Sep 17 00:00:00 2001 From: keepchen Date: Thu, 28 Dec 2023 17:35:44 +0800 Subject: [PATCH] =?UTF-8?q?1.schedule=E4=BB=A3=E7=A0=81=E8=B0=83=E6=95=B4,?= =?UTF-8?q?2.=E8=8E=B7=E5=8F=96logger=E7=BB=84=E4=BB=B6=E5=8A=A0=E5=85=A5m?= =?UTF-8?q?odules=E5=8F=82=E6=95=B0=E6=94=AF=E6=8C=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- sail/components.go | 4 ++-- schedule/schedule.go | 38 ++++++++++++++++++-------------------- 2 files changed, 20 insertions(+), 22 deletions(-) diff --git a/sail/components.go b/sail/components.go index 69bb514..42ee30a 100644 --- a/sail/components.go +++ b/sail/components.go @@ -56,8 +56,8 @@ func GetNats() *natsLib.Conn { } // GetLogger 获取日志实例 -func GetLogger() *zap.Logger { - return logger.GetLogger() +func GetLogger(modules ...string) *zap.Logger { + return logger.GetLogger(modules...) } // Response http响应组件 diff --git a/schedule/schedule.go b/schedule/schedule.go index a92209d..7457af1 100644 --- a/schedule/schedule.go +++ b/schedule/schedule.go @@ -15,14 +15,12 @@ type TaskJob struct { task func() interval time.Duration lockerKey string - cancelTaskChan chan struct{} withoutOverlapping bool + cancelFunc func() + cancelTaskChan chan struct{} } -var ( - cancelFunc func() - cronJob *cron.Cron -) +var cronJob *cron.Cron // Job 实例化任务 // @@ -39,7 +37,7 @@ func Job(name string, task func()) *TaskJob { cancelTaskChan: make(chan struct{}), } - cancelFunc = func() { + job.cancelFunc = func() { go func() { job.cancelTaskChan <- struct{}{} close(job.cancelTaskChan) @@ -75,7 +73,7 @@ func (j *TaskJob) Every(interval time.Duration) (cancel func()) { j.interval = interval j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel } @@ -85,7 +83,7 @@ func (j *TaskJob) EverySecond() (cancel func()) { j.interval = time.Second j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel } @@ -95,7 +93,7 @@ func (j *TaskJob) EveryFiveSeconds() (cancel func()) { j.interval = time.Second * 5 j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel } @@ -105,7 +103,7 @@ func (j *TaskJob) EveryTenSeconds() (cancel func()) { j.interval = time.Second * 10 j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel } @@ -115,7 +113,7 @@ func (j *TaskJob) EveryThirtySeconds() (cancel func()) { j.interval = time.Second * 30 j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel } @@ -125,7 +123,7 @@ func (j *TaskJob) EveryMinute() (cancel func()) { j.interval = time.Minute j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel } @@ -135,7 +133,7 @@ func (j *TaskJob) EveryFiveMinutes() (cancel func()) { j.interval = time.Minute * 5 j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel } @@ -145,7 +143,7 @@ func (j *TaskJob) EveryTenMinutes() (cancel func()) { j.interval = time.Minute * 10 j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel } @@ -155,7 +153,7 @@ func (j *TaskJob) EveryThirtyMinutes() (cancel func()) { j.interval = time.Minute * 30 j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel } @@ -165,7 +163,7 @@ func (j *TaskJob) Hourly() (cancel func()) { j.interval = time.Hour j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel } @@ -175,7 +173,7 @@ func (j *TaskJob) Daily() (cancel func()) { j.interval = time.Hour * 24 j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel } @@ -185,7 +183,7 @@ func (j *TaskJob) Weekly() (cancel func()) { j.interval = time.Hour * 24 * 7 j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel } @@ -195,7 +193,7 @@ func (j *TaskJob) Monthly() (cancel func()) { j.interval = time.Hour * 24 * 30 j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel } @@ -205,7 +203,7 @@ func (j *TaskJob) Yearly() (cancel func()) { j.interval = time.Hour * 24 * 365 j.run() - cancel = cancelFunc + cancel = j.cancelFunc return cancel }