Skip to content

Commit

Permalink
refactor: leaky abstraction from anvil service
Browse files Browse the repository at this point in the history
  • Loading branch information
gligneul committed Dec 12, 2023
1 parent cc1682f commit 5af35f8
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 26 deletions.
1 change: 0 additions & 1 deletion TODO
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
TODO

- Move hardcoded addresses to opts
- Remove cleanup function from anvil (leaky abstraction)
- Add unit tests to model
- Add inspect API
- Add graphql API
Expand Down
58 changes: 42 additions & 16 deletions internal/nonodo/anvil.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package nonodo

import (
"context"
_ "embed"
"fmt"
"log"
Expand All @@ -20,6 +21,47 @@ import (
//go:embed anvil_state.json
var anvilState []byte

// Start the anvil process in the host machine.
type anvilService struct {
ready chan struct{}
command *supervisor.CommandService
cleanup func()
}

func newAnvilService(opts opts.NonodoOpts) *anvilService {
stateFile, cleanup := loadStateFile()
args := []string{
"--port", fmt.Sprint(opts.AnvilPort),
"--block-time", fmt.Sprint(opts.AnvilBlockTime),
"--load-state", stateFile,
}
if !opts.AnvilVerbose {
args = append(args, "--silent")
}
return &anvilService{
ready: make(chan struct{}),
command: supervisor.NewCommandService("anvil", args, nil, opts.AnvilPort),
cleanup: cleanup,
}
}

func (s *anvilService) Start(ctx context.Context) error {
go func() {
// cleanup after the internal service is ready
defer s.cleanup()
select {
case <-s.command.Ready():
s.ready <- struct{}{}
case <-ctx.Done():
}
}()
return s.command.Start(ctx)
}

func (s *anvilService) Ready() <-chan struct{} {
return s.ready
}

// Create a temporary file with the anvil state.
// Return a function to delete this file.
func loadStateFile() (string, func()) {
Expand All @@ -41,19 +83,3 @@ func loadStateFile() (string, func()) {
}
return path, cleanup
}

// Start the anvil process in the host machine.
// Return a close function that should be called when the program finishes.
func newAnvilService(opts opts.NonodoOpts) (supervisor.Service, func()) {
stateFile, cleanup := loadStateFile()
args := []string{
"--port", fmt.Sprint(opts.AnvilPort),
"--block-time", fmt.Sprint(opts.AnvilBlockTime),
"--load-state", stateFile,
}
if !opts.AnvilVerbose {
args = append(args, "--silent")
}
service := supervisor.NewCommandService("anvil", args, nil, opts.AnvilPort)
return service, cleanup
}
11 changes: 2 additions & 9 deletions internal/nonodo/nonodo.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,12 @@ import (
)

func Run(ctx context.Context, opts opts.NonodoOpts) {
model := model.NewNonodoModel()
var services []supervisor.Service

services = append(services, supervisor.NewSignalListenerService())

anvil, cleanup := newAnvilService(opts)
defer cleanup()
services = append(services, anvil)

model := model.NewNonodoModel()
services = append(services, newAnvilService(opts))
rpcEndpoint := fmt.Sprintf("ws://127.0.0.1:%v", opts.AnvilPort)
services = append(services, newInputterService(model, rpcEndpoint))

services = append(services, newEchoService(model, opts.HttpPort))

supervisor.Start(ctx, services)
}

0 comments on commit 5af35f8

Please sign in to comment.