Cron with interval 1 second, yaml based configs http://gogocron.blindage.org/
go
Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.

timing.go 1.5KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. package main
  2. import (
  3. "math"
  4. "strconv"
  5. "strings"
  6. "time"
  7. )
  8. // tick tack tick tack tick tack tick tack
  9. func ticktack(tickchan chan struct{}, gogoConfig cronConfig) {
  10. lastSecond := time.Now().Second()
  11. for {
  12. currentSecond := time.Now().Second()
  13. if lastSecond != currentSecond {
  14. tickchan <- struct{}{}
  15. lastSecond = currentSecond
  16. }
  17. time.Sleep(gogoConfig.Precision)
  18. }
  19. }
  20. // isReadyToExec accepting cron value like "*/5" and time value like current second or hour
  21. func isReadyToExec(cronValue string, timeValue int) bool {
  22. run := false
  23. if cronValue != "" {
  24. // check for * mask
  25. if strings.Contains(cronValue, "*") {
  26. // yes, contains. compute now often to run.
  27. if cronValue == "*" {
  28. // run every second
  29. run = true
  30. }
  31. if strings.Contains(cronValue, "/") {
  32. // slash found, compute time to run
  33. split := strings.Split(cronValue, "/")
  34. divider, _ := strconv.ParseFloat(split[1], 10)
  35. if v := math.Mod(float64(timeValue), divider); v == 0 {
  36. run = true
  37. }
  38. }
  39. } else {
  40. // search for array of integers
  41. if strings.Contains(cronValue, ",") {
  42. // run every second
  43. split := strings.Split(cronValue, ",")
  44. for _, intVal := range split {
  45. arrVal, _ := strconv.ParseInt(intVal, 10, 8)
  46. if timeValue == int(arrVal) {
  47. run = true
  48. break
  49. }
  50. }
  51. }
  52. // if just a integer
  53. if intval, err := strconv.Atoi(cronValue); err == nil {
  54. if timeValue == int(intval) {
  55. run = true
  56. }
  57. }
  58. }
  59. }
  60. return run
  61. }