-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
75 lines (59 loc) · 1.25 KB
/
main.go
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
71
72
73
74
75
// +build linux
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
)
func main() {
parseFlags()
cleanup()
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go func() {
sigch := make(chan os.Signal, 1)
defer signal.Stop(sigch)
signal.Notify(sigch, syscall.SIGINT, syscall.SIGTERM)
select {
case sig := <-sigch:
log.Println(sig, "- cleaning up")
case <-ctx.Done():
}
cancel()
}()
buffer := &dataBuffer{
recent: make([]string, minLinesBeforeDuplicate),
fuzzy: make([][]string, fuzzyDuplicateWindow),
queue: make([]string, 0, maxQueuedLines),
}
client := initClient()
pullExistingStatuses(ctx, buffer, client)
ch := make(chan string)
go dwarfFortress(ctx, buffer, ch)
initialDelay := nextDelay()
log.Println("Waiting", initialDelay, "before making first toot.")
time.Sleep(initialDelay)
for {
var line string
select {
case <-ctx.Done():
return
case line = <-ch:
default:
log.Println("Warning: no toots are ready")
select {
case <-ctx.Done():
return
case line = <-ch:
}
}
makeToot(ctx, client, line)
time.Sleep(nextDelay())
}
}
func nextDelay() time.Duration {
return tootInterval - time.Duration(time.Now().UnixNano())%tootInterval
}