gocron.go 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. // Package gocron : A Golang Job Scheduling Package.
  2. //
  3. // An in-process scheduler for periodic jobs that uses the builder pattern
  4. // for configuration. Schedule lets you run Golang functions periodically
  5. // at pre-determined intervals using a simple, human-friendly syntax.
  6. //
  7. // Copyright 2014 Jason Lyu. jasonlvhit@gmail.com .
  8. // All rights reserved.
  9. // Use of this source code is governed by a BSD-style .
  10. // license that can be found in the LICENSE file.
  11. package gocron
  12. import (
  13. "crypto/sha256"
  14. "errors"
  15. "fmt"
  16. "reflect"
  17. "regexp"
  18. "runtime"
  19. "time"
  20. )
  21. // Error declarations for gocron related errors
  22. var (
  23. ErrTimeFormat = errors.New("time format error")
  24. ErrParamsNotAdapted = errors.New("the number of params is not adapted")
  25. ErrNotAFunction = errors.New("only functions can be schedule into the job queue")
  26. ErrPeriodNotSpecified = errors.New("unspecified job period")
  27. ErrNotScheduledWeekday = errors.New("job not scheduled weekly on a weekday")
  28. ErrJobNotFoundWithTag = errors.New("no jobs found with given tag")
  29. ErrUnsupportedTimeFormat = errors.New("the given time format is not supported")
  30. )
  31. // regex patterns for supported time formats
  32. var (
  33. timeWithSeconds = regexp.MustCompile(`(?m)^\d{1,2}:\d\d:\d\d$`)
  34. timeWithoutSeconds = regexp.MustCompile(`(?m)^\d{1,2}:\d\d$`)
  35. )
  36. type timeUnit int
  37. const (
  38. seconds timeUnit = iota + 1
  39. minutes
  40. hours
  41. days
  42. weeks
  43. months
  44. )
  45. func callJobFuncWithParams(jobFunc interface{}, params []interface{}) ([]reflect.Value, error) {
  46. f := reflect.ValueOf(jobFunc)
  47. if len(params) != f.Type().NumIn() {
  48. return nil, ErrParamsNotAdapted
  49. }
  50. in := make([]reflect.Value, len(params))
  51. for k, param := range params {
  52. in[k] = reflect.ValueOf(param)
  53. }
  54. return f.Call(in), nil
  55. }
  56. func getFunctionName(fn interface{}) string {
  57. return runtime.FuncForPC(reflect.ValueOf(fn).Pointer()).Name()
  58. }
  59. func getFunctionKey(funcName string) string {
  60. h := sha256.New()
  61. h.Write([]byte(funcName))
  62. return fmt.Sprintf("%x", h.Sum(nil))
  63. }
  64. func parseTime(t string) (hour, min, sec int, err error) {
  65. var timeLayout string
  66. switch {
  67. case timeWithSeconds.Match([]byte(t)):
  68. timeLayout = "15:04:05"
  69. case timeWithoutSeconds.Match([]byte(t)):
  70. timeLayout = "15:04"
  71. default:
  72. return 0, 0, 0, ErrUnsupportedTimeFormat
  73. }
  74. parsedTime, err := time.Parse(timeLayout, t)
  75. if err != nil {
  76. return 0, 0, 0, ErrUnsupportedTimeFormat
  77. }
  78. return parsedTime.Hour(), parsedTime.Minute(), parsedTime.Second(), nil
  79. }