Skip to content

Commit

Permalink
dornröschen: add gokrazy-specific main
Browse files Browse the repository at this point in the history
  • Loading branch information
stapelberg committed Mar 4, 2017
1 parent 98cd120 commit 6f90ace
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 9 deletions.
13 changes: 4 additions & 9 deletions dornröschen/dornröschen.go
Original file line number Diff line number Diff line change
Expand Up @@ -225,17 +225,14 @@ func sync(NASen []string) {
}
}

func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
flag.Parse()

func run() error {
if !*runBackup && !*runSync {
log.Fatal("Neither -backup nor -sync enabled, nothing to do.")
return fmt.Errorf("Neither -backup nor -sync enabled, nothing to do.")
}

storageList := strings.Split(*storageHosts, ",")
if len(storageList) > 2 {
log.Fatal("More than 2 -storage_hosts are not supported. Please send a patch to fix.")
return fmt.Errorf("More than 2 -storage_hosts are not supported. Please send a patch to fix.")
}

if *runBackup {
Expand All @@ -253,7 +250,5 @@ func main() {
prometheus.MustRegister(lastSuccess)
lastSuccess.Set(float64(time.Now().Unix()))

if err := prometheus.Push("dornroeschen", "dornroeschen", *pushGateway); err != nil {
log.Fatal(err)
}
return prometheus.Push("dornroeschen", "dornroeschen", *pushGateway)
}
44 changes: 44 additions & 0 deletions dornröschen/gokrazy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// +build gokrazy

package main

import (
"log"
"time"

"github.com/gokrazy/gokrazy"
)

func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)

gokrazy.WaitForClock()

// Run forever, trigger a run at 10:00 each Monday through Friday.
for {
now := time.Now()
runToday := now.Hour() < 10 &&
now.Weekday() != time.Saturday &&
now.Weekday() != time.Sunday
today := now.Day()
log.Printf("now = %v, runToday = %v", now, runToday)
for {
if time.Now().Day() != today {
// Day changed, re-evaluate whether to run today.
break
}

nextHour := time.Now().Truncate(time.Hour).Add(1 * time.Hour)
log.Printf("today = %d, runToday = %v, next hour: %v", today, runToday, nextHour)
time.Sleep(time.Until(nextHour))

if time.Now().Hour() >= 10 && runToday {
runToday = false
log.Println("Running dornröschen")
if err := run(); err != nil {
log.Printf("failed: %v", err)
}
}
}
}
}
17 changes: 17 additions & 0 deletions dornröschen/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// +build !gokrazy

package main

import (
"flag"
"log"
)

func main() {
log.SetFlags(log.LstdFlags | log.Lshortfile)
flag.Parse()

if err := run(); err != nil {
log.Fatal(err)
}
}

0 comments on commit 6f90ace

Please sign in to comment.