-
Notifications
You must be signed in to change notification settings - Fork 24
/
worker.go
99 lines (86 loc) · 2.47 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
package main
import (
"os"
"os/signal"
"runtime"
"syscall"
"github.com/codegangsta/cli"
"github.com/remind101/conveyor"
"github.com/remind101/conveyor/builder/docker"
"github.com/remind101/conveyor/worker"
)
// flags for the worker.
var workerFlags = []cli.Flag{
cli.IntFlag{
Name: "github.app_id",
Usage: "GitHub App ID. See https://developer.github.com/apps/building-github-apps/authentication-options-for-github-apps/",
EnvVar: "GITHUB_APP_ID",
},
cli.IntFlag{
Name: "github.installation_id",
Usage: "GitHub Installation ID. See https://developer.github.com/apps/building-github-apps/authentication-options-for-github-apps/",
EnvVar: "GITHUB_INSTALLATION_ID",
},
cli.StringFlag{
Name: "github.private_key",
Usage: "Private key for the GitHub App. See https://developer.github.com/apps/building-github-apps/authentication-options-for-github-apps/",
EnvVar: "GITHUB_PRIVATE_KEY",
},
cli.BoolFlag{
Name: "dry",
Usage: "Enable dry run mode.",
EnvVar: "DRY",
},
cli.StringFlag{
Name: "builder.image",
Value: docker.DefaultBuilderImage,
Usage: "A docker image to use to perform the build.",
EnvVar: "BUILDER_IMAGE",
},
cli.StringFlag{
Name: "reporter",
Value: "",
Usage: "The reporter to use to report errors. Available options are `hb://api.honeybadger.io?key=<key>&environment=<environment>",
EnvVar: "REPORTER",
},
cli.IntFlag{
Name: "workers",
Value: runtime.NumCPU(),
Usage: "Number of workers in goroutines to start.",
EnvVar: "WORKERS",
},
cli.StringFlag{
Name: "stats",
Value: "",
Usage: "If provided, defines where build metrics are sent. Available options are dogstatsd://<host>",
EnvVar: "STATS",
},
}
var cmdWorker = cli.Command{
Name: "worker",
Usage: "Run a set of workers.",
Action: workerAction,
Flags: append(sharedFlags, workerFlags...),
}
func workerAction(c *cli.Context) {
cy := newConveyor(c)
if err := runWorker(cy, c); err != nil {
must(err)
}
}
func runWorker(cy *conveyor.Conveyor, c *cli.Context) error {
numWorkers := c.Int("workers")
info("Starting %d workers\n", numWorkers)
ch := make(chan conveyor.BuildContext)
cy.BuildQueue.Subscribe(ch)
workers := worker.NewPool(cy, numWorkers, worker.Options{
Builder: newBuilder(c),
BuildRequests: ch,
})
workers.Start()
quit := make(chan os.Signal, 1)
signal.Notify(quit, os.Interrupt, syscall.SIGTERM)
sig := <-quit
info("Signal %d received. Shutting down workers.\n", sig)
return workers.Shutdown()
}