Skip to content

Commit

Permalink
chore: typo and tiny fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
YuukanOO committed Sep 26, 2024
1 parent bf0f12a commit b0176e9
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 28 deletions.
2 changes: 1 addition & 1 deletion cmd/config/configuration.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ const (
type (
// Configuration used to configure seelf commands.
Configuration interface {
serve.Options // The configuration should provide every settings needed by the seelf server
serve.Options

Initialize(log.ConfigurableLogger, string) error // Initialize the configuration by loading it (from config file, env vars, etc.)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/serve/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,14 @@ func newHttpServer(options ServerOptions, root startup.ServerRoot) *server {
_ = s.router.SetTrustedProxies(nil)

// Configure the session store
store := cookie.NewStore(s.options.Secret())
store := cookie.NewStore(options.Secret())
store.Options(sessions.Options{
Secure: s.options.IsSecure(),
Secure: options.IsSecure(),
HttpOnly: true,
SameSite: http.SameSiteStrictMode,
})

if s.options.IsDebug() {
if options.IsDebug() {
s.router.Use(s.requestLogger)
}

Expand Down
19 changes: 10 additions & 9 deletions cmd/startup/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ type (
}

serverRoot struct {
options ServerOptions
bus bus.Bus
logger log.Logger
db *sqlite.Database
Expand All @@ -61,13 +60,15 @@ type (
// needed by the server.
func Server(options ServerOptions, logger log.Logger) (ServerRoot, error) {
s := &serverRoot{
options: options,
logger: logger,
logger: logger,
}

// embedded.NewBus()
// embedded.NewScheduler()

s.bus = memory.NewBus()

db, err := sqlite.Open(s.options.ConnectionString(), s.logger, s.bus)
db, err := sqlite.Open(options.ConnectionString(), s.logger, s.bus)

if err != nil {
return nil, err
Expand All @@ -81,13 +82,13 @@ func Server(options ServerOptions, logger log.Logger) (ServerRoot, error) {
return nil, err
}

s.scheduler = bus.NewScheduler(s.schedulerStore, s.logger, s.bus, s.options.RunnersPollInterval(),
s.scheduler = bus.NewScheduler(s.schedulerStore, s.logger, s.bus, options.RunnersPollInterval(),
bus.WorkerGroup{
Size: s.options.RunnersDeploymentCount(),
Size: options.RunnersDeploymentCount(),
Messages: []string{deploy.Command{}.Name_()},
},
bus.WorkerGroup{
Size: s.options.RunnersCleanupCount(),
Size: options.RunnersCleanupCount(),
Messages: []string{
cleanup_app.Command{}.Name_(),
delete_app.Command{}.Name_(),
Expand All @@ -105,7 +106,7 @@ func Server(options ServerOptions, logger log.Logger) (ServerRoot, error) {

// Setups deployment infrastructure
if err = deploymentinfra.Setup(
s.options,
options,
s.logger,
s.db,
s.bus,
Expand All @@ -125,7 +126,7 @@ func Server(options ServerOptions, logger log.Logger) (ServerRoot, error) {
}

// Create the target needed to expose seelf itself and manage certificates if needed
if exposedUrl, isSet := s.options.AppExposedUrl().TryGet(); isSet {
if exposedUrl, isSet := options.AppExposedUrl().TryGet(); isSet {
container := exposedUrl.User().Get("")

s.logger.Infow("exposing seelf container using the local target, creating it if needed, the container may restart once done",
Expand Down
8 changes: 4 additions & 4 deletions internal/deployment/infra/artifact/local_artifact_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,15 @@ func (a *localArtifactManager) appPath(appID domain.AppID) string {
return filepath.Join(a.appsDirectory, string(appID))
}

func (a *localArtifactManager) deploymentPath(depl domain.Deployment) (string, error) {
func (a *localArtifactManager) deploymentPath(deployment domain.Deployment) (string, error) {
var w strings.Builder

if err := a.options.DeploymentDirTemplate().Execute(&w, deploymentTemplateData{
Number: depl.ID().DeploymentNumber(),
Environment: depl.Config().Environment(),
Number: deployment.ID().DeploymentNumber(),
Environment: deployment.Config().Environment(),
}); err != nil {
return "", err
}

return filepath.Join(a.appPath(depl.ID().AppID()), w.String()), nil
return filepath.Join(a.appPath(deployment.ID().AppID()), w.String()), nil
}
14 changes: 11 additions & 3 deletions pkg/bus/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"errors"

"github.com/YuukanOO/seelf/pkg/storage"
"github.com/YuukanOO/seelf/pkg/types"
)

var ErrNoHandlerRegistered = errors.New("no_handler_registered")
Expand All @@ -30,7 +31,10 @@ type (
}
)

// Register an handler for a specific request on the provided bus.
// Register an handler for a specific request on the provided bus. You should always
// prefer this registration method.
// If the provided message is an async one, it will be automatically registered on
// the Marshallable mapper to make things easier.
func Register[TResult any, TMsg TypedRequest[TResult]](bus Bus, handler RequestHandler[TResult, TMsg]) {
var (
msg TMsg
Expand All @@ -42,8 +46,12 @@ func Register[TResult any, TMsg TypedRequest[TResult]](bus Bus, handler RequestH
bus.Register(msg, h)

// If the message is schedulable, register the unmarshaller automatically.
if _, isSchedulable := any(msg).(Schedulable); isSchedulable {
Marshallable.Register(msg, func(s string) (Request, error) { return storage.UnmarshalJSON[TMsg](s) })
// This is done here because of the known type TMsg but maybe I should try to
// move it to bus/memory in the future.
if types.Is[Schedulable](msg) {
Marshallable.Register(msg, func(s string) (Request, error) {
return storage.UnmarshalJSON[TMsg](s)
})
}
}

Expand Down
13 changes: 7 additions & 6 deletions pkg/bus/memory/dispatcher.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"

"github.com/YuukanOO/seelf/pkg/bus"
"github.com/YuukanOO/seelf/pkg/types"
)

type (
Expand All @@ -28,12 +29,12 @@ func (b *dispatcher) Register(msg bus.Message, handler bus.NextFunc) {
name := msg.Name_()
_, exists := b.handlers[name]

// Apply middlewares to avoid doing it at runtime
// Apply middlewares here to avoid doing it at runtime
for i := len(b.middlewares) - 1; i >= 0; i-- {
handler = b.middlewares[i](handler)
}

if msg.Kind_() == bus.MessageKindNotification {
if types.Is[bus.Signal](msg) {
if !exists {
b.handlers[name] = []bus.NextFunc{handler}
} else {
Expand Down Expand Up @@ -61,15 +62,15 @@ func (b *dispatcher) Send(ctx context.Context, msg bus.Request) (any, error) {

func (b *dispatcher) Notify(ctx context.Context, msgs ...bus.Signal) error {
for _, msg := range msgs {
handlers := b.handlers[msg.Name_()]
value := b.handlers[msg.Name_()]

if handlers == nil {
if value == nil {
continue
}

hdls := handlers.([]bus.NextFunc)
handlers := value.([]bus.NextFunc)

for _, h := range hdls {
for _, h := range handlers {
_, err := h(ctx, msg)

if err != nil {
Expand Down
2 changes: 0 additions & 2 deletions pkg/bus/scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@ import (
"github.com/YuukanOO/seelf/pkg/storage"
)

var _ Scheduler = (*defaultScheduler)(nil) // Validate interface implementation

const (
JobPolicyRetryPreserveOrder JobPolicy = 1 << iota // Retry the job but preserve the order among the group
JobPolicyWaitForOthersResourceID // Wait for other jobs on the same resource id to finish before processing
Expand Down
2 changes: 2 additions & 0 deletions pkg/storage/discriminated.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ func NewDiscriminatedMapper[T any](
}

// Register a new concrete type available to the mapper.
// It will panic if a type is already registered with the same discriminator since it
// should never happen.
func (m *DiscriminatedMapper[T]) Register(concreteType T, mapper DiscriminatedMapperFunc[T]) {
discriminator := m.extractor(concreteType)

Expand Down

0 comments on commit b0176e9

Please sign in to comment.