-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
53 lines (45 loc) · 1.11 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
package main
import (
"context"
"flag"
"fmt"
"log"
"net/http"
"os"
"time"
"github.com/cvhariharan/kubernetes-job-queue/jobqueue"
"github.com/google/uuid"
)
func main() {
log.SetFlags(log.Lshortfile)
var worker, exitIfEmpty bool
flag.BoolVar(&worker, "WORKER", false, "Worker node")
flag.BoolVar(&exitIfEmpty, "EXIT", false, "Exit if the queue is empty")
flag.Parse()
queue := jobqueue.NewRedisJobqueue()
if worker {
host, _ := os.Hostname()
log.Println("Starting worker", host)
jobsChan := queue.Subscribe(context.Background(), exitIfEmpty)
for {
job, ok := <-jobsChan
if !ok {
return
}
time.Sleep(5 * time.Second)
log.Println(fmt.Sprintf("Processed job %s by worker %s", job, host))
}
}
port := os.Getenv("PORT")
if port == "" {
port = "8080"
}
pubJobqueue := make(chan string)
go queue.Publish(context.Background(), pubJobqueue)
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
pubJobqueue <- uuid.New().String()
w.WriteHeader(http.StatusAccepted)
})
log.Println("Starting API server on port", port)
log.Fatal(http.ListenAndServe(":"+port, nil))
}