-
Notifications
You must be signed in to change notification settings - Fork 8
/
consumer.go
47 lines (39 loc) · 930 Bytes
/
consumer.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
package main
import (
"encoding/json"
"github.com/bitly/go-nsq"
"log"
"os"
)
type Task struct {
Url string `json:"url"`
Target string `json:"target"`
Path string `json:"path"`
Device string `json:"device"`
}
func handle(c *Config) {
r, err := nsq.NewConsumer("topic", "channel", nsq.NewConfig())
if err != nil {
log.Fatal(err.Error())
}
// only support mac browser now.
browser := NewMacBrowser()
storage := NewStorage(c)
r.AddConcurrentHandlers(nsq.HandlerFunc(func(m *nsq.Message) error {
task := &Task{}
json.Unmarshal(m.Body, &task)
log.Println(task)
browser.Snapshot(task.Url, task.Target, c.Timeout)
if _, err := os.Stat(task.Target); task.Path != "" && err == nil {
// storage put
storage.put(task.Path, task.Target)
}
log.Println("task done.")
return nil
}), c.Workers)
err1 := r.ConnectToNSQD(c.NsqAddr)
if err1 != nil {
log.Fatal(err1.Error())
}
<-r.StopChan
}