-
Notifications
You must be signed in to change notification settings - Fork 17
/
load_man_articles.go
124 lines (104 loc) · 2.77 KB
/
load_man_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
package cron
import (
"context"
"runtime/debug"
"time"
"github.com/Ptt-official-app/go-openbbsmiddleware/api"
"github.com/Ptt-official-app/go-openbbsmiddleware/mand"
"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 RetryLoadManArticles(ctx context.Context) error {
defer func() {
if r := recover(); r != nil {
logrus.Errorf("RetryLoadManArticles: Recovered r: %v stack: %v", r, string(debug.Stack()))
}
}()
for {
select {
case <-ctx.Done():
return nil
default:
logrus.Infof("RetryLoadManArticles: to LoadManArticles")
_ = LoadManArticles()
select {
case <-ctx.Done():
return nil
default:
logrus.Infof("RetryLoadManArticles: to sleep 1 min")
time.Sleep(1 * time.Minute)
}
}
}
}
func LoadManArticles() (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 = loadManArticles(each.BBoardID)
if err == nil {
count++
}
}
if newNextBrdname == "" {
logrus.Infof("cron.LoadManArticles: load %v boards", count)
return nil
}
nextBrdname = newNextBrdname
}
}
func loadManArticles(boardID bbs.BBoardID) (err error) {
count, err := loadManArticlesCore(boardID, "")
if err != nil {
logrus.Errorf("cron.loadManArticles: unable to loadManArticles: e: %v", err)
return err
}
logrus.Infof("cron.loadManArticles: bid: %v count: %v", boardID, count)
return nil
}
func loadManArticlesCore(boardID bbs.BBoardID, levelIdx types.ManArticleID) (count int, err error) {
// backend load-general-articles
brdname := boardID.ToBrdname()
ctx := context.Background()
req := &mand.ListRequest{
BoardName: brdname,
Path: string(levelIdx),
}
resp, err := mand.Cli.List(ctx, req)
if err != nil {
return 0, err
}
entries := resp.Entries
// update to db
updateNanoTS := types.NowNanoTS()
articleSummaries, err := api.DeserializePBManArticlesAndUpdateDB(boardID, levelIdx, entries, updateNanoTS)
if err != nil {
return 0, err
}
count += len(articleSummaries)
// recursively loop through dirs.
for _, each := range articleSummaries {
if !each.IsDir {
continue
}
eachCount, err := loadManArticlesCore(boardID, each.ArticleID)
if err != nil {
logrus.Errorf("cron.loadManArticlesCore: unable to loadManArticlesCore: boardID: %v path: %v each: %v e: %v", boardID, levelIdx, each.ArticleID, err)
}
count += eachCount
}
return count, nil
}