-
Notifications
You must be signed in to change notification settings - Fork 17
/
main.go
156 lines (140 loc) · 3.45 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/*************************************************************************
* Copyright 2018 Gravwell, Inc. All rights reserved.
* Contact: <legal@gravwell.io>
*
* This software may be modified and distributed under the terms of the
* BSD 2-clause license. See the LICENSE file for details.
**************************************************************************/
package main
import (
"flag"
"log"
"runtime/debug"
"sync"
"time"
"github.com/gravwell/gravwell/v4/ingesters/utils"
"github.com/turnage/graw/reddit"
"github.com/turnage/graw/streams"
)
const ()
var (
verboseFlag = flag.Bool("v", false, "Verbose output")
ver = flag.Bool("version", false, "Print the version information and exit")
expireIDs time.Duration = 48 * time.Hour
parentAuthors = map[string]commentToAuthor{}
cleanThreshhold int = 1000000
verbose bool
)
type commentToAuthor struct {
ID string
Author string
TS time.Time
}
func main() {
debug.SetTraceback("all")
iw, err := NewIngestWriter()
if err != nil {
log.Println("Failed to create new ingest writer", err)
return
}
dieChan := make(chan bool, 1)
var wg sync.WaitGroup
go func() {
for {
if err := redditConnection(iw, dieChan, &wg); err != nil {
log.Println("Got error from reddit system: ", err)
time.Sleep(10 * time.Second)
}
}
}()
//register quit signals so we can die gracefully
utils.WaitForQuit()
dieChan <- true
wg.Wait()
if err := iw.Close(); err != nil {
log.Fatal("Failed to close ingest writer")
}
}
func redditConnection(iw *ingestWriter, dieChan chan bool, wg *sync.WaitGroup) error {
wg.Add(1)
defer wg.Done()
apiHandle, err := reddit.NewScript(`gravwell reddit ingester`, time.Second)
if err != nil {
return err
}
killCh := make(chan bool, 1)
errCh := make(chan error, 1)
comments, err := streams.SubredditComments(apiHandle, killCh, errCh, subreddits...)
if err != nil {
return err
}
tck := time.NewTicker(10 * time.Second)
defer tck.Stop()
mainLoop:
for {
select {
case err, ok := <-errCh:
if !ok {
if verbose {
log.Println("Reddit system error chan closed")
}
}
killCh <- true
return err
case c, ok := <-comments:
if !ok {
if verbose {
log.Println("Reddit system closed the comment channel")
}
break mainLoop
}
iw.emptyTimes = 0
lc := TranslateCommentStructure(c)
if _, ok := parentAuthors[c.ID]; ok {
continue
}
ParentAuthorFunc(&lc)
iw.AddComment(lc)
case _ = <-tck.C:
if err := iw.Flush(); err != nil {
log.Println("Failed to flush comments", err)
}
cleanParentToAuthors()
if iw.emptyTimes > 3 {
// this means we've gone 30 seconds without reading any articles! bail out.
if *verboseFlag {
log.Println("Reddit system appears to have gone quiet, restarting")
}
break mainLoop
}
case _ = <-dieChan:
if err := iw.Flush(); err != nil {
log.Println("Failed to flush comments", err)
}
break mainLoop
}
}
killCh <- true
return nil
}
func ParentAuthorFunc(c *Comment) {
if cta, ok := parentAuthors[c.ParentID]; ok {
c.ParentAuthor = cta.Author
}
parentAuthors[c.ID] = commentToAuthor{
ID: c.ID,
Author: c.Author,
TS: time.Now(),
}
}
func cleanParentToAuthors() {
if len(parentAuthors) < cleanThreshhold {
return
}
expires := time.Now().Add(-1 * expireIDs)
for k, v := range parentAuthors {
if v.TS.Before(expires) {
delete(parentAuthors, k)
}
}
}