From ea1a092fa28d9c93012b817d23c8d811353f58d7 Mon Sep 17 00:00:00 2001 From: wenjunhuang Date: Tue, 12 Jan 2021 17:07:05 +0800 Subject: [PATCH 1/2] Make dynamic register tasks available before schduler start --- .gitignore | 1 + scheduler.go | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..723ef36 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea \ No newline at end of file diff --git a/scheduler.go b/scheduler.go index 7b977a7..a60b7d3 100644 --- a/scheduler.go +++ b/scheduler.go @@ -55,6 +55,7 @@ func New() *CronSchduler { } type CronSchduler struct { + running bool tasks map[string]*JobModel ctx context.Context cancel context.CancelFunc @@ -104,7 +105,7 @@ func (c *CronSchduler) reset(srv string, model *JobModel, denyReplace, autoStart } c.tasks[srv] = model - if autoStart { + if autoStart && c.running { c.wg.Add(1) go c.tasks[srv].runLoop(c.wg) } @@ -223,6 +224,7 @@ func (c *CronSchduler) Start() { job.runLoop(c.wg) } + c.running = true }) } From 4c0e009db0f40e29ed43d2d05f6505ea592de5bd Mon Sep 17 00:00:00 2001 From: wenjunhuang Date: Tue, 12 Jan 2021 17:13:08 +0800 Subject: [PATCH 2/2] format code --- scheduler.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/scheduler.go b/scheduler.go index a60b7d3..56b3f75 100644 --- a/scheduler.go +++ b/scheduler.go @@ -56,9 +56,9 @@ func New() *CronSchduler { type CronSchduler struct { running bool - tasks map[string]*JobModel - ctx context.Context - cancel context.CancelFunc + tasks map[string]*JobModel + ctx context.Context + cancel context.CancelFunc wg *sync.WaitGroup once *sync.Once