在计划函数中获取当前cronjob任务的计划时间

问题描述 投票:0回答:2

我想在计划函数中访问当前任务的计划时间。

我已经尝试过 https://go.dev/play/:

package main

import (
    "fmt"
    "time"

    "github.com/go-co-op/gocron/v2"
)

func main() {
    s, _ := gocron.NewScheduler()
    defer func() {
        _ = s.Shutdown()
    }()

    var j gocron.Job
    j, _ = s.NewJob(
        gocron.DurationJob(
            time.Second,
        ),
        gocron.NewTask(
            func() {
                fmt.Println(j.LastRun())
            },
        ),
    )
    s.Start()
    select {}
}

但是,打印输出是

timeout running program
0001-01-01 00:00:00 +0000 UTC gocron: job not found
0001-01-01 00:00:00 +0000 UTC gocron: job not found
0001-01-01 00:00:00 +0000 UTC gocron: job not found
0001-01-01 00:00:00 +0000 UTC gocron: job not found
...

此外,我不确定这是否是获取当前运行的计划时间的正确方法。

go cron callback
2个回答
1
投票

这似乎是一个竞争条件。下面的代码表明它访问内部的

j
是不安全的:

package main

import (
    "log"
    "time"

    "github.com/go-co-op/gocron/v2"
)

func main() {
    s, _ := gocron.NewScheduler()
    defer func() {
        _ = s.Shutdown()
    }()

    var j gocron.Job
    j, _ = s.NewJob(
        gocron.DurationJob(
            time.Second,
        ),
        gocron.NewTask(
            func() {
                log.Printf("%s doing stuff\n", j.Name())
                if t, err := j.LastRun(); err == nil {

                    log.Printf("Inside task last run: %s", t)
                } else {
                    log.Printf("determining time inside: %s", err)
                }
            },
        ),
    )
    s.Start()
    timeout := time.After(10 * time.Second)
    for {
        select {
        case <-time.After(2 * time.Second):
            t, _ := j.LastRun()
            log.Printf("Last run: %s", t)
        case <-timeout:
            log.Println("Shutting down...")
            s.Shutdown()
            log.Println("Shut down. Exiting.")
            return
        }
    }
}

在操场上跑步

日志:

2009/11/10 23:00:01 main.main.func2 doing stuff
2009/11/10 23:00:01 Inside task last run: 2009-11-10 23:00:01 +0000 UTC
2009/11/10 23:00:02 main.main.func2 doing stuff
2009/11/10 23:00:02 Inside task last run: 2009-11-10 23:00:02 +0000 UTC
2009/11/10 23:00:02 Last run: 2009-11-10 23:00:02 +0000 UTC
2009/11/10 23:00:03 main.main.func2 doing stuff
2009/11/10 23:00:03 Inside task last run: 2009-11-10 23:00:03 +0000 UTC
2009/11/10 23:00:04 Last run: 2009-11-10 23:00:03 +0000 UTC
2009/11/10 23:00:04 main.main.func2 doing stuff
2009/11/10 23:00:04 determining time inside: gocron: job not found
2009/11/10 23:00:05 main.main.func2 doing stuff
2009/11/10 23:00:05 determining time inside: gocron: job not found
2009/11/10 23:00:06 Last run: 2009-11-10 23:00:05 +0000 UTC
2009/11/10 23:00:06 main.main.func2 doing stuff
2009/11/10 23:00:06 Inside task last run: 2009-11-10 23:00:06 +0000 UTC
2009/11/10 23:00:07 main.main.func2 doing stuff
2009/11/10 23:00:07 Inside task last run: 2009-11-10 23:00:07 +0000 UTC
2009/11/10 23:00:08 Last run: 2009-11-10 23:00:07 +0000 UTC
2009/11/10 23:00:08 main.main.func2 doing stuff
2009/11/10 23:00:08 Inside task last run: 2009-11-10 23:00:08 +0000 UTC
2009/11/10 23:00:09 main.main.func2 doing stuff
2009/11/10 23:00:09 determining time inside: gocron: job not found
2009/11/10 23:00:10 Shutting down...
2009/11/10 23:00:10 Shut down. Exiting.

0
投票

此问题已在 2024 年 2 月以来的版本中得到修复。如果您仍有问题,请在存储库上提出问题!

© www.soinside.com 2019 - 2024. All rights reserved.