-
Notifications
You must be signed in to change notification settings - Fork 17
/
load_general_articles.go
160 lines (135 loc) · 3.8 KB
/
load_general_articles.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
157
158
159
160
package cron
import (
"context"
"time"
"github.com/Ptt-official-app/go-openbbsmiddleware/api"
"github.com/Ptt-official-app/go-openbbsmiddleware/boardd"
"github.com/Ptt-official-app/go-openbbsmiddleware/schema"
"github.com/Ptt-official-app/go-openbbsmiddleware/types"
"github.com/Ptt-official-app/go-pttbbs/bbs"
"github.com/sirupsen/logrus"
)
func RetryLoadGeneralArticles(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return nil
default:
logrus.Infof("RetryLoadGeneralArticles: to LoadGeneralArticles")
_ = LoadGeneralArticles()
select {
case <-ctx.Done():
return nil
default:
logrus.Infof("RetryLoadGeneralArticles: to sleep 1 min")
time.Sleep(1 * time.Minute)
}
}
}
}
func LoadGeneralArticles() (err error) {
nextBrdname := ""
count := 0
for {
boardIDs, err := schema.GetBoardIDs(nextBrdname, false, N_BOARDS+1, true)
if err != nil {
return err
}
newNextBrdname := ""
if len(boardIDs) == N_BOARDS+1 {
newNextBoardID := boardIDs[N_BOARDS]
newNextBrdname = newNextBoardID.Brdname
boardIDs = boardIDs[:N_BOARDS]
}
for _, each := range boardIDs {
err = loadGeneralArticles(each.BBoardID)
if err == nil {
count++
}
}
if newNextBrdname == "" {
logrus.Infof("cron.LoadGeneralArticle: load %v boards", count)
return nil
}
nextBrdname = newNextBrdname
}
}
func loadGeneralArticles(boardID bbs.BBoardID) (err error) {
nextIdx := int32(0)
count := 0
for {
articleSummaries, newNextIdx, err := loadGeneralArticlesCore(boardID, nextIdx)
if err != nil {
logrus.Errorf("cron.LoadGeneralArticles: unable to loadGeneralArticles: nextIdx: %v e: %v", nextIdx, err)
return err
}
count += len(articleSummaries)
logrus.Infof("cron.LoadGeneralArticles: bid: %v count: %v", boardID, count)
if newNextIdx == INVALID_LOAD_GENERAL_ARTICLES_NEXT_IDX {
logrus.Infof("cron.LoadGeneralArticles: bid: %v load %v articles", boardID, count)
break
}
nextIdx = newNextIdx
}
err = loadBottomArticles(boardID)
if err != nil {
logrus.Errorf("loadGeneralArticles: unable to loadBottomArticles: e: %v", err)
return err
}
return nil
}
func loadGeneralArticlesCore(boardID bbs.BBoardID, startIdx int32) (articleSummaries []*schema.ArticleSummaryWithRegex, nextIdx int32, err error) {
nextIdx = INVALID_LOAD_GENERAL_ARTICLES_NEXT_IDX
brdnameStr := boardID.ToBrdname()
// backend load-general-articles
ctx := context.Background()
brdname := &boardd.BoardRef_Name{Name: brdnameStr}
req := &boardd.ListRequest{
Ref: &boardd.BoardRef{Ref: brdname},
IncludePosts: true,
Offset: startIdx,
Length: N_ARTICLES + 1,
}
resp, err := boardd.Cli.List(ctx, req)
if err != nil {
return nil, INVALID_LOAD_GENERAL_ARTICLES_NEXT_IDX, err
}
posts := resp.Posts
if len(posts) == N_ARTICLES+1 {
nextIdx = startIdx + N_ARTICLES
posts = posts[:N_ARTICLES]
}
// update to db
updateNanoTS := types.NowNanoTS()
articleSummaries, err = api.DeserializePBArticlesAndUpdateDB(boardID, posts, updateNanoTS, false)
if err != nil {
return nil, INVALID_LOAD_GENERAL_ARTICLES_NEXT_IDX, err
}
return articleSummaries, nextIdx, nil
}
func loadBottomArticles(boardID bbs.BBoardID) (err error) {
brdnameStr := boardID.ToBrdname()
// backend load-general-articles
ctx := context.Background()
brdname := &boardd.BoardRef_Name{Name: brdnameStr}
req := &boardd.ListRequest{
Ref: &boardd.BoardRef{Ref: brdname},
IncludeBottoms: true,
Offset: 0,
Length: N_ARTICLES + 1,
}
resp, err := boardd.Cli.List(ctx, req)
if err != nil {
return err
}
err = schema.ResetArticleIsBottom(boardID)
if err != nil {
return err
}
updateNanoTS := types.NowNanoTS()
_, err = api.DeserializePBArticlesAndUpdateDB(boardID, resp.Bottoms, updateNanoTS, true)
if err != nil {
return err
}
return nil
}