美文网首页
go 实现定时任务

go 实现定时任务

作者: 我爱张智容 | 来源:发表于2021-06-09 14:43 被阅读0次

代码:

package main

import (
    "fmt"
    "os"
    "os/signal"
    "syscall"
    "time"
)

func main() {

    stopAll := make(chan bool)
    go func() {
        quit := make(chan os.Signal, 1)
        signal.Notify(quit, syscall.SIGTERM, syscall.SIGINT, syscall.SIGQUIT, syscall.SIGKILL)
        <-quit
        stopAll <- true
        fmt.Println("Shutting down server...")
    }()

    if err := run(stopAll); err != nil {
        fmt.Println(err)
    }
}

func run(stop <-chan bool) error {
    tick := time.NewTicker(10 * time.Millisecond)
    for {
        select {
        case <-stop:
            return nil
        case <-tick.C:
            process()
        }
    }
}

func process() {

    fmt.Println("=========== process ================")

}

相关文章

网友评论

      本文标题:go 实现定时任务

      本文链接:https://www.haomeiwen.com/subject/dbeeeltx.html