forked from zerodha/dungbeetle
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
237 lines (203 loc) · 6.51 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
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package main
// (c) Kailash Nadh, 2018. https://nadh.in
// MIT License.
import (
"fmt"
"log"
"net/http"
"os"
"strings"
"time"
machinery "github.com/RichardKnop/machinery/v1"
"github.com/RichardKnop/machinery/v1/config"
mlog "github.com/RichardKnop/machinery/v1/log"
"github.com/go-chi/chi"
"github.com/go-chi/chi/middleware"
"github.com/knadh/koanf"
"github.com/knadh/koanf/parsers/toml"
"github.com/knadh/koanf/providers/env"
"github.com/knadh/koanf/providers/file"
"github.com/knadh/koanf/providers/posflag"
"github.com/knadh/sql-jobber/backends"
flag "github.com/spf13/pflag"
// MySQL and Postgres drivers.
_ "github.com/go-sql-driver/mysql"
_ "github.com/lib/pq"
)
const (
dbMySQL = "mysql"
dbPostgres = "postgres"
)
type constants struct {
ResultsDB string
}
type taskFunc func(jobID string, taskName string, ttl int, args ...interface{}) (int64, error)
// Jobber represents a collection of the tooling required to run a job server.
type Jobber struct {
Tasks Tasks
Machinery *machinery.Server
Worker *machinery.Worker
DBs DBs
ResultBackends ResultBackends
Logger *log.Logger
}
// DBConfig represents an SQL database's configuration.
type DBConfig struct {
Type string `mapstructure:"type"`
DSN string `mapstructure:"dsn"`
MaxIdleConns int `mapstructure:"max_idle"`
MaxActiveConns int `mapstructure:"max_active"`
ConnectTimeout time.Duration `mapstructure:"connect_timeout"`
}
var (
buildString = "unknown"
sysLog = log.New(os.Stdout, "JOBBER: ", log.Ldate|log.Ltime|log.Lshortfile)
ko = koanf.New(".")
jobber = &Jobber{
Tasks: make(Tasks),
DBs: make(DBs),
ResultBackends: make(ResultBackends),
Logger: sysLog,
}
)
func init() {
// Command line flags.
f := flag.NewFlagSet("config", flag.ContinueOnError)
f.Usage = func() {
sysLog.Println("SQL Jobber")
sysLog.Println(f.FlagUsages())
os.Exit(0)
}
f.String("config", "config.toml", "Path to the TOML configuration file")
f.String("server", "127.0.0.1:6060", "Web server address")
f.StringSlice("sql-directory", []string{"./sql"}, "Path to directory with .sql scripts. Can be specified multiple times")
f.String("queue", "default_queue", "Name of the job queue to accept jobs from")
f.String("worker-name", "sqljobber", "Name of this worker instance")
f.Int("worker-concurrency", 10, "Number of concurrent worker threads to run")
f.Bool("worker-only", false, "Don't start the HTTP server and run in worker-only mode?")
f.Bool("version", false, "Current version and build")
f.Parse(os.Args[1:])
// Load commandline params.
ko.Load(posflag.Provider(f, ".", ko), nil)
// Display version.
if ko.Bool("version") {
fmt.Println(buildString)
os.Exit(0)
}
// Load the config file.
sysLog.Printf("reading config: %s", ko.String("config"))
if err := ko.Load(file.Provider(ko.String("config")), toml.Parser()); err != nil {
sysLog.Printf("error reading config: %v", err)
}
// Load environment variables and merge into the loaded config.
if err := ko.Load(env.Provider("SQL_JOBBER", ".", func(s string) string {
return strings.Replace(
strings.ToLower(strings.TrimPrefix(s, "SQL_JOBBER")), "__", ".", -1)
}), nil); err != nil {
sysLog.Fatalf("error loading config from env: %v", err)
}
// Override Machinery's default logger.
mlog.Set(log.New(os.Stdout, "MACHIN: ", log.Ldate|log.Ltime|log.Lshortfile))
}
func main() {
mode := "default"
if ko.Bool("worker-only") {
mode = "worker only"
}
sysLog.Printf("starting server %s (queue = %s) in %s mode",
ko.String("worker-name"),
ko.String("queue"),
mode)
// Source and result backend DBs.
var (
dbs map[string]DBConfig
resDBs map[string]DBConfig
)
ko.Unmarshal("db", &dbs)
ko.Unmarshal("results", &resDBs)
// There should be at least one DB.
if len(dbs) == 0 {
sysLog.Fatal("found 0 source databases in config")
}
if len(resDBs) == 0 {
sysLog.Fatal("found 0 result backends in config")
}
// Connect to source DBs.
for dbName, cfg := range dbs {
sysLog.Printf("connecting to source %s DB %s", cfg.Type, dbName)
conn, err := connectDB(cfg)
if err != nil {
log.Fatal(err)
}
jobber.DBs[dbName] = conn
}
// Connect to backend DBs.
for dbName, cfg := range resDBs {
sysLog.Printf("connecting to result backend %s DB %s", cfg.Type, dbName)
conn, err := connectDB(cfg)
if err != nil {
log.Fatal(err)
}
// Create a new backend instance.
backend, err := backends.NewSQLBackend(conn,
cfg.Type,
ko.String(fmt.Sprintf("results.%s.results_table", dbName)),
sysLog)
if err != nil {
log.Fatalf("error initializing result backend: %v", err)
}
jobber.ResultBackends[dbName] = backend
}
// Parse and load SQL queries.
for _, d := range ko.Strings("sql-directory") {
sysLog.Printf("loading SQL queries from directory: %s", d)
tasks, err := loadSQLTasks(d, jobber.DBs, jobber.ResultBackends, ko.String("queue"))
if err != nil {
sysLog.Fatal(err)
}
for t, q := range tasks {
if _, ok := jobber.Tasks[t]; ok {
sysLog.Fatalf("duplicate task %s", t)
}
jobber.Tasks[t] = q
}
sysLog.Printf("loaded %d SQL queries from %s", len(tasks), d)
}
sysLog.Printf("loaded %d tasks in total", len(jobber.Tasks))
// Bind the server HTTP endpoints.
r := chi.NewRouter()
r.Use(middleware.RequestLogger(&middleware.DefaultLogFormatter{
Logger: log.New(os.Stdout, "HTTP: ", log.Ldate|log.Ltime)}))
r.Get("/", func(w http.ResponseWriter, r *http.Request) {
sendResponse(w, "welcome!")
})
r.Get("/tasks", handleGetTasksList)
r.Post("/tasks/{taskName}/jobs", handlePostJob)
r.Get("/jobs/{jobID}", handleGetJobStatus)
r.Delete("/jobs/{jobID}", handleDeleteJob)
r.Get("/jobs/queue/{queue}", handleGetPendingJobs)
r.Post("/groups", handlePostJobGroup)
r.Get("/groups/{groupID}", handleGetGroupStatus)
// Setup the job server.
var err error
jobber.Machinery, err = connectJobServer(jobber, &config.Config{
Broker: ko.String("machinery.broker_address"),
DefaultQueue: ko.String("queue"),
ResultBackend: ko.String("machinery.state_address"),
ResultsExpireIn: ko.Int("result_backend.results_ttl"),
}, jobber.Tasks)
if err != nil {
log.Fatal(err)
}
// Start the HTTP server.
if !ko.Bool("worker-only") {
sysLog.Printf("starting HTTP server on %s", ko.String("server"))
go func() {
sysLog.Println(http.ListenAndServe(ko.String("server"), r))
os.Exit(0)
}()
}
jobber.Worker = jobber.Machinery.NewWorker(ko.String("worker-name"),
ko.Int("worker-concurrency"))
jobber.Worker.Launch()
}