forked from temporalio/temporalite-archived
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.go
171 lines (146 loc) · 5 KB
/
server.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
// 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 temporalite
import (
"context"
"fmt"
"os"
"path/filepath"
"strings"
"time"
"go.temporal.io/sdk/client"
"go.temporal.io/server/common/authorization"
"go.temporal.io/server/common/config"
"go.temporal.io/server/schema/sqlite"
"go.temporal.io/server/temporal"
"github.com/temporalio/temporalite/internal/liteconfig"
)
// Server wraps temporal.Server.
type Server struct {
internal temporal.Server
ui liteconfig.UIServer
frontendHostPort string
config *liteconfig.Config
}
type ServerOption interface {
apply(*liteconfig.Config)
}
// NewServer returns a new instance of Server.
func NewServer(opts ...ServerOption) (*Server, error) {
c, err := liteconfig.NewDefaultConfig()
if err != nil {
return nil, err
}
for _, opt := range opts {
opt.apply(c)
}
for pragma := range c.SQLitePragmas {
if _, ok := liteconfig.SupportedPragmas[strings.ToLower(pragma)]; !ok {
return nil, fmt.Errorf("ERROR: unsupported pragma %q, %v allowed", pragma, liteconfig.GetAllowedPragmas())
}
}
cfg := liteconfig.Convert(c)
sqlConfig := cfg.Persistence.DataStores[liteconfig.PersistenceStoreName].SQL
if !c.Ephemeral {
// Apply migrations if file does not already exist
if _, err := os.Stat(c.DatabaseFilePath); os.IsNotExist(err) {
// Check if any of the parent dirs are missing
dir := filepath.Dir(c.DatabaseFilePath)
if _, err := os.Stat(dir); err != nil {
return nil, fmt.Errorf("error setting up schema: %w", err)
}
if err := sqlite.SetupSchema(sqlConfig); err != nil {
return nil, fmt.Errorf("error setting up schema: %w", err)
}
}
}
// Pre-create namespaces
var namespaces []*sqlite.NamespaceConfig
for _, ns := range c.Namespaces {
namespaces = append(namespaces, sqlite.NewNamespaceConfig(cfg.ClusterMetadata.CurrentClusterName, ns, false))
}
if err := sqlite.CreateNamespaces(sqlConfig, namespaces...); err != nil {
return nil, fmt.Errorf("error creating namespaces: %w", err)
}
authorizer, err := authorization.GetAuthorizerFromConfig(&cfg.Global.Authorization)
if err != nil {
return nil, fmt.Errorf("unable to instantiate authorizer: %w", err)
}
claimMapper, err := authorization.GetClaimMapperFromConfig(&cfg.Global.Authorization, c.Logger)
if err != nil {
return nil, fmt.Errorf("unable to instantiate claim mapper: %w", err)
}
serverOpts := []temporal.ServerOption{
temporal.WithConfig(cfg),
temporal.ForServices(temporal.Services),
temporal.WithLogger(c.Logger),
temporal.WithAuthorizer(authorizer),
temporal.WithClaimMapper(func(cfg *config.Config) authorization.ClaimMapper {
return claimMapper
}),
}
if len(c.DynamicConfig) > 0 {
// To prevent having to code fall-through semantics right now, we currently
// eagerly fail if dynamic config is being configured in two ways
if cfg.DynamicConfigClient != nil {
return nil, fmt.Errorf("unable to have file-based dynamic config and individual dynamic config values")
}
serverOpts = append(serverOpts, temporal.WithDynamicConfigClient(c.DynamicConfig))
}
if len(c.UpstreamOptions) > 0 {
serverOpts = append(serverOpts, c.UpstreamOptions...)
}
srv, err := temporal.NewServer(serverOpts...)
if err != nil {
return nil, fmt.Errorf("unable to instantiate server: %w", err)
}
s := &Server{
internal: srv,
ui: c.UIServer,
frontendHostPort: cfg.PublicClient.HostPort,
config: c,
}
return s, nil
}
// Start temporal server.
func (s *Server) Start() error {
go func() {
if err := s.ui.Start(); err != nil {
panic(err)
}
}()
return s.internal.Start()
}
// Stop the server.
func (s *Server) Stop() {
s.ui.Stop()
s.internal.Stop()
}
// NewClient initializes a client ready to communicate with the Temporal
// server in the target namespace.
func (s *Server) NewClient(ctx context.Context, namespace string) (client.Client, error) {
return s.NewClientWithOptions(ctx, client.Options{Namespace: namespace})
}
// NewClientWithOptions is the same as NewClient but allows further customization.
//
// To set the client's namespace, use the corresponding field in client.Options.
//
// Note that the HostPort and ConnectionOptions fields of client.Options will always be overridden.
func (s *Server) NewClientWithOptions(ctx context.Context, options client.Options) (client.Client, error) {
options.HostPort = s.frontendHostPort
return client.NewClient(options)
}
// FrontendHostPort returns the host:port for this server.
//
// When constructing a Temporalite client from within the same process,
// NewClient or NewClientWithOptions should be used instead.
func (s *Server) FrontendHostPort() string {
return s.frontendHostPort
}
func timeoutFromContext(ctx context.Context, defaultTimeout time.Duration) time.Duration {
if deadline, ok := ctx.Deadline(); ok {
return deadline.Sub(time.Now())
}
return defaultTimeout
}