-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
66 lines (53 loc) · 1.67 KB
/
Copy pathmain.go
File metadata and controls
66 lines (53 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
package main
// Logging Time, could be the heart-beat of a scheduling system.
import (
"log"
"sync"
"time"
)
// watchFile contains the logMessage.SEC, logMessage.MIN, and logMessage.HR.
var watchPath = "config"
var watchFile = "config/clock.json"
func main() {
// readConf, channels, and watcher are all in support of realtime message changes.
logMessage := readConf(watchFile)
newConf := make(chan bool)
quit := make(chan bool)
wg := new(sync.WaitGroup) // I want to wait for watcher to end.
wg.Add(1) // I know I don't need to wait, I'm doing it just because I should.
go watcher(wg, watchPath, watchFile, quit, newConf)
// The ticker and timing variables are for logging time.
clockTick := time.NewTicker(time.Second)
min := time.Minute
hr := time.Hour
deadline := hr * 3
msg := logMessage.SEC
// This process will run until the duration of sec >= deadline.
for sec := time.Second; sec <= deadline; sec = sec + time.Second {
select {
case <-clockTick.C:
// Set the value of msg, depending on the modulus of time duration.
if sec%min == 0 {
msg = logMessage.MIN
}
if sec%hr == 0 {
msg = logMessage.HR
}
// Print msg, and set default msg
log.Printf("%s, total time: %v \n", msg, sec)
msg = logMessage.SEC
case <-newConf: // Fires whenever clock.json is changed
logMessage = readConf(watchFile)
}
// If passed deadline, tell the watcher to quit, log the event, and stop the ticker.
if sec >= deadline {
quit <- true
log.Printf("Deadline of %v has passed.\n", sec)
clockTick.Stop()
break
}
}
// Wait for the watcher to stop, it's the right thing to do ;-)
wg.Wait()
println("Main is shutting down.")
}