为什么 golang CronJob 不能在 goroutine 中 运行?

Why golang CronJob cannot run within goroutine?

我每天都使用 CronJob 来启动我的任务,我的任务有几个子任务,我打算使用 goroutine 来 运行。然而,事情并不顺利。

文件框架

|-gpool
|  -pool.go
|-main.go

main.go

import (
        "code.byted.org/i18n_web/content_import_tool_cronJob/gpool"
        "fmt"
        "github.com/robfig/cron/v3"
        "log"
        "os"
        "runtime"
        "time"
    )
    
    func SeedJob(it string, pool *gpool.Pool){
        fmt.Println("Name item: ", it)
        println(runtime.NumGoroutine())
        pool.Done()
    }
    
    type delayJob struct {
        PagePatternNameList []string
    }
    
    func (j *delayJob) GetPagePatternNameList() {
        //j.PagePatternNameList = dal.GetPagePatternName()
        j.PagePatternNameList = []string{"atama_posts","cchan_posts", "cookdoor_posts", "cookpad_posts",
            "cookpad_recipe_seed", "kurashiru_posts", "lips_all_posts", "lips_product", "lips_product_sku_seed",
            "lips_rank", "press_posts", "voce_all_posts", "zozo_posts_women"}
    }
    
    func (j *delayJob)Run(){
        log.Println("delay Job RUN")
        //time.Sleep(2 * time.Second)
        // startSeedJob
        pool := gpool.New(10)
        println(runtime.NumGoroutine())
        for _, it := range j.PagePatternNameList {
            pool.Add(1)
            go SeedJob(it, pool)
        }
        pool.Wait()
        println(runtime.NumGoroutine())
    }
    
    func main() {
        c := cron.New(
            cron.WithLogger(
                cron.VerbosePrintfLogger(log.New(os.Stdout, "cron: ", log.LstdFlags))))
    
        _, err := c.AddJob("CRON_TZ=America/New_York @every 2m", cron.NewChain(cron.DelayIfStillRunning(cron.DefaultLogger)).Then(&delayJob{}))
        if err != nil {
            fmt.Println("Cron Job err!")
            return
        }
        fmt.Println("it started")
        c.Start()
    
        defer c.Stop()
    
        time.Sleep(time.Second * 5)
    
    }

pool.go

package gpool
    
    import (
        "sync"
    )
    
    type Pool struct {
        queue chan int
        wg    *sync.WaitGroup
    }
    
    func New(size int) *Pool {
        if size <= 0 {
            size = 1
        }
        return &Pool{
            queue: make(chan int, size),
            wg:    &sync.WaitGroup{},
        }
    }
    
    func (p *Pool) Add(delta int) {
        for i := 0; i < delta; i++ {
            p.queue <- 1
        }
        for i := 0; i > delta; i-- {
            <-p.queue
        }
        p.wg.Add(delta)
    }
    
    func (p *Pool) Done() {
        <-p.queue
        p.wg.Done()
    }
    
    func (p *Pool) Wait() {
        p.wg.Wait()
    }

当我 运行 我的代码时,它没有打印出我期望的任何信息,只是

it started
cron: 2021/06/10 13:41:21 start
cron: 2021/06/10 13:41:21 schedule, now=2021-06-10T13:41:21+08:00, entry=1, next=2021-06-10T13:43:21+08:00
Exiting.

Debugger finished with the exit code 0

似乎没有触发错误,但运行 不正确。我该如何解决这个问题?

它没有 运行,因为您将作业安排为每 2 分钟 运行,然后仅等待 5 秒,然后您从 main() 返回,这导致您的程序退出。

如果您想在后台启动此 cron,您必须让程序 运行ning 保持足够长的时间以使其 运行 并完成,在此示例中至少超过 2 分钟。

如果您在 main() 中无事可做,那么您可以考虑使用 c.Run() 而不是 c.Start()