forked from temporalio/temporalite-archived
/
main.go
387 lines (354 loc) · 11.4 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// Unless explicitly stated otherwise all files in this repository are licensed under the MIT License.
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.
package main
import (
"encoding/json"
"fmt"
goLog "log"
"net"
"os"
"path/filepath"
"strings"
"github.com/urfave/cli/v2"
"go.temporal.io/server/common/config"
"go.temporal.io/server/common/dynamicconfig"
"go.temporal.io/server/common/headers"
"go.temporal.io/server/common/log"
"go.temporal.io/server/temporal"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
// Load sqlite storage driver
_ "go.temporal.io/server/common/persistence/sql/sqlplugin/sqlite"
"github.com/hishamk/temporalite"
"github.com/hishamk/temporalite/internal/liteconfig"
)
// Name of the ui-server module, used in tests to verify that it is included/excluded
// as a dependency when building with the `headless` tag enabled.
const uiServerModule = "github.com/temporalio/ui-server/v2"
const (
ephemeralFlag = "ephemeral"
dbPathFlag = "filename"
portFlag = "port"
metricsPortFlag = "metrics-port"
uiPortFlag = "ui-port"
headlessFlag = "headless"
ipFlag = "ip"
uiIPFlag = "ui-ip"
uiCodecEndpointFlag = "ui-codec-endpoint"
logFormatFlag = "log-format"
logLevelFlag = "log-level"
namespaceFlag = "namespace"
pragmaFlag = "sqlite-pragma"
configFlag = "config"
dynamicConfigValueFlag = "dynamic-config-value"
)
type uiConfig struct {
Host string
Port int
TemporalGRPCAddress string
EnableUI bool
CodecEndpoint string
}
func main() {
if err := buildCLI().Run(os.Args); err != nil {
goLog.Fatal(err)
}
}
// These variables are set by GoReleaser using ldflags
var version string
func buildCLI() *cli.App {
defaultCfg, _ := liteconfig.NewDefaultConfig()
if version == "" {
version = "(devel)"
}
app := cli.NewApp()
app.Name = "temporalite"
app.Usage = "An experimental distribution of Temporal that runs as a single process\n\nFind more information at: https://github.com/hishamk/temporalite"
app.Version = fmt.Sprintf("%s (server %s)", version, headers.ServerVersion)
app.Commands = []*cli.Command{
{
Name: "start",
Usage: "Start Temporal server",
ArgsUsage: " ",
Flags: []cli.Flag{
&cli.BoolFlag{
Name: ephemeralFlag,
Value: defaultCfg.Ephemeral,
Usage: "enable the in-memory storage driver **data will be lost on restart**",
},
&cli.StringFlag{
Name: dbPathFlag,
Aliases: []string{"f"},
Value: defaultCfg.DatabaseFilePath,
Usage: "file in which to persist Temporal state",
},
&cli.StringSliceFlag{
Name: namespaceFlag,
Aliases: []string{"n"},
Usage: `specify namespaces that should be pre-created`,
EnvVars: nil,
Value: nil,
},
&cli.IntFlag{
Name: portFlag,
Aliases: []string{"p"},
Usage: "port for the temporal-frontend GRPC service",
Value: liteconfig.DefaultFrontendPort,
},
&cli.IntFlag{
Name: metricsPortFlag,
Usage: "port for the metrics listener",
Value: liteconfig.DefaultMetricsPort,
},
&cli.IntFlag{
Name: uiPortFlag,
Usage: "port for the temporal web UI",
DefaultText: fmt.Sprintf("--port + 1000, eg. %d", liteconfig.DefaultFrontendPort+1000),
},
&cli.BoolFlag{
Name: headlessFlag,
Usage: "disable the temporal web UI",
},
&cli.StringFlag{
Name: ipFlag,
Usage: `IPv4 address to bind the frontend service to instead of localhost`,
EnvVars: nil,
Value: "127.0.0.1",
},
&cli.StringFlag{
Name: uiIPFlag,
Usage: `IPv4 address to bind the web UI to instead of localhost`,
DefaultText: "same as --ip (eg. 127.0.0.1)",
},
&cli.StringFlag{
Name: uiCodecEndpointFlag,
Usage: `UI Remote data converter HTTP endpoint`,
EnvVars: nil,
},
&cli.StringFlag{
Name: logFormatFlag,
Usage: `customize the log formatting (allowed: ["json" "pretty"])`,
EnvVars: nil,
Value: "json",
},
&cli.StringFlag{
Name: logLevelFlag,
Usage: `customize the log level (allowed: ["debug" "info" "warn" "error" "fatal"])`,
EnvVars: nil,
Value: "info",
},
&cli.StringSliceFlag{
Name: pragmaFlag,
Aliases: []string{"sp"},
Usage: fmt.Sprintf("specify sqlite pragma statements in pragma=value format (allowed: %q)", liteconfig.GetAllowedPragmas()),
EnvVars: nil,
Value: nil,
},
&cli.StringFlag{
Name: configFlag,
Aliases: []string{"c"},
Usage: `config dir path`,
EnvVars: []string{config.EnvKeyConfigDir},
Value: "",
},
&cli.StringSliceFlag{
Name: dynamicConfigValueFlag,
Usage: `dynamic config value, as KEY=JSON_VALUE (meaning strings need quotes)`,
},
},
Before: func(c *cli.Context) error {
if c.Args().Len() > 0 {
return cli.Exit("ERROR: start command doesn't support arguments.", 1)
}
if c.IsSet(ephemeralFlag) && c.IsSet(dbPathFlag) {
return cli.Exit(fmt.Sprintf("ERROR: only one of %q or %q flags may be passed at a time", ephemeralFlag, dbPathFlag), 1)
}
// Make sure the default db path exists (user does not specify path explicitly)
if !c.IsSet(dbPathFlag) {
if err := os.MkdirAll(filepath.Dir(c.String(dbPathFlag)), os.ModePerm); err != nil {
return cli.Exit(err.Error(), 1)
}
}
switch c.String(logFormatFlag) {
case "json", "pretty", "noop":
default:
return cli.Exit(fmt.Sprintf("bad value %q passed for flag %q", c.String(logFormatFlag), logFormatFlag), 1)
}
switch c.String(logLevelFlag) {
case "debug", "info", "warn", "error", "fatal":
default:
return cli.Exit(fmt.Sprintf("bad value %q passed for flag %q", c.String(logLevelFlag), logLevelFlag), 1)
}
// Check that ip address is valid
if c.IsSet(ipFlag) && net.ParseIP(c.String(ipFlag)) == nil {
return cli.Exit(fmt.Sprintf("bad value %q passed for flag %q", c.String(ipFlag), ipFlag), 1)
}
if c.IsSet(configFlag) {
cfgPath := c.String(configFlag)
if _, err := os.Stat(cfgPath); os.IsNotExist(err) {
return cli.Exit(fmt.Sprintf("bad value %q passed for flag %q: file not found", c.String(configFlag), configFlag), 1)
}
}
return nil
},
Action: func(c *cli.Context) error {
var (
ip = c.String(ipFlag)
serverPort = c.Int(portFlag)
metricsPort = c.Int(metricsPortFlag)
uiPort = serverPort + 1000
uiIP = ip
uiCodecEndpoint = ""
)
if c.IsSet(uiPortFlag) {
uiPort = c.Int(uiPortFlag)
}
if c.IsSet(uiIPFlag) {
uiIP = c.String(uiIPFlag)
}
if c.IsSet(uiCodecEndpointFlag) {
uiCodecEndpoint = c.String(uiCodecEndpointFlag)
}
pragmas, err := getPragmaMap(c.StringSlice(pragmaFlag))
if err != nil {
return err
}
baseConfig := &config.Config{}
if c.IsSet(configFlag) {
// Temporal server requires a couple of persistence config values to
// be explicitly set or the config loading fails. While these are the
// same values used internally, they are overridden later anyways,
// they are just here to pass validation.
baseConfig.Persistence.DefaultStore = liteconfig.PersistenceStoreName
baseConfig.Persistence.NumHistoryShards = 1
if err := config.Load("temporalite", c.String(configFlag), "", &baseConfig); err != nil {
return err
}
}
interruptChan := make(chan interface{}, 1)
go func() {
if doneChan := c.Done(); doneChan != nil {
s := <-doneChan
interruptChan <- s
} else {
s := <-temporal.InterruptCh()
interruptChan <- s
}
}()
opts := []temporalite.ServerOption{
temporalite.WithDynamicPorts(),
temporalite.WithFrontendPort(serverPort),
temporalite.WithMetricsPort(metricsPort),
temporalite.WithFrontendIP(ip),
temporalite.WithDatabaseFilePath(c.String(dbPathFlag)),
temporalite.WithNamespaces(c.StringSlice(namespaceFlag)...),
temporalite.WithSQLitePragmas(pragmas),
temporalite.WithUpstreamOptions(
temporal.InterruptOn(interruptChan),
),
temporalite.WithBaseConfig(baseConfig),
}
if !c.Bool(headlessFlag) {
frontendAddr := fmt.Sprintf("%s:%d", ip, serverPort)
cfg := &uiConfig{
Host: uiIP,
Port: uiPort,
TemporalGRPCAddress: frontendAddr,
EnableUI: true,
CodecEndpoint: uiCodecEndpoint,
}
opt, err := newUIOption(cfg, c.String(configFlag))
if err != nil {
return err
}
if opt != nil {
opts = append(opts, opt)
}
}
if c.Bool(ephemeralFlag) {
opts = append(opts, temporalite.WithPersistenceDisabled())
}
var logger log.Logger
switch c.String(logFormatFlag) {
case "pretty":
lcfg := zap.NewDevelopmentConfig()
switch c.String(logLevelFlag) {
case "debug":
lcfg.Level.SetLevel(zap.DebugLevel)
case "info":
lcfg.Level.SetLevel(zap.InfoLevel)
case "warn":
lcfg.Level.SetLevel(zap.WarnLevel)
case "error":
lcfg.Level.SetLevel(zap.ErrorLevel)
case "fatal":
lcfg.Level.SetLevel(zap.FatalLevel)
}
lcfg.EncoderConfig.EncodeLevel = zapcore.CapitalColorLevelEncoder
l, err := lcfg.Build(
zap.WithCaller(false),
zap.AddStacktrace(zapcore.ErrorLevel),
)
if err != nil {
return err
}
logger = log.NewZapLogger(l)
case "noop":
logger = log.NewNoopLogger()
default:
logger = log.NewZapLogger(log.BuildZapLogger(log.Config{
Stdout: true,
Level: c.String(logLevelFlag),
OutputFile: "",
}))
}
opts = append(opts, temporalite.WithLogger(logger))
configVals, err := getDynamicConfigValues(c.StringSlice(dynamicConfigValueFlag))
if err != nil {
return err
}
for k, v := range configVals {
opts = append(opts, temporalite.WithDynamicConfigValue(k, v))
}
s, err := temporalite.NewServer(opts...)
if err != nil {
return err
}
if err := s.Start(); err != nil {
return cli.Exit(fmt.Sprintf("Unable to start server. Error: %v", err), 1)
}
return cli.Exit("All services are stopped.", 0)
},
},
}
return app
}
func getPragmaMap(input []string) (map[string]string, error) {
result := make(map[string]string)
for _, pragma := range input {
vals := strings.Split(pragma, "=")
if len(vals) != 2 {
return nil, fmt.Errorf("ERROR: pragma statements must be in KEY=VALUE format, got %q", pragma)
}
result[vals[0]] = vals[1]
}
return result, nil
}
func getDynamicConfigValues(input []string) (map[dynamicconfig.Key][]dynamicconfig.ConstrainedValue, error) {
ret := make(map[dynamicconfig.Key][]dynamicconfig.ConstrainedValue, len(input))
for _, keyValStr := range input {
keyVal := strings.SplitN(keyValStr, "=", 2)
if len(keyVal) != 2 {
return nil, fmt.Errorf("dynamic config value not in KEY=JSON_VAL format")
}
key := dynamicconfig.Key(keyVal[0])
// We don't support constraints currently
var val dynamicconfig.ConstrainedValue
if err := json.Unmarshal([]byte(keyVal[1]), &val.Value); err != nil {
return nil, fmt.Errorf("invalid JSON value for key %q: %w", key, err)
}
ret[key] = append(ret[key], val)
}
return ret, nil
}