• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

Golang cronexpr.MustParse函数代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

本文整理汇总了Golang中github.com/gorhill/cronexpr.MustParse函数的典型用法代码示例。如果您正苦于以下问题:Golang MustParse函数的具体用法?Golang MustParse怎么用?Golang MustParse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。



在下文中一共展示了MustParse函数的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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


示例16:

	. "github.com/onsi/ginkgo"
	. "github.com/onsi/gomega"
	"github.com/onsi/gomega/gbytes"
	"github.com/onsi/gomega/gexec"
)

var _ = Describe("Check", func() {
	var checkCmd *exec.Cmd

	BeforeEach(func() {
		checkCmd = exec.Command(checkPath)
	})

	Context("MustParse", func() {
		It("can parse crontab expressions", func() {
			expr := cronexpr.MustParse("* * * * 0-5")
			Expect(expr).ToNot(BeNil())
		})
	})

	Context("when a crontab expression is specified", func() {
		var request models.CheckRequest
		var response models.CheckResponse
		var session *gexec.Session

		BeforeEach(func() {
			request = models.CheckRequest{
				Source: models.Source{
					Location: "America/New_York",
				},
			}
开发者ID:pivotal-cf-experimental,项目名称:cron-resource,代码行数:31,代码来源:check_test.go


示例17: TestGenericHourlyFeed

func TestGenericHourlyFeed(t *testing.T) {
	if err := runGenericFeedTest(t, nil, cronexpr.MustParse("@hourly"), 1*time.Second, 1); err != nil {
		t.Fatal(err)
	}
}
开发者ID:jacktang,项目名称:ibconnect.go,代码行数:5,代码来源:generic_feed_test.go


示例18: TestGenericFeedRefreshInterval

func TestGenericFeedRefreshInterval(t *testing.T) {
	oncePerSecond := "* * * * * * *"
	if err := runGenericFeedTest(t, nil, cronexpr.MustParse(oncePerSecond), 3*time.Second, 2); err != nil {
		t.Fatal(err)
	}
}
开发者ID:jacktang,项目名称:ibconnect.go,代码行数:6,代码来源:generic_feed_test.go


示例19: BenchmarkParse

func BenchmarkParse(b *testing.B) {
	for i := 0; i < b.N; i++ {
		_ = cronexpr.MustParse(benchmarkExpressions[i%benchmarkExpressionsLen])
	}
}
开发者ID:ZhuHangpeng,项目名称:mig,代码行数:5,代码来源:cronexpr_test.go


示例20: main

func main() {
	flag.Parse()
	runtime.GOMAXPROCS(*threads)

	var cron *cronexpr.Expression
	if *update != "" {
		cron = cronexpr.MustParse(*update)
	}

	var db oui.DynamicDB
	url := ""
	fileName := ""
	var err error

	if strings.HasPrefix(*ouiFile, "http") {
		url = *ouiFile
		if url == "http" {
			url = "http://standards-oui.ieee.org/oui.txt"
		}
		log.Println("Downloading new Db from: " + url)
		db, err = oui.OpenHttp(url)
		if err != nil {
			log.Fatalf("Error downloading:%s", err.Error())
		}
	} else {
		fileName = *ouiFile
		log.Println("Opening database from: " + fileName)
		db, err = oui.OpenFile(fileName)
		if err != nil {
			log.Fatalf("Error updating file:%s", err.Error())
		}
	}
	log.Printf("Database generated at %s\n", db.Generated().Local().String())

	// Start updater if needed.
	if cron != nil {
		go func() {
			for {
				// Sleep until next update
				next := cron.Next(time.Now())
				log.Println("Next update: " + next.String())
				time.Sleep(next.Sub(time.Now()))
				if url != "" {
					log.Println("Updating db from: " + url)
					err := oui.UpdateHttp(db, url)
					if err != nil {
						log.Printf("Error downloading update:%s", err.Error())
					} else {
						log.Println("Updated Successfully")
					}
				} else {
					log.Println("Updating db with file: " + fileName)
					err := oui.UpdateFile(db, fileName)
					if err != nil {
						log.Printf("Error loading update:%s", err.Error())
					} else {
						log.Println("Updated Successfully")
					}
				}
			}
		}()
	}

	// We dereference this to avoid a pretty big penalty under heavy load.
	prettyL := *pretty

	http.HandleFunc("/", func(w http.ResponseWriter, req *http.Request) {
		var mac string
		var hw *oui.HardwareAddr

		// Prepare the response and queue sending the result.
		res := &Response{}

		defer func() {
			var j []byte
			var err error
			if prettyL {
				j, err = json.MarshalIndent(res, "", "  ")
			} else {
				j, err = res.MarshalJSON()
			}
			if err != nil {
				log.Fatal(err)
			}
			w.Write(j)
		}()

		// Set headers
		if *originPolicy != "" {
			w.Header().Set("Access-Control-Allow-Origin", *originPolicy)
		}
		w.Header().Set("Content-Type", "application/json")
		w.Header().Set("Last-Modified", db.Generated().Format(http.TimeFormat))

		// Find Mac
		mac = req.URL.Query().Get("mac")
		if mac == "" {
			mac = strings.Trim(req.URL.Path, "/")
		}
		hw, err := oui.ParseMac(mac)
//.........这里部分代码省略.........
开发者ID:wheelcomplex,项目名称:oui,代码行数:101,代码来源:ouiserver.go



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Golang cronexpr.Parse函数代码示例发布时间:2022-05-23
下一篇:
Golang portaudio.Terminate函数代码示例发布时间:2022-05-23
热门推荐
热门话题
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap