-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathalive.go
More file actions
70 lines (60 loc) · 1.49 KB
/
alive.go
File metadata and controls
70 lines (60 loc) · 1.49 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
67
68
69
70
package main
import (
"bytes"
"os"
"sync"
"time"
)
func aliveCheckPauseLoop(delay time.Duration, dl *DataLogWriter, fd *os.File, wg *sync.WaitGroup) {
go func() {
var buffer []byte
buffer = make([]byte, 128)
for {
now := time.Now()
// read old value
fd.Seek(0, 0)
n, _ := fd.Read(buffer)
if n == len(buffer) { // we have a full 128 bytes buffer
pos := bytes.IndexByte(buffer, 0) // with some 0 somewhere
if pos > 0 {
org := string(buffer[:pos]) // extract string
date, err := time.Parse(time.RFC3339, org)
if err == nil {
// if it was more than delay*2 time ago (and not
// a proper suspend/resume) add a stop / start
diff := now.Sub(date)
if diff > delay*2 && dl.AnySuspendEventBefore(date, delay*2) == false {
dl.AppendRaw(date.Format(time.RFC3339) + ";stop\n")
dl.Append("start")
}
}
}
} else {
// probably a first start (no existing alive.dat)
dl.Append("start")
}
// overwrite with current time
fd.Seek(0, 0)
copy(buffer, now.Format(time.RFC3339))
fd.Write(buffer)
if wg != nil {
wg.Done()
wg = nil
}
time.Sleep(delay)
}
}()
}
// AliveSchedule test
// (will wait the first loop to finish)
func AliveSchedule(delay time.Duration, dl *DataLogWriter) error {
fd, err := os.OpenFile(aliveFilePath, os.O_CREATE|os.O_RDWR, 0644)
if err != nil {
return err
}
var wg sync.WaitGroup
wg.Add(1)
go aliveCheckPauseLoop(delay, dl, fd, &wg)
wg.Wait()
return nil
}