-
Notifications
You must be signed in to change notification settings - Fork 0
/
main3api.go
49 lines (43 loc) · 899 Bytes
/
main3api.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
package main
import (
"fmt"
"log"
"os"
"strconv"
"time"
"github.com/golang-collections/collections/queue"
"github.com/google/uuid"
"github.com/vishalvivekm/cube/task"
"github.com/vishalvivekm/cube/worker"
)
func main() {
host := os.Getenv("CUBE_HOST")
port, _ := strconv.Atoi(os.Getenv("CUBE_PORT"))
fmt.Printf("starting cube worker at %s:%d\n", host, port)
w := worker.Worker {
Queue: *queue.New(),
Db: make(map[uuid.UUID]*task.Task),
}
api := worker.Api{
Address: host,
Port: port,
Worker: &w,
}
go runTasks(&w)
go w.CollectStats()
api.Start()
}
func runTasks(w *worker.Worker) {
for {
if w.Queue.Len() != 0 {
res := w.RunTask()
if res.Error != nil {
log.Printf("error running task: %v\n", res.Error)
}
} else {
log.Printf("no tasks to process currently. \n")
}
log.Println("sleeping for 15 seconds")
time.Sleep(15 * time.Second)
}
}