本文整理汇总了Golang中github.com/robfig/cron.Parse函数的典型用法代码示例。如果您正苦于以下问题:Golang Parse函数的具体用法?Golang Parse怎么用?Golang Parse使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Parse函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: nextRun
func nextRun(schedule string) (int64, error) {
sched, err := cron.Parse(schedule)
if err != nil {
return 0, err
}
return sched.Next(time.Now().UTC()).UnixNano(), nil
}
示例2: Add
//create a schdule with the cmd ID (overrides old ones) and
func (sched *Scheduler) Add(cmd *core.Command) (interface{}, error) {
defer sched.restart()
db := sched.pool.Get()
defer db.Close()
job := &SchedulerJob{}
err := json.Unmarshal([]byte(cmd.Data), job)
if err != nil {
log.Println("Failed to load command spec", cmd.Data, err)
return nil, err
}
_, err = cron.Parse(job.Cron)
if err != nil {
return nil, err
}
job.ID = cmd.ID
//we can safely push the command to the hashset now.
db.Do("HSET", hashScheduleKey, cmd.ID, cmd.Data)
return true, nil
}
示例3: ValidCron
// ValidCron checks if the syntax of value is valid for a cron job
func ValidCron(value string) error {
_, err := cron.Parse(value)
if err != nil {
return errors.New(notValidCron)
}
return nil
}
示例4: Set
func (c *CronValue) Set(value string) error {
_, err := cron.Parse(value)
if err != nil {
return fmt.Errorf("expected cron expression or '@every DURATION' got '%s'", value)
}
*c = (CronValue)(value)
return nil
}
示例5: validateScheduleFormat
func validateScheduleFormat(schedule string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
_, err := cron.Parse(schedule)
if err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, schedule, err.Error()))
}
return allErrs
}
示例6: Validate
// Validate returns error if cron entry dosn't match crontab format
func (c *CronSchedule) Validate() error {
if c.entry == "" {
return ErrMissingCronEntry
}
_, err := cron.Parse(c.entry)
if err != nil {
return err
}
return nil
}
示例7: Schedule
func Schedule(spec string, job cron.Job) {
// Look to see if given spec is a key from the Config.
if strings.HasPrefix(spec, "cron.") {
confSpec, found := revel.Config.String(spec)
if !found {
panic("Cron spec not found: " + spec)
}
spec = confSpec
}
MainCron.Schedule(cron.Parse(spec), New(job))
}
示例8: newJob
func newJob(id string, pri int) *Job {
sch, _ := cron.Parse("* * * * *")
// subtract priority from fixed last run, so higher priority jobs were last run further back in time
t_last_run := time.Unix(1437856044-int64(pri), 0)
return &Job{
Id: id,
t_schedule: sch,
t_last_run: &t_last_run,
scheduling_priority: NormalPriority,
}
}
示例9: PutTask
func (k *Keeper) PutTask(name string, t Task) error {
k.tkmu.Lock()
defer k.tkmu.Unlock()
if name != t.Name {
return errors.New("Task name not correct")
}
if _, err := cron.Parse(t.Schedule); err != nil {
return err
}
k.tasks[name] = t
k.reloadCron()
return k.save()
}
示例10: validateScheduleFormat
func validateScheduleFormat(schedule string, fldPath *field.Path) field.ErrorList {
allErrs := field.ErrorList{}
// TODO soltysh: this should be removed when https://github.com/robfig/cron/issues/58 is fixed
tmpSchedule := schedule
if len(schedule) > 0 && schedule[0] != '@' {
tmpSchedule = "0 " + schedule
}
if _, err := cron.Parse(tmpSchedule); err != nil {
allErrs = append(allErrs, field.Invalid(fldPath, schedule, err.Error()))
}
return allErrs
}
示例11: UpdateSchedule
func UpdateSchedule(s *spec.Schedule) error {
_, err := cron.Parse(s.String())
if err != nil {
return err
}
if err = gDb.UpdateSchedule(s); err != nil {
return err
}
schedules.Stop()
return initCron()
}
示例12: getRecentUnmetScheduleTimes
// getRecentUnmetScheduleTimes gets a slice of times (from oldest to latest) that have passed when a Job should have started but did not.
//
// If there are too many (>100) unstarted times, just give up and return an empty slice.
// If there were missed times prior to the last known start time, then those are not returned.
func getRecentUnmetScheduleTimes(sj batch.ScheduledJob, now time.Time) ([]time.Time, error) {
starts := []time.Time{}
tmpSched := addSeconds(sj.Spec.Schedule)
sched, err := cron.Parse(tmpSched)
if err != nil {
return starts, fmt.Errorf("Unparseable schedule: %s : %s", sj.Spec.Schedule, err)
}
var earliestTime time.Time
if sj.Status.LastScheduleTime != nil {
earliestTime = sj.Status.LastScheduleTime.Time
} else {
// If none found, then this is either a recently created scheduledJob,
// or the active/completed info was somehow lost (contract for status
// in kubernetes says it may need to be recreated), or that we have
// started a job, but have not noticed it yet (distributed systems can
// have arbitrary delays). In any case, use the creation time of the
// ScheduledJob as last known start time.
earliestTime = sj.ObjectMeta.CreationTimestamp.Time
}
if earliestTime.After(now) {
return []time.Time{}, nil
}
for t := sched.Next(earliestTime); !t.After(now); t = sched.Next(t) {
starts = append(starts, t)
// An object might miss several starts. For example, if
// controller gets wedged on friday at 5:01pm when everyone has
// gone home, and someone comes in on tuesday AM and discovers
// the problem and restarts the controller, then all the hourly
// jobs, more than 80 of them for one hourly scheduledJob, should
// all start running with no further intervention (if the scheduledJob
// allows concurrency and late starts).
//
// However, if there is a bug somewhere, or incorrect clock
// on controller's server or apiservers (for setting creationTimestamp)
// then there could be so many missed start times (it could be off
// by decades or more), that it would eat up all the CPU and memory
// of this controller. In that case, we want to not try to list
// all the misseded start times.
//
// I've somewhat arbitrarily picked 100, as more than 80, but
// but less than "lots".
if len(starts) > 100 {
// We can't get the most recent times so just return an empty slice
return []time.Time{}, fmt.Errorf("Too many missed start times to list")
}
}
return starts, nil
}
示例13: AddTask
func (k *Keeper) AddTask(t Task) error {
k.tkmu.Lock()
defer k.tkmu.Unlock()
if _, exists := k.tasks[t.Name]; exists {
return errors.New("Task name duplicate: " + t.Name)
}
if _, err := cron.Parse(t.Schedule); err != nil {
return err
}
t.Enabled = true
k.tasks[t.Name] = t
k.reloadCron()
return k.save()
}
示例14: getNextStartTimeAfter
// getNextStartTimeAfter gets the latest scheduled start time that is less than "now", or an error.
func getNextStartTimeAfter(schedule string, now time.Time) (time.Time, error) {
// Using robfig/cron for cron scheduled parsing and next runtime
// computation. Not using the entire library because:
// - I want to detect when we missed a runtime due to being down.
// - How do I set the time such that I can detect the last known runtime?
// - I guess the functions could launch a go-routine to start the job and
// then return.
// How to handle concurrency control.
// How to detect changes to schedules or deleted schedules and then
// update the jobs?
sched, err := cron.Parse(schedule)
if err != nil {
return time.Unix(0, 0), fmt.Errorf("Unparseable schedule: %s : %s", schedule, err)
}
return sched.Next(now), nil
}
示例15: Schedule
func Schedule(spec string, job cron.Job) error {
// Look to see if given spec is a key from the Config.
if strings.HasPrefix(spec, "cron.") {
confSpec, found := revel.Config.String(spec)
if !found {
panic("Cron spec not found: " + spec)
}
spec = confSpec
}
sched, err := cron.Parse(spec)
if err != nil {
return err
}
MainCron.Schedule(sched, New(job))
return nil
}