/
load_general_boards.go
82 lines (71 loc) · 1.97 KB
/
load_general_boards.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
package cron
import (
"context"
"fmt"
"time"
"github.com/Ptt-official-app/go-openbbsmiddleware/api"
"github.com/Ptt-official-app/go-openbbsmiddleware/schema"
"github.com/Ptt-official-app/go-openbbsmiddleware/types"
"github.com/Ptt-official-app/go-openbbsmiddleware/utils"
pttbbsapi "github.com/Ptt-official-app/go-pttbbs/api"
"github.com/sirupsen/logrus"
)
func RetryLoadGeneralBoards(ctx context.Context) error {
for {
select {
case <-ctx.Done():
return nil
default:
logrus.Infof("RetryLoadGeneralBoards: to LoadGeneralBoards")
_ = LoadGeneralBoards()
select {
case <-ctx.Done():
return nil
default:
logrus.Infof("RetryLoadGeneralBoards: to sleep 1 min")
time.Sleep(1 * time.Minute)
}
}
}
}
func LoadGeneralBoards() (err error) {
nextIdx := ""
count := 0
for {
boardDetails, newNextIdx, err := loadGeneralBoards(nextIdx)
if err != nil {
logrus.Errorf("cron.LoadGeneralBoards: unable to loadGeneralBoards: nextIdx: %v e: %v", nextIdx, err)
return err
}
count += len(boardDetails)
if newNextIdx == "" {
logrus.Infof("cron.LoadGeneralBoards: load %v boards", count)
return nil
}
nextIdx = newNextIdx
}
}
func loadGeneralBoards(startIdx string) (boardDetails []*schema.BoardDetail, nextIdx string, err error) {
// backend load-general-baords
theParams_b := &pttbbsapi.LoadGeneralBoardDetailsParams{
StartIdx: startIdx,
NBoards: N_BOARDS,
Asc: true,
IsSystem: true,
}
var result_b *pttbbsapi.LoadGeneralBoardDetailsResult
statusCode, err := utils.BackendGet(nil, pttbbsapi.LOAD_GENERAL_BOARD_DETAILS_R, theParams_b, nil, &result_b)
if err != nil {
return nil, "", err
}
if statusCode != 200 {
return nil, "", fmt.Errorf("invalid statusCode: statusCode: %v", statusCode)
}
// update to db
updateNanoTS := types.NowNanoTS()
boardDetails, err = api.DeserializeBoardDetailsAndUpdateDB(result_b.Boards, updateNanoTS)
if err != nil {
return nil, "", err
}
return boardDetails, result_b.NextIdx, nil
}