-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathredis.go
More file actions
139 lines (133 loc) · 3.17 KB
/
redis.go
File metadata and controls
139 lines (133 loc) · 3.17 KB
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
package main
import (
"encoding/json"
"strconv"
"strings"
"time"
"github.com/gomodule/redigo/redis"
log "github.com/sirupsen/logrus"
)
func (task *Task) cleanInfo() {
var conn redis.Conn
defer func() {
task.closeRedisConn(conn)
}()
conn = task.redisPool.Get()
_, _ = redis.String(conn.Do("del", "cursor:"+task.ID))
_, _ = redis.String(conn.Do("del", "nil_list:"+task.ID))
_, _ = redis.String(conn.Do("del", "fail_list:"+task.ID))
}
func (task *Task) getCursor() (int, int) {
var conn redis.Conn
defer func() {
task.closeRedisConn(conn)
}()
conn = task.redisPool.Get()
replay, err := redis.String(conn.Do("get", "cursor:"+task.ID))
if err != nil {
return -1, -1
} else {
cursor := strings.Split(replay, ":")
zoom, err := strconv.ParseInt(cursor[0], 10, 64)
if err != nil {
return -1, -1
}
col, err := strconv.ParseInt(cursor[1], 10, 64)
if err != nil {
return -1, -1
}
return int(zoom), int(col)
}
}
func (task *Task) saveCursor() {
var conn redis.Conn
defer func() {
task.closeRedisConn(conn)
}()
conn = task.redisPool.Get()
_, err := redis.String(conn.Do("set", "cursor:"+task.ID, strconv.Itoa(task.CurZoom)+":"+strconv.Itoa(task.CurCol)))
if err != nil {
log.Errorf("redis save cursor failure")
}
}
func (task *Task) saveFailedToRedis(batch []Tile) {
for _, tile := range batch {
task.errToRedis(TileXyz{X: tile.X, Y: tile.Y, Z: tile.Z}, "save failure")
}
}
func (task *Task) closeRedisConn(conn redis.Conn) {
err := conn.Close()
if err != nil {
log.Errorf("redis connection close failure")
}
}
func (task *Task) errToRedis(tile TileXyz, res string) {
var conn redis.Conn
defer func() {
task.closeRedisConn(conn)
}()
conn = task.redisPool.Get()
et := ErrTile{
X: tile.X,
Y: tile.Y,
Z: tile.Z,
Res: res,
}
key := "tile_" + strconv.Itoa(et.X) + "_" + strconv.Itoa(et.Y) + "_" + strconv.Itoa(et.Z)
val, _ := json.Marshal(et)
if res == "nil tile" || res == "resp 404" {
replay, err := redis.Int64(conn.Do("hset", "nil_list:"+task.ID, key, val))
if err != nil && replay < 0 {
log.Errorf("redis save tile failure")
}
} else {
replay, err := redis.Int64(conn.Do("hset", "fail_list:"+task.ID, key, val))
if err != nil && replay < 0 {
log.Errorf("redis save tile failure")
}
}
}
func (task *Task) cleanFail(t TileXyz) {
var conn redis.Conn
defer func() {
task.closeRedisConn(conn)
}()
conn = task.redisPool.Get()
key := "tile_" + strconv.Itoa(t.X) + "_" + strconv.Itoa(t.Y) + "_" + strconv.Itoa(t.Z)
_, err := redis.Int64(conn.Do("hdel", "fail_list:"+task.ID, key))
if err != nil {
return
}
}
func (task *Task) retry() {
var conn redis.Conn
var alls map[string]string
defer func() {
task.closeRedisConn(conn)
}()
conn = task.redisPool.Get()
alls, err := redis.StringMap(conn.Do("hgetall", "fail_list:"+task.ID))
if err != nil {
return
}
var count = 0
for kv := range alls {
var te ErrTile
err = json.Unmarshal([]byte(alls[kv]), &te)
if err != nil {
continue
}
if count > 0 && count%100 == 0 {
time.Sleep(time.Second * 2)
}
tile := TileXyz{
X: te.X,
Y: te.Y,
Z: te.Z,
}
count++
task.workers <- tile
task.wg.Add(1)
go task.tileFetcher(tile, task.TileMap.URL, true)
}
}