/
main.go
executable file
·138 lines (109 loc) · 3.17 KB
/
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
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
package main
import (
"flag"
"fmt"
"log"
"net"
"os"
"sync"
"time"
"context"
"golang.org/x/sync/errgroup"
"google.golang.org/grpc"
pb_svc_provision "github.com/aglide100/dak-keyword/pb/svc/provision"
"github.com/aglide100/dak-keyword/pkg/container"
"github.com/aglide100/dak-keyword/pkg/db"
"github.com/aglide100/dak-keyword/pkg/servers/provision"
)
var (
provisionedAddr = flag.String("provision grpc addr", "0.0.0.0:50012", "grpc address")
maxConcurrencyContainer = flag.Int("Max Concurrency Container", 4, "concurrency for container")
)
func main() {
if err := realMain(); err != nil {
log.Printf("err :%v", err)
os.Exit(1)
}
}
func realMain() error {
flag.Parse()
provisionedL, err := net.Listen("tcp", fmt.Sprintf("%s", *provisionedAddr))
if err != nil {
return err
}
defer provisionedL.Close()
var wait sync.WaitGroup
wait.Add(1)
var opts []grpc.ServerOption
// twitterToken := config.GetInstance().GetTwitterBearerToken()
grpcServer := grpc.NewServer(opts...)
q := container.NewContainerQueue(400, *maxConcurrencyContainer)
c, err := container.NewController(q)
if err != nil {
return err
}
dbConfig, err := db.GetDBConfig()
if err != nil {
return err
}
provisionSrv := provision.NewProvisionServiceServer(c, q, *dbConfig)
pb_svc_provision.RegisterProvisionServer(grpcServer, provisionSrv)
wg, ctx := errgroup.WithContext(context.Background())
_ = ctx
wg.Go(func() error {
log.Printf("Starting normal grpcServer at: %s" ,*provisionedAddr)
err := grpcServer.Serve(provisionedL)
if err != nil {
log.Fatalf("failed to serve: %v", err)
return err
}
return nil
})
wg.Go(func() error {
for {
<-time.After(10*time.Second)
for (q.LenQueue() > 0) {
if (q.LenRunning() < q.GetLimitContainerCount()) {
cSpec:= q.DequeueFromQueue()
if (cSpec.Type == "Scraper") {
err, countErr := c.CreateScraperService(cSpec.WorkerId, cSpec.JobId, cSpec.Keyword, cSpec.InjectString, dbConfig)
if err != nil {
log.Printf("Can't make scraper in queue %v, %v", err, cSpec)
// q.EnqueueFromQueue(cSpec)
}
if (countErr) {
log.Printf("Container Queue is full.. %d \n", q.LenRunning())
q.EnqueueFromQueue(cSpec)
time.Sleep(5*time.Second)
}
}
if (cSpec.Type == "Analyzer") {
err, countErr := c.CreateAnalyzerService(cSpec.WorkerId, cSpec.JobId, cSpec.Keyword, dbConfig)
if err != nil {
log.Printf("Can't make analyzer in queue %v, %v", err, cSpec)
// q.EnqueueFromQueue(cSpec)
}
if (countErr) {
log.Printf("Container queue is full.. %d \n", q.LenRunning())
q.EnqueueFromQueue(cSpec)
time.Sleep(5*time.Second)
}
}
if (cSpec.Type == "Similarity") {
err, countErr := c.CreateSimilarityService(cSpec.WorkerId, cSpec.JobId, dbConfig)
if err != nil {
log.Printf("Can't make similarity in queue %v, %v", err, cSpec)
// q.EnqueueFromQueue(cSpec)
}
if (countErr) {
log.Printf("Container queue is full.. %d \n", q.LenRunning())
q.EnqueueFromQueue(cSpec)
time.Sleep(5*time.Second)
}
}
}
}
}
})
return wg.Wait()
}