本文整理汇总了Golang中github.com/gorhill/cronexpr.MustParse函数的典型用法代码示例。如果您正苦于以下问题:Golang MustParse函数的具体用法?Golang MustParse怎么用?Golang MustParse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了MustParse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: StartChore
func StartChore(c *Chore) error {
Logger.Debug("Re-Starting: ", c.Name)
expr := cronexpr.MustParse(c.Sched)
if expr.Next(time.Now()).IsZero() {
Logger.Debug("invalid schedule", c.Sched)
c.State = fmt.Sprintf("NOT Scheduled (invalid Schedule: %s)", c.Sched)
} else {
Logger.Debug("valid Schedule: ", c.Sched)
c.Next = expr.Next(time.Now())
dur := c.Next.Sub(time.Now())
if dur > 0 {
Logger.Debug("valid duration: ", dur)
Logger.Debug("testing timer.. ")
if c.Timer == nil {
Logger.Debug("creating new timer ")
c.Timer = time.AfterFunc(dur, c.Trigger) // auto go-routine'd
} else {
Logger.Debug("pre-existing timer found, resetting to: ", dur)
c.Timer.Reset(dur) // auto go-routine'd
}
c.State = fmt.Sprintf("Scheduled: %s", c.Next.String())
} else {
Logger.Debug("invalid duration", dur)
c.State = fmt.Sprintf("Halted. (invalid duration: %s)", dur)
}
}
Logger.Debug("all set! Chore: ", c.Name, "scheduled at: ", c.Next)
return nil
}
示例2: ShouldSendNowGivenTime
func (c *Channel) ShouldSendNowGivenTime(currentTime time.Time) bool {
// We assume these are already sent
if c.TimeToNotify == "@immediately" {
return false
}
// the additional -1s is to make sure that the range always had 1 second.
// example: if you specify 01:00:00, the minute before and minute after will
// be 01:00:00 and 01:01:00. The next time computed will be an hour from now
// (in the hourly case). This means that there's a chance that this hour we
// never send anything. With an additional second it minimize this risk.
//
// Also, double sending is minimized as only 1 goroutine sends and we use a
// delivered flag in the db.
minuteBefore := currentTime.Add(time.Duration(-currentTime.Second()-1) * time.Second)
minuteAfter := minuteBefore.Add(time.Minute + time.Second)
expression := cronexpr.MustParse(c.TimeToNotify)
nextTime := expression.Next(minuteBefore)
if nextTime.IsZero() {
return false
}
// Operator overloading is so good.
// return (minuteBefore <= nextTime <= currentTime)
return (nextTime.After(minuteBefore) || nextTime.Equal(minuteBefore)) && (nextTime.Before(minuteAfter) || nextTime.Equal(minuteAfter))
}
示例3: scheduleLoop
func scheduleLoop() {
jobs := make([]*Job, 0, len(config.Schedule))
for exprStr, cmd := range config.Schedule {
expr := cronexpr.MustParse(exprStr)
jobs = append(jobs, &Job{
expr,
cmd,
expr.Next(time.Now()),
})
}
go func() {
for {
<-time.Tick(time.Second)
for _, j := range jobs {
if time.Now().Before(j.next) {
continue
}
log.Printf("Executing scheduled command '%s'...", j.cmd)
execCmd(j.cmd)
j.next = j.expr.Next(time.Now())
}
}
}()
}
示例4: TestZero
func TestZero(t *testing.T) {
from, _ := time.Parse("2006-01-02", "2013-08-31")
next := cronexpr.MustParse("* * * * * 1980").Next(from)
if next.IsZero() == false {
t.Error(`("* * * * * 1980").Next("2013-08-31").IsZero() returned 'false', expected 'true'`)
}
next = cronexpr.MustParse("* * * * * 2050").Next(from)
if next.IsZero() == true {
t.Error(`("* * * * * 2050").Next("2013-08-31").IsZero() returned 'true', expected 'false'`)
}
next = cronexpr.MustParse("* * * * * 2099").Next(time.Time{})
if next.IsZero() == false {
t.Error(`("* * * * * 2014").Next(time.Time{}).IsZero() returned 'true', expected 'false'`)
}
}
示例5: TestGenericFeedKilledNotificationSystem
func TestGenericFeedKilledNotificationSystem(t *testing.T) {
fun := func(ctx *FeedContext) {
ctx.N.Close() // easiest way to kill it
}
if err := runGenericFeedTest(t, fun, cronexpr.MustParse("@hourly"), 1*time.Second, 100000); err == nil {
t.Fatal("killing notifier should have reported back an error")
}
}
示例6: processCronRule
func processCronRule(rule Rule, stop chan struct{}, outCh chan messages.Message, cronRoom string) {
nextTime := cronexpr.MustParse(rule.When).Next(time.Now())
for {
select {
case <-stop:
return
default:
if nextTime.Format("2006-01-02 15:04") == time.Now().Format("2006-01-02 15:04") {
msgs := rule.Action()
for _, msg := range msgs {
msg.Room = cronRoom
outCh <- msg
}
}
nextTime = cronexpr.MustParse(rule.When).Next(time.Now())
time.Sleep(2 * time.Second)
}
}
}
示例7: SubmitMultiple
func (s *Scheduler) SubmitMultiple(newJobs []*pb.Job) {
var jobs []*job
for _, j := range newJobs {
job := &job{
id: JobId(j.Id),
expr: cronexpr.MustParse(j.When),
}
jobs = append(jobs, job)
}
s.queue <- jobs
}
示例8: TestGenericFeedFiredOnRefreshAllEvent
func TestGenericFeedFiredOnRefreshAllEvent(t *testing.T) {
done := false
fun := func(ctx *FeedContext) {
if !done {
done = true
ctx.N.Publish(core.NtRefreshAll, 0)
}
}
// expect 2 events (1 due to startup, 1 due to NtRefreshAll request)
if err := runGenericFeedTest(t, fun, cronexpr.MustParse("@hourly"), 1*time.Second, 2); err != nil {
t.Fatal(err)
}
}
示例9: ExampleMustParse
// ExampleMustParse
func ExampleMustParse() {
t := time.Date(2013, time.August, 31, 0, 0, 0, 0, time.UTC)
nextTimes := cronexpr.MustParse("0 0 29 2 *").NextN(t, 5)
for i := range nextTimes {
fmt.Println(nextTimes[i].Format(time.RFC1123))
// Output:
// Mon, 29 Feb 2016 00:00:00 UTC
// Sat, 29 Feb 2020 00:00:00 UTC
// Thu, 29 Feb 2024 00:00:00 UTC
// Tue, 29 Feb 2028 00:00:00 UTC
// Sun, 29 Feb 2032 00:00:00 UTC
}
}
示例10: Start
// verify the schedule and start the timer
func (t *TimerCallback) Start() error {
expr := cronexpr.MustParse(t.Schedule)
if expr.Next(time.Now()).IsZero() {
Logger.Debug("invalid schedule", t.Schedule)
t.State = fmt.Sprintf("NOT Scheduled (invalid Schedule: %s)", t.Schedule)
return fmt.Errorf("invalid schedule", t.Schedule)
}
t.Next = expr.Next(time.Now())
dur := t.Next.Sub(time.Now())
if dur > 0 {
go t.Run(dur)
}
return nil
}
示例11: BenchmarkNext
func BenchmarkNext(b *testing.B) {
exprs := make([]*cronexpr.Expression, benchmarkExpressionsLen)
for i := 0; i < benchmarkExpressionsLen; i++ {
exprs[i] = cronexpr.MustParse(benchmarkExpressions[i])
}
from := time.Now()
b.ResetTimer()
for i := 0; i < b.N; i++ {
expr := exprs[i%benchmarkExpressionsLen]
next := expr.Next(from)
next = expr.Next(next)
next = expr.Next(next)
next = expr.Next(next)
next = expr.Next(next)
}
}
示例12: GetNextStartTime
func (at *ActionPlan) GetNextStartTime(now time.Time) (t time.Time) {
if !at.stCache.IsZero() {
return at.stCache
}
i := at.Timing
if i == nil || i.Timing == nil {
return
}
// Normalize
if i.Timing.StartTime == "" {
i.Timing.StartTime = "00:00:00"
}
if len(i.Timing.Years) > 0 && len(i.Timing.Months) == 0 {
i.Timing.Months = append(i.Timing.Months, 1)
}
if len(i.Timing.Months) > 0 && len(i.Timing.MonthDays) == 0 {
i.Timing.MonthDays = append(i.Timing.MonthDays, 1)
}
at.stCache = cronexpr.MustParse(i.Timing.CronString()).Next(now)
return at.stCache
}
示例13: TestNextN_every5min
func TestNextN_every5min(t *testing.T) {
expected := []string{
"Mon, 2 Sep 2013 08:45:00",
"Mon, 2 Sep 2013 08:50:00",
"Mon, 2 Sep 2013 08:55:00",
"Mon, 2 Sep 2013 09:00:00",
"Mon, 2 Sep 2013 09:05:00",
}
from, _ := time.Parse("2006-01-02 15:04:05", "2013-09-02 08:44:32")
result := cronexpr.MustParse("*/5 * * * *").NextN(from, uint(len(expected)))
if len(result) != len(expected) {
t.Errorf(`MustParse("*/5 * * * *").NextN("2013-09-02 08:44:30", 5):\n"`)
t.Errorf(` Expected %d returned time values but got %d instead`, len(expected), len(result))
}
for i, next := range result {
nextStr := next.Format("Mon, 2 Jan 2006 15:04:05")
if nextStr != expected[i] {
t.Errorf(`MustParse("*/5 * * * *").NextN("2013-09-02 08:44:30", 5):\n"`)
t.Errorf(` result[%d]: expected "%s" but got "%s"`, i, expected[i], nextStr)
}
}
}
示例14: TestNextN
func TestNextN(t *testing.T) {
expected := []string{
"Sat, 30 Nov 2013 00:00:00",
"Sat, 29 Mar 2014 00:00:00",
"Sat, 31 May 2014 00:00:00",
"Sat, 30 Aug 2014 00:00:00",
"Sat, 29 Nov 2014 00:00:00",
}
from, _ := time.Parse("2006-01-02 15:04:05", "2013-09-02 08:44:30")
result := cronexpr.MustParse("0 0 * * 6#5").NextN(from, uint(len(expected)))
if len(result) != len(expected) {
t.Errorf(`MustParse("0 0 * * 6#5").NextN("2013-09-02 08:44:30", 5):\n"`)
t.Errorf(` Expected %d returned time values but got %d instead`, len(expected), len(result))
}
for i, next := range result {
nextStr := next.Format("Mon, 2 Jan 2006 15:04:15")
if nextStr != expected[i] {
t.Errorf(`MustParse("0 0 * * 6#5").NextN("2013-09-02 08:44:30", 5):\n"`)
t.Errorf(` result[%d]: expected "%s" but got "%s"`, i, expected[i], nextStr)
}
}
}
示例15: main
func main() {
app := cli.NewApp()
app.Version = "0.1.0"
app.Name = "cronexpr"
app.Usage = "convert cron expression and get next occurance"
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "unix, u",
Value: "",
Usage: "from specific unix timestamp",
},
cli.StringFlag{
Name: "format, f",
Value: "",
Usage: "format options see http://strftime.org/",
},
cli.StringFlag{
Name: "next, n",
Value: "",
Usage: "n next time stamps",
},
cli.StringFlag{
Name: "utc",
Value: "false",
Usage: "n next time stamps",
},
}
app.Action = func(c *cli.Context) {
cron := ""
if len(c.Args()) > 0 {
cron = c.Args()[0]
} else {
panic("missing cron expression")
}
from := time.Now()
if c.String("unix") != "" {
u, err := strconv.ParseInt(c.String("unix"), 10, 64)
if err != nil {
panic(err)
}
from = time.Unix(u, 0)
}
if c.BoolT("utc") {
from = from.UTC()
}
if c.String("next") != "" {
n, err := strconv.ParseInt(c.String("next"), 10, 64)
if err != nil {
panic(err)
}
result := cronexpr.MustParse(cron).NextN(from, uint(n))
for _, next := range result {
out := strconv.FormatInt(next.Unix(), 10)
if c.String("format") != "" {
out = strftime.Format(c.String("format"), next)
}
fmt.Println(out)
}
} else {
result := cronexpr.MustParse(cron).Next(from)
out := strconv.FormatInt(result.Unix(), 10)
if c.String("format") != "" {
out = strftime.Format(c.String("format"), result)
}
fmt.Println(out)
}
}
app.Run(os.Args)
}