当前位置: 首页>>代码示例>>Golang>>正文


Golang cronexpr.MustParse函数代码示例

本文整理汇总了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
}
开发者ID:mjbrender,项目名称:hustlebot,代码行数:29,代码来源:chore.go

示例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))
}
开发者ID:shuhaowu,项目名称:towncrier,代码行数:26,代码来源:config.go

示例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())
			}
		}
	}()
}
开发者ID:LA3QMA,项目名称:wl2k-go,代码行数:25,代码来源:schedule.go

示例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'`)
	}
}
开发者ID:ZhuHangpeng,项目名称:mig,代码行数:17,代码来源:cronexpr_test.go

示例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")
	}
}
开发者ID:jacktang,项目名称:ibconnect.go,代码行数:8,代码来源:generic_feed_test.go

示例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)
		}
	}
}
开发者ID:ccirello,项目名称:gochatbot,代码行数:19,代码来源:cron.go

示例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
}
开发者ID:a-palchikov,项目名称:kron,代码行数:12,代码来源:scheduler.go

示例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)
	}
}
开发者ID:jacktang,项目名称:ibconnect.go,代码行数:13,代码来源:generic_feed_test.go

示例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
	}
}
开发者ID:ZhuHangpeng,项目名称:mig,代码行数:14,代码来源:example_test.go

示例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
}
开发者ID:sharadgana,项目名称:lazlo,代码行数:15,代码来源:timers.go

示例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)
	}
}
开发者ID:ZhuHangpeng,项目名称:mig,代码行数:16,代码来源:cronexpr_test.go

示例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
}
开发者ID:foehn,项目名称:cgrates,代码行数:21,代码来源:action_plan.go

示例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)
		}
	}
}
开发者ID:ZhuHangpeng,项目名称:mig,代码行数:22,代码来源:cronexpr_test.go

示例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)
		}
	}
}
开发者ID:ZhuHangpeng,项目名称:mig,代码行数:22,代码来源:cronexpr_test.go

示例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)
}
开发者ID:fentas,项目名称:cronexpr,代码行数:72,代码来源:cronexpr.go


注:本文中的github.com/gorhill/cronexpr.MustParse函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。