-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworker.go
82 lines (69 loc) · 1.87 KB
/
worker.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
package main
import (
"fmt"
"time"
"database/sql"
)
func NewWorker(id int, queue chan chan Message) Worker {
worker := Worker{
ID: id,
Messages: make(chan Message),
WorkerQueue: queue,
QuitChan: make(chan bool),
}
return worker
}
type Worker struct {
ID int
Messages chan Message
WorkerQueue chan chan Message
QuitChan chan bool
}
func (w *Worker) Start(db *sql.DB) {
go func(){
for {
// Add itself (a single worker's work queue) to the overall queue of Workers
w.WorkerQueue <- w.Messages
select {
case msg := <- w.Messages:
// ******** WHERE IT IS DIFFERENT
fmt.Printf("worker%d: Doing work on %s", w.ID, msg.Platform)
fmt.Println(time.Now())
if msg.Platform == "linkedin" {
respBody, lrs := msg.LinkedIn()
if lrs.UpdateKey != "" {
queryStr := `UPDATE posts SET posted = true where message = '` + msg.MessageURL + `' and token = '` + msg.Token + `'`
_, err := db.Exec(queryStr)
if err != nil {
fmt.Println(err)
}
} else {
fmt.Println(string(respBody))
}
} else if msg.Platform == "facebook" {
respBody, frs := msg.Facebook()
if frs.Id != "" {
queryStr := `UPDATE posts SET posted = true where message = '` + msg.MessageURL + `' and token = '` + msg.Token + `'`
_, err := db.Exec(queryStr)
if err != nil {
fmt.Println(err)
}
} else {
fmt.Println(string(respBody))
}
}
time.Sleep(time.Second * 5)
fmt.Println(time.Now())
case <- w.QuitChan:
// we've been told to discontinue
fmt.Printf("worker%d: STOPPING", w.ID)
return
}
}
}()
}
func (w *Worker) Stop() {
go func() {
w.QuitChan <- true
}()
}