當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。