Skip to content
This repository has been archived by the owner on Apr 3, 2024. It is now read-only.

Commit

Permalink
move server library to top level package
Browse files Browse the repository at this point in the history
  • Loading branch information
jlegrone committed Sep 11, 2021
1 parent 0d97753 commit 793d9ed
Show file tree
Hide file tree
Showing 8 changed files with 77 additions and 86 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ Features that align with this goal:
Build from source using [go install](https://golang.org/ref/mod#go-install):

```bash
go install github.com/DataDog/temporalite@latest
go install github.com/DataDog/temporalite/cmd/temporalite@latest
```

Start Temporal server:
Expand Down
14 changes: 7 additions & 7 deletions main.go → cmd/temporalite/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@ import (
"go.uber.org/zap"
"go.uber.org/zap/zapcore"

"github.com/DataDog/temporalite"
_ "github.com/DataDog/temporalite/internal/common/persistence/sql/sqlplugin/sqlite" // needed to load sqlite plugin
"github.com/DataDog/temporalite/internal/liteconfig"
"github.com/DataDog/temporalite/server"
)

var (
Expand Down Expand Up @@ -92,12 +92,12 @@ func buildCLI() *cli.App {
return nil
},
Action: func(c *cli.Context) error {
opts := []server.Option{
server.WithFrontendPort(c.Int(portFlag)),
server.WithDatabaseFilePath(c.String(dbPathFlag)),
opts := []temporalite.ServerOption{
temporalite.WithFrontendPort(c.Int(portFlag)),
temporalite.WithDatabaseFilePath(c.String(dbPathFlag)),
}
if c.Bool(ephemeralFlag) {
opts = append(opts, server.WithPersistenceDisabled())
opts = append(opts, temporalite.WithPersistenceDisabled())
}
if c.String(logFormatFlag) == "pretty" {
lcfg := zap.NewDevelopmentConfig()
Expand All @@ -110,10 +110,10 @@ func buildCLI() *cli.App {
return err
}
logger := tlog.NewZapLogger(l)
opts = append(opts, server.WithLogger(logger))
opts = append(opts, temporalite.WithLogger(logger))
}

s, err := server.New(opts...)
s, err := temporalite.NewServer(opts...)
if err != nil {
return err
}
Expand Down
12 changes: 6 additions & 6 deletions internal/liteconfig/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func NewDefaultConfig() (*Config, error) {
return &Config{
Ephemeral: false,
DatabaseFilePath: filepath.Join(userConfigDir, "temporalite/db/default.db"),
FrontendPort: 7233,
FrontendPort: 0,
DynamicPorts: false,
Namespaces: nil,
DefaultNamespaceRetentionPeriod: 24 * time.Hour,
Expand All @@ -61,7 +61,6 @@ func Convert(cfg *Config) *config.Config {
if err := cfg.portProvider.close(); err != nil {
panic(err)
}
// time.Sleep(5 * time.Second)
}()

sqliteConfig := config.SQL{
Expand All @@ -80,16 +79,17 @@ func Convert(cfg *Config) *config.Config {
sqliteConfig.ConnectAttributes["preCreateNamespaces"] = strings.Join(cfg.Namespaces, ",")
}

var (
metricsPort = cfg.FrontendPort + 200
pprofPort = cfg.FrontendPort + 201
)
var metricsPort, pprofPort int
if cfg.DynamicPorts {
if cfg.FrontendPort == 0 {
cfg.FrontendPort = cfg.portProvider.mustGetFreePort()
}
metricsPort = cfg.portProvider.mustGetFreePort()
pprofPort = cfg.portProvider.mustGetFreePort()
} else {
cfg.FrontendPort = 7233
metricsPort = cfg.FrontendPort + 200
pprofPort = cfg.FrontendPort + 201
}

return &config.Config{
Expand Down
21 changes: 11 additions & 10 deletions server/options.go → options.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,52 +2,53 @@
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

package server
package temporalite

import (
"github.com/DataDog/temporalite/internal/liteconfig"

"go.temporal.io/server/common/log"

"github.com/DataDog/temporalite/internal/liteconfig"
)

// WithLogger overrides the default logger.
func WithLogger(logger log.Logger) Option {
func WithLogger(logger log.Logger) ServerOption {
return newApplyFuncContainer(func(cfg *liteconfig.Config) {
cfg.Logger = logger
})
}

// WithDatabaseFilePath persists state to the file at the specified path.
func WithDatabaseFilePath(filepath string) Option {
func WithDatabaseFilePath(filepath string) ServerOption {
return newApplyFuncContainer(func(cfg *liteconfig.Config) {
cfg.Ephemeral = false
cfg.DatabaseFilePath = filepath
})
}

// WithPersistenceDisabled disables file persistence and uses the in-memory storage driver. State will be reset on each process restart.
func WithPersistenceDisabled() Option {
func WithPersistenceDisabled() ServerOption {
return newApplyFuncContainer(func(cfg *liteconfig.Config) {
cfg.Ephemeral = true
})
}

// WithFrontendPort sets the listening port for the temporal-frontend GRPC service.
func WithFrontendPort(port int) Option {
// When unspecified, the default port number of 7233 is used.
func WithFrontendPort(port int) ServerOption {
return newApplyFuncContainer(func(cfg *liteconfig.Config) {
cfg.FrontendPort = port
})
}

// WithDynamicPorts starts Temporal on any available ports.
func WithDynamicPorts() Option {
// WithDynamicPorts starts Temporal on system-chosen ports.
func WithDynamicPorts() ServerOption {
return newApplyFuncContainer(func(cfg *liteconfig.Config) {
cfg.DynamicPorts = true
})
}

// WithNamespaces registers each namespace on Temporal start.
func WithNamespaces(namespaces ...string) Option {
func WithNamespaces(namespaces ...string) ServerOption {
return newApplyFuncContainer(func(cfg *liteconfig.Config) {
cfg.Namespaces = append(cfg.Namespaces, namespaces...)
})
Expand Down
10 changes: 7 additions & 3 deletions server/server.go → server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

package server
package temporalite

import (
"context"
Expand All @@ -22,18 +22,20 @@ import (
"github.com/DataDog/temporalite/internal/liteconfig"
)

// Server wraps a temporal.Server.
type Server struct {
internal *temporal.Server
frontendHostPort string
config *liteconfig.Config
setupWaitGroup sync.WaitGroup
}

type Option interface {
type ServerOption interface {
apply(*liteconfig.Config)
}

func New(opts ...Option) (*Server, error) {
// NewServer returns a new instance of Server.
func NewServer(opts ...ServerOption) (*Server, error) {
c, err := liteconfig.NewDefaultConfig()
if err != nil {
return nil, err
Expand Down Expand Up @@ -73,6 +75,7 @@ func New(opts ...Option) (*Server, error) {
return s, nil
}

// Start temporal server.
func (s *Server) Start() error {
if len(s.config.Namespaces) > 0 {
go func() {
Expand Down Expand Up @@ -124,6 +127,7 @@ func (s *Server) Start() error {
return s.internal.Start()
}

// Stop the server.
func (s *Server) Stop() {
s.internal.Stop()
}
Expand Down
49 changes: 0 additions & 49 deletions server/temporaltest/server_test.go

This file was deleted.

19 changes: 11 additions & 8 deletions server/temporaltest/server.go → temporaltest/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,13 @@ import (
"go.temporal.io/server/common/log"
"go.uber.org/zap"

"github.com/DataDog/temporalite/server"
"github.com/DataDog/temporalite"
)

// A TestServer is a Temporal server listening on a system-chosen port on the
// local loopback interface, for use in end-to-end tests.
type TestServer struct {
server *server.Server
server *temporalite.Server
defaultTestNamespace string
defaultClient client.Client
clients []client.Client
Expand Down Expand Up @@ -63,16 +65,17 @@ func (ts *TestServer) Stop() {
ts.server.Stop()
}

// NewServer starts and returns a new TestServer. The caller should call Stop
// when finished, to shut it down.
func NewServer() *TestServer {
rand.Seed(time.Now().UnixNano())
testNamespace := fmt.Sprintf("temporaltest-%d", rand.Intn(999999))

s, err := server.New(
server.WithNamespaces(testNamespace),
server.WithPersistenceDisabled(),
server.WithFrontendPort(0),
server.WithDynamicPorts(),
server.WithLogger(log.NewZapLogger(zap.NewNop())),
s, err := temporalite.NewServer(
temporalite.WithNamespaces(testNamespace),
temporalite.WithPersistenceDisabled(),
temporalite.WithDynamicPorts(),
temporalite.WithLogger(log.NewZapLogger(zap.NewNop())),
)
if err != nil {
panic(fmt.Errorf("error creating server: %w", err))
Expand Down
36 changes: 34 additions & 2 deletions server/server_test.go → temporaltest/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
//
// This product includes software developed at Datadog (https://www.datadoghq.com/). Copyright 2021 Datadog, Inc.

package server_test
package temporaltest_test

import (
"context"
Expand All @@ -13,9 +13,41 @@ import (
"go.temporal.io/sdk/worker"

"github.com/DataDog/temporalite/internal/examples/helloworld"
"github.com/DataDog/temporalite/server/temporaltest"
"github.com/DataDog/temporalite/temporaltest"
)

func TestNewServer(t *testing.T) {
// Create test Temporal server and client
ts := temporaltest.NewServer()
c := ts.Client()
defer ts.Stop()

w := worker.New(c, "example", worker.Options{})
helloworld.RegisterWorkflowsAndActivities(w)

if err := w.Start(); err != nil {
t.Fatal(err)
}
defer w.Stop()

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
defer cancel()

wfr, err := c.ExecuteWorkflow(ctx, client.StartWorkflowOptions{TaskQueue: "example"}, helloworld.Greet, "world")
if err != nil {
t.Fatal(err)
}

var resp string
if err := wfr.Get(ctx, &resp); err != nil {
t.Fatal(err)
}

if resp != "Hello world" {
t.Fatalf("unexpected result: %q", resp)
}
}

func BenchmarkRunWorkflow(b *testing.B) {
ts := temporaltest.NewServer()
c := ts.Client()
Expand Down

0 comments on commit 793d9ed

Please sign in to comment.