-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
91 lines (73 loc) · 1.6 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
package main
import (
"encoding/json"
"fmt"
"k9exp/postgres-task-queue/data"
"log"
"net/http"
"time"
)
type RequestPayload struct {
Text string `json:"text"`
Time uint32 `json:"time"`
}
// POST /process
func producer(w http.ResponseWriter, r *http.Request) {
var requestPayload RequestPayload
err := json.NewDecoder(r.Body).Decode(&requestPayload)
if err != nil {
log.Println(err.Error())
w.WriteHeader(http.StatusBadRequest)
return
}
text := requestPayload.Text
time := requestPayload.Time
err = data.InsertTask(text, time)
if err != nil {
log.Println(err.Error())
w.WriteHeader(http.StatusInternalServerError)
return
}
msg := "Task added in the queue\n"
w.Write([]byte(msg))
return
}
func worker(err chan error, worker_id uint16) {
for {
data, e := data.GetTask()
if e != nil {
err <- e
}
if data == nil {
time.Sleep(10 * time.Second)
continue
}
printText(data, worker_id)
time.Sleep(1 * time.Second)
}
}
func printText(data *data.TaskData, worker_id uint16) {
for i := 0; i < int(data.Time); i++ {
fmt.Printf("\ttask: %d, by worker: %d> %s [%d/%d]\n", data.Task_id, worker_id, data.Text, i+1, data.Time)
time.Sleep(500 * time.Millisecond)
}
}
func app(err chan error) {
http.HandleFunc("/producer", producer)
PORT := "4545"
log.Printf("Serving on http://localhost:%s\n", PORT)
err <- http.ListenAndServe(":"+PORT, nil)
}
func main() {
y := data.SetupQueue()
if y != nil {
log.Fatal(y)
}
err := make(chan error, 1)
go app(err)
for i := 0; i < 3; i++ {
go worker(err, uint16(i+1))
}
e := <-err
log.Printf("Got error: %v\n", e.Error())
}