docs: format code annotation

This commit is contained in:
rfyiamcool
2022-02-18 15:44:11 +08:00
committed by fengyun.rui
parent 3de8a6eb5b
commit eeb0ee00ee
2 changed files with 26 additions and 6 deletions

View File

@@ -2,6 +2,7 @@ package main
import ( import (
"log" "log"
"time"
"github.com/rfyiamcool/cronlib" "github.com/rfyiamcool/cronlib"
) )
@@ -12,15 +13,23 @@ var (
func main() { func main() {
handleClean() handleClean()
go start()
// dynamic add time.AfterFunc(time.Duration(2*time.Second), func() {
handleBackup() // dynamic add
handleBackup()
})
select {} time.AfterFunc(
} 12*time.Second,
func() {
log.Println("stop clean")
cron.StopService("clean")
log.Println("stop backup")
cron.StopService("backup")
},
)
func start() {
cron.Start() cron.Start()
cron.Wait() cron.Wait()
} }

View File

@@ -43,6 +43,7 @@ func SetPanicCaller(p panicType) {
panicCaller = p panicCaller = p
} }
// New - create CronSchduler
func New() *CronSchduler { func New() *CronSchduler {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
return &CronSchduler{ return &CronSchduler{
@@ -54,6 +55,7 @@ func New() *CronSchduler {
} }
} }
// CronSchduler
type CronSchduler struct { type CronSchduler struct {
tasks map[string]*JobModel tasks map[string]*JobModel
ctx context.Context ctx context.Context
@@ -138,6 +140,7 @@ func (c *CronSchduler) Stop() {
job.kill() job.kill()
delete(c.tasks, srv) delete(c.tasks, srv)
} }
c.cancel()
} }
// StopService - stop job by serviceName // StopService - stop job by serviceName
@@ -228,10 +231,18 @@ func (c *CronSchduler) Start() {
}) })
} }
// Wait - if all jobs is exited, return.
func (c *CronSchduler) Wait() { func (c *CronSchduler) Wait() {
c.wg.Wait() c.wg.Wait()
} }
// WaitStop - when stop cronlib controller, return.
func (c *CronSchduler) WaitStop() {
select {
case <-c.ctx.Done():
}
}
func (c *CronSchduler) GetServiceCron(srv string) (*JobModel, error) { func (c *CronSchduler) GetServiceCron(srv string) (*JobModel, error) {
c.RLock() c.RLock()
defer c.RUnlock() defer c.RUnlock()