forked from Shyp/rickover
-
Notifications
You must be signed in to change notification settings - Fork 2
/
example_dequeuer_test.go
84 lines (74 loc) · 2.43 KB
/
example_dequeuer_test.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
// Run the rickover dequeuer. Configure the following environment variables:
//
// DATABASE_URL: Postgres connection string (see Makefile)
// PG_WORKER_POOL_SIZE: Maximum number of database connections from this process
// DOWNSTREAM_URL: Downstream server that can perform the work
// DOWNSTREAM_WORKER_AUTH: Basic Auth password for downstream server (user "jobs")
//
// Create job types by making a POST request to /v1/jobs with the job name and
// concurrency. After that, CreatePools will start and run dequeuers for those
// types.
package rickover
import (
"context"
"fmt"
"os"
"os/signal"
log "github.com/inconshreveable/log15"
"github.com/kevinburke/rickover/config"
"github.com/kevinburke/rickover/dequeuer"
"github.com/kevinburke/rickover/metrics"
"github.com/kevinburke/rickover/models/db"
"github.com/kevinburke/rickover/services"
"golang.org/x/sys/unix"
)
var dbConns int
var downstreamUrl string
var downstreamPassword string
func init() {
var err error
dbConns, err = config.GetInt("PG_WORKER_POOL_SIZE")
if err != nil {
log.Info("Error getting database pool size: %s. Defaulting to 20", err)
dbConns = 20
}
downstreamPassword = os.Getenv("DOWNSTREAM_WORKER_AUTH")
}
func Example_dequeuer() {
ctx, cancel := context.WithCancel(context.Background())
go func() {
sigterm := make(chan os.Signal, 1)
signal.Notify(sigterm, unix.SIGINT, unix.SIGTERM)
sig := <-sigterm
fmt.Printf("Caught signal %v, shutting down...\n", sig)
cancel()
}()
logger := log.New()
downstreamUrl = config.GetURLOrBail("DOWNSTREAM_URL").String()
jp := services.NewJobProcessor(services.NewDownstreamHandler(logger, downstreamUrl, downstreamPassword))
go metrics.Run(context.TODO(), metrics.LibratoConfig{
Namespace: "rickover.dequeuer",
Source: "worker",
Email: "TODO@example.com",
})
srv, err := dequeuer.New(ctx, dequeuer.Config{
Connector: db.DefaultConnection,
Processor: jp,
NumConns: 10,
StuckJobTimeout: dequeuer.DefaultStuckJobTimeout,
})
if err != nil {
log.Info("could not start dequeuer", "err", err)
}
// Run will:
//
// - start all worker pools
// - start a daemon to "fail" stuck jobs after 7 minutes
// - start metrics to monitor in progress jobs, active queries against the
// database, and the depth of the queue.
if err := srv.Run(ctx); err != nil && err != context.Canceled {
log.Error("error running dequeuer", "err", err)
os.Exit(2)
}
log.Info("All pools shut down. Quitting.")
}