12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364 |
- package main
-
- import (
- "math"
- "strconv"
- "strings"
- "time"
- )
-
- // tick tack tick tack tick tack tick tack
- func ticktack(tickchan chan struct{}, gogoConfig cronConfig) {
- lastSecond := time.Now().Second()
- for {
- currentSecond := time.Now().Second()
- if lastSecond != currentSecond {
- tickchan <- struct{}{}
- lastSecond = currentSecond
- }
- time.Sleep(gogoConfig.Precision)
- }
- }
-
- // isReadyToExec accepting cron value like "*/5" and time value like current second or hour
- func isReadyToExec(cronValue string, timeValue int) bool {
- run := false
- if cronValue != "" {
- // check for * mask
- if strings.Contains(cronValue, "*") {
- // yes, contains. compute now often to run.
- if cronValue == "*" {
- // run every second
- run = true
- }
- if strings.Contains(cronValue, "/") {
- // slash found, compute time to run
- split := strings.Split(cronValue, "/")
- divider, _ := strconv.ParseFloat(split[1], 10)
- if v := math.Mod(float64(timeValue), divider); v == 0 {
- run = true
- }
- }
- } else {
- // search for array of integers
- if strings.Contains(cronValue, ",") {
- // run every second
- split := strings.Split(cronValue, ",")
- for _, intVal := range split {
- arrVal, _ := strconv.ParseInt(intVal, 10, 8)
- if timeValue == int(arrVal) {
- run = true
- break
- }
- }
- }
- // if just a integer
- if intval, err := strconv.Atoi(cronValue); err == nil {
- if timeValue == int(intval) {
- run = true
- }
- }
- }
- }
- return run
- }
|