Browse Source

new ticktack() function with check every 10 ms

tags/version-1.0
Vladimir Smagin 1 year ago
parent
commit
14f87e02e7
3 changed files with 66 additions and 55 deletions
  1. 19
    0
      config.go
  2. 47
    0
      cron.go
  3. 0
    55
      gogocron.go

+ 19
- 0
config.go View File

@@ -0,0 +1,19 @@
package main

import "log"

// loadConfig() searches for config files and reading them to cronTasks
// used once at start and every HUP signal
// TODO: add HUP signal
func loadConfig() cronTasks {
var tasks cronTasks

configFiles := readConfigDirectory("configs")
log.Printf("Files found: %v", configFiles)
for _, configFile := range configFiles {
config := readConfigFile(configFile)
log.Printf("Parsed %#v", config)
tasks = append(tasks, config)
}
return tasks
}

+ 47
- 0
cron.go View File

@@ -0,0 +1,47 @@
package main

import (
"math"
"strconv"
"strings"
"time"
)

// tick tack tick tack tick tack tick tack
func ticktack(tickchan chan struct{}) {
lastSecond := time.Now().Second()
for {
currentSecond := time.Now().Second()
if lastSecond != currentSecond {
tickchan <- struct{}{}
lastSecond = currentSecond
}
time.Sleep(time.Second / 100) // check time every 10 ms
}
}

// 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 {
// no, searching for integers and arrays of integers
}
}
return run
}

+ 0
- 55
gogocron.go View File

@@ -2,9 +2,6 @@ package main

import (
"log"
"math"
"strconv"
"strings"
"time"
)

@@ -22,58 +19,6 @@ type cronTask struct {

type cronTasks []cronTask

// loadConfig() searches for config files and reading them to cronTasks
// used once at start and every HUP signal
// TODO: add HUP signal
func loadConfig() cronTasks {
var tasks cronTasks

configFiles := readConfigDirectory("configs")
log.Printf("Files found: %v", configFiles)
for _, configFile := range configFiles {
config := readConfigFile(configFile)
log.Printf("Parsed %#v", config)
tasks = append(tasks, config)
}
return tasks
}

// tick tack tick tack tick tack tick tack
func ticktack(tickchan chan struct{}) {
for {
// TODO: this code is wrong, but enough for tests
// rewrite to calculate seconds change
time.Sleep(time.Second)
tickchan <- struct{}{}
}
}

// 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 {
// no, searching for integers and arrays of integers
}
}
return run
}

// search for tasks that must be executed now
func filterTasksToExecute(tasks cronTasks) cronTasks {
now := time.Now()

Loading…
Cancel
Save