~sircmpwn/builds.sr.ht

builds.sr.ht/worker/main.go -rw-r--r-- 2.0 KiB
106c941fDhruvin Gandhi guix: disable host substitute server 2 days ago
                                                                                
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
92
93
94
95
96
97
98
99
100
101
102
103
package main

import (
	"database/sql"
	"flag"
	"log"
	"os"
	"os/signal"
	"runtime"
	"sync"

	"github.com/go-redis/redis"
	"github.com/vaughan0/go-ini"
	"git.sr.ht/~sircmpwn/core-go/crypto"

	_ "github.com/lib/pq"
	celery "github.com/gocelery/gocelery"
)

var (
	config  ini.File
	origin  string
	workers int

	jobs      map[int]*JobContext
	jobsMutex sync.Mutex
)

func main() {
	flag.IntVar(&workers, "workers", runtime.NumCPU(),
		"configure number of workers")
	flag.Parse()

	var err error
	for _, path := range []string{"../config.ini", "/etc/sr.ht/config.ini"} {
		config, err = ini.LoadFile(path)
		if err == nil {
			break
		}
	}
	if err != nil {
		panic(err)
	}
	crypto.InitCrypto(config)

	jobs = make(map[int]*JobContext, 1)

	pgcs := conf("builds.sr.ht", "connection-string")
	db, err := sql.Open("postgres", pgcs)
	if err != nil {
		panic(err)
	}
	if err := db.Ping(); err != nil {
		log.Fatalf("Failed to open a database connection: %v", err)
	}

	origin = conf("builds.sr.ht", "origin")

	clusterRedis := conf("builds.sr.ht", "redis")
	broker := celery.NewRedisCeleryBroker(clusterRedis)
	backend := celery.NewRedisCeleryBackend(clusterRedis)

	client, err := celery.NewCeleryClient(broker, backend, workers)
	if err != nil {
		panic(err)
	}
	redisHost, ok := config.Get("sr.ht", "redis-host")
	if !ok {
		redisHost = "redis://localhost:6379"
	}
	ropts, err := redis.ParseURL(redisHost)
	if err != nil {
		panic(err)
	}
	localRedis := redis.NewClient(ropts)
	if _, err := localRedis.Ping().Result(); err != nil {
		panic(err)
	}

	ctx := &WorkerContext{db, localRedis, conf}
	client.Register("buildsrht.runner.run_build", ctx.RunBuild)

	log.Printf("Starting %d workers...", workers)
	go HttpServer()
	client.StartWorker()
	log.Println("Waiting for tasks.")

	c := make(chan os.Signal, 1)
	signal.Notify(c, os.Interrupt)

	<-c

	log.Println("Waiting for workers to terminate...")
	client.StopWorker()
}

func conf(section string, key string) string {
	value, ok := config.Get(section, key)
	if !ok {
		log.Fatalf("Expected config option [%s]%s", section, key)
	}
	return value
}