-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
72 lines (56 loc) · 1023 Bytes
/
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
package main
import (
"log"
"sync"
"time"
"github.com/fengyfei/moon/models"
"github.com/fengyfei/moon/service/stock"
_ "github.com/mattn/go-sqlite3"
)
const (
workers = 10
intervals = 10000
stop = "stop"
)
var (
dateCh = make(chan string, workers)
lock = sync.Mutex{}
wg = sync.WaitGroup{}
)
func main() {
wg.Add(workers)
for i := 0; i < workers; i++ {
go workerFunc()
}
now := time.Now()
for i := 0; i < intervals; i++ {
t := now.AddDate(0, 0, -i)
date := t.Format("2006-01-02")
dateCh <- date
}
for i := 0; i < workers; i++ {
dateCh <- stop
}
wg.Wait()
}
func workerFunc() {
for {
date := <-dateCh
if date == stop {
wg.Done()
return
}
resp, err := stock.GetNorthDailyReport(date)
if err != nil {
time.Sleep(2 * time.Second)
continue
}
log.Println(resp)
lock.Lock()
if err = models.Record(date, resp); err != nil {
log.Printf("[North] Record error(%#v) for %s", err, date)
}
lock.Unlock()
time.Sleep(2 * time.Second)
}
}