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
|
package autotrack
import (
"os"
"time"
"ticktock/internal/config"
)
// Run executes the poll loop: sample → Classify → Step → WriteSegment →
// sleep, until a signal arrives on sigc; then it flushes the open segment and
// returns (parity with the Python daemon's SIGTERM/SIGINT flush). sample is
// injected (ReadSample in production) so the loop is testable.
func Run(dataDir string, tr config.Tracking, sample func() Sample, sigc <-chan os.Signal) {
var cur *Segment
var curKey Key
poll := time.Duration(tr.PollSecs) * time.Second
for {
now := time.Now()
s := sample()
hard := s.Locked || Away(s.Raw)
ctx := Classify(s.Idle, s.Raw, tr.IdleGraceSecs, s.Locked)
var emitted *Segment
cur, curKey, emitted = Step(cur, curKey, now, s.Idle, ctx, tr.IdleGraceSecs, hard)
if emitted != nil {
_ = WriteSegment(dataDir, emitted, tr.MinSecs) // an IO hiccup must not kill the loop
}
select {
case <-sigc:
if cur != nil {
closed := *cur
closed.End = time.Now()
_ = WriteSegment(dataDir, &closed, tr.MinSecs)
}
return
case <-time.After(poll):
}
}
}
|