第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會有你想問的

在 golang 的特定紀(jì)元時(shí)間啟動 cronjob

在 golang 的特定紀(jì)元時(shí)間啟動 cronjob

Go
阿晨1998 2023-01-03 14:14:11
我正在使用github.com/robfig/cron庫。我想以毫秒為單位在 epoc 時(shí)間運(yùn)行 cronjob 并每秒工作。cron 從000毫秒開始。我需要它在特定時(shí)間開始。例如,如果我采取以下內(nèi)容:c := cron.New() c.AddFunc("@every 1s", func() {        // Do Something     }) c.Start()并在 epoc 時(shí)間戳運(yùn)行它,1657713890300然后我希望函數(shù)在以下時(shí)間運(yùn)行:165771389130016577138923001657713893300.目前,cron 運(yùn)行于165771389100016577138920001657713893000.這可能嗎?
查看完整描述

1 回答

?
寶慕林4294392

TA貢獻(xiàn)2021條經(jīng)驗(yàn) 獲得超8個(gè)贊

當(dāng)您使用@every 1s庫時(shí),會創(chuàng)建一個(gè)ConstantDelaySchedule“循環(huán),以便下一次激活時(shí)間將在第二個(gè)”。

如果這不是您想要的,那么您可以創(chuàng)建自己的調(diào)度程序(游樂場):

package main


import (

    "fmt"

    "time"


    "github.com/robfig/cron/v3"

)


func main() {

    time.Sleep(300 * time.Millisecond) // So we don't start cron too near the second boundary

    c := cron.New()


    c.Schedule(CustomConstantDelaySchedule{time.Second}, cron.FuncJob(func() {

        fmt.Println(time.Now().UnixNano())

    }))

    c.Start()

    time.Sleep(time.Second * 5)

}


// CustomConstantDelaySchedule is a copy of the libraries ConstantDelaySchedule with the rounding removed

type CustomConstantDelaySchedule struct {

    Delay time.Duration

}


// Next returns the next time this should be run.

func (schedule CustomConstantDelaySchedule) Next(t time.Time) time.Time {

    return t.Add(schedule.Delay)

}

Follow up: 上面使用的是time.Timepassed to Nextwhich is time.Now()so will the time會隨著時(shí)間慢慢推進(jìn)。

解決這個(gè)問題是可能的(見下文 -游樂場),但這樣做會引入一些潛在的發(fā)行者(CustomConstantDelaySchedule不能重復(fù)使用,如果作業(yè)運(yùn)行時(shí)間太長,那么你仍然會以差異告終)。我建議您考慮放棄 cron 包,而只使用time.Ticker.

package main


import (

    "fmt"

    "time"


    "github.com/robfig/cron/v3"

)


func main() {

    time.Sleep(300 * time.Millisecond) // So we don't start cron too nead the second boundary

    c := cron.New()


    c.Schedule(CustomConstantDelaySchedule{Delay: time.Second}, cron.FuncJob(func() {

        fmt.Println(time.Now().UnixNano())

    }))

    c.Start()

    time.Sleep(time.Second * 5)

}


// CustomConstantDelaySchedule is a copy of the libraries ConstantDelaySchedule with the rounding removed

// Note that because this stored the last time it cannot be reused!

type CustomConstantDelaySchedule struct {

    Delay      time.Duration

    lastTarget time.Time

}


// Next returns the next time this should be run.

func (schedule CustomConstantDelaySchedule) Next(t time.Time) time.Time {

    if schedule.lastTarget.IsZero() {

        schedule.lastTarget = t.Add(schedule.Delay)

    } else {

        schedule.lastTarget = schedule.lastTarget.Add(schedule.Delay)

    }

    return schedule.lastTarget

}


查看完整回答
反對 回復(fù) 2023-01-03
  • 1 回答
  • 0 關(guān)注
  • 146 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號