-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
136 lines (124 loc) · 2.82 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
package main
import (
"github.com/prep/beanstalk"
"context"
"log"
"fmt"
"encoding/json"
"bytes"
"io"
"time"
"os"
"strings"
)
func main() {
pool := NewEnginePool(10)
env := getEnvironment()
host := env["beanstalkd_host"]
if host == "" {
host = "localhost"
}
port := env["beanstalkd_port"]
if port == "" {
port = "11300"
}
tuneName := env["beanstalkd_tube"]
if tuneName == "" {
tuneName = "face-recognize"
}
consumer, err := beanstalk.NewConsumer([]string{host + ":" + port}, []string{tuneName}, beanstalk.Config{
// Multiply the list of URIs to create a larger pool of connections.
Multiply: 3,
// NumGoroutines is the number of goroutines that the Receive method will
// spin up to process jobs concurrently.
NumGoroutines: 30,
})
if err != nil {
// handle error
log.Fatal(err)
}
producer, err := beanstalk.NewProducer([]string{host + ":" + port}, beanstalk.Config{
// Multiply the list of URIs to create a larger pool of connections.
Multiply: 3,
})
if err != nil {
log.Fatal(err)
}
defer producer.Stop()
params := beanstalk.PutParams{Priority: 1024, Delay: 0, TTR: 30 * time.Second}
type Message struct {
id int
filepath string
}
type Result struct {
id int
feature []byte
}
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
for {
consumer.Receive(ctx, func(ctx context.Context, job *beanstalk.Job) {
// handle job
dec := json.NewDecoder(bytes.NewReader(job.Body))
var m Message
for {
if err := dec.Decode(&m); err == io.EOF {
break
} else if err != nil {
log.Println(err)
break
}
log.Println(job.ID, m.filepath)
handle := pool.Get()
featureBytes := extract(m.filepath, handle)
//convert
result := Result{id: m.id, feature: featureBytes}
resultStr, err := json.Marshal(result)
id, err := producer.Put(ctx, tuneName, resultStr, params)
log.Println("send result back", id)
if err != nil {
log.Println(err)
}
pool.Put(handle)
}
if err := job.Delete(ctx); err != nil {
log.Println(err)
}
})
//runtime.Gosched()
}
}
func Dec2Hex(n int64) string {
if n < 0 {
log.Println("Decimal to hexadecimal error: the argument must be greater than zero.")
return ""
}
if n == 0 {
return "00"
}
hex := map[int64]int64{10: 65, 11: 66, 12: 67, 13: 68, 14: 69, 15: 70}
s := ""
for q := n; q > 0; q = q / 16 {
m := q % 16
if m > 9 && m < 16 {
m = hex[m]
s = fmt.Sprintf("%v%v", string(m), s)
continue
}
s = fmt.Sprintf("%v%v", m, s)
}
return s
}
func getEnvironment() map[string]string{
var env = map[string]string{}
for _, line:= range os.Environ() {
pair := strings.Split(line, "=")
if len(pair) > 1 {
key := strings.Trim(pair[0], " ")
if(len(key) > 0) {
env[key] = strings.Trim(pair[1], " ")
}
}
}
return env
}