Skip to content

Commit

Permalink
hack hack HACK
Browse files Browse the repository at this point in the history
  • Loading branch information
stuartwdouglas committed Nov 27, 2024
1 parent e760263 commit 9c0d75f
Show file tree
Hide file tree
Showing 25 changed files with 676 additions and 716 deletions.
32 changes: 22 additions & 10 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,15 +498,27 @@ func (s *Service) UpdateDeploy(ctx context.Context, req *connect.Request[ftlv1.U

logger := s.getDeploymentLogger(ctx, deploymentKey)
logger.Debugf("Update deployment for: %s", deploymentKey)

err = s.dal.SetDeploymentReplicas(ctx, deploymentKey, int(req.Msg.MinReplicas))
if err != nil {
if errors.Is(err, libdal.ErrNotFound) {
logger.Errorf(err, "Deployment not found: %s", deploymentKey)
return nil, connect.NewError(connect.CodeNotFound, errors.New("deployment not found"))
if req.Msg.MinReplicas != nil {
err = s.dal.SetDeploymentReplicas(ctx, deploymentKey, int(*req.Msg.MinReplicas))
if err != nil {
if errors.Is(err, libdal.ErrNotFound) {
logger.Errorf(err, "Deployment not found: %s", deploymentKey)
return nil, connect.NewError(connect.CodeNotFound, errors.New("deployment not found"))
}
logger.Errorf(err, "Could not set deployment replicas: %s", deploymentKey)
return nil, fmt.Errorf("could not set deployment replicas: %w", err)
}
}
if req.Msg.Endpoint != nil {
err = s.dal.SetDeploymentEndpoint(ctx, deploymentKey, *req.Msg.Endpoint)
if err != nil {
if errors.Is(err, libdal.ErrNotFound) {
logger.Errorf(err, "Deployment not found: %s", deploymentKey)
return nil, connect.NewError(connect.CodeNotFound, errors.New("deployment not found"))
}
logger.Errorf(err, "Could not set deployment endpoint: %s", deploymentKey)
return nil, fmt.Errorf("could not set deployment endpoint: %w", err)
}
logger.Errorf(err, "Could not set deployment replicas: %s", deploymentKey)
return nil, fmt.Errorf("could not set deployment replicas: %w", err)
}

return connect.NewResponse(&ftlv1.UpdateDeployResponse{}), nil
Expand Down Expand Up @@ -1648,8 +1660,8 @@ func (s *Service) syncRoutesAndSchema(ctx context.Context) (ret time.Duration, e
deploymentLogger.Debugf("Deployment %s has no runtime metadata", v.Key.String())
continue
}
targetEndpoint := v.Schema.Runtime.Endpoint
if targetEndpoint == "" {
targetEndpoint, ok := v.Endpoint.Get()
if !ok {
deploymentLogger.Debugf("Failed to get updated endpoint for deployment %s", v.Key.String())
continue
} else {
Expand Down
17 changes: 17 additions & 0 deletions backend/controller/dal/dal.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,22 @@ func (d *DAL) SetDeploymentReplicas(ctx context.Context, key model.DeploymentKey
return nil
}

// SetDeploymentReplicas sets the deployment endpoint
func (d *DAL) SetDeploymentEndpoint(ctx context.Context, key model.DeploymentKey, endpoint string) (err error) {
// Start the transaction
tx, err := d.Begin(ctx)
if err != nil {
return libdal.TranslatePGError(err)
}
defer tx.CommitOrRollback(ctx, &err)

err = tx.db.SetDeploymentEndpoint(ctx, key, optional.Some(endpoint))
if err != nil {
return libdal.TranslatePGError(err)
}
return nil
}

var ErrReplaceDeploymentAlreadyActive = errors.New("deployment already active")

// ReplaceDeployment replaces an old deployment of a module with a new deployment.
Expand Down Expand Up @@ -520,6 +536,7 @@ func (d *DAL) GetActiveDeployments(ctx context.Context) ([]dalmodel.Deployment,
Replicas: optional.Some(int(in.Replicas)),
Schema: in.Deployment.Schema,
CreatedAt: in.Deployment.CreatedAt,
Endpoint: in.Deployment.Endpoint,
}
}), nil
}
Expand Down
1 change: 1 addition & 0 deletions backend/controller/dal/internal/sql/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/controller/dal/internal/sql/querier.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion backend/controller/dal/internal/sql/queries.sql
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ FROM runners r
ORDER BY r.key;

-- name: GetActiveDeployments :many
SELECT sqlc.embed(d), m.name AS module_name, m.language, COUNT(r.id) AS replicas
SELECT sqlc.embed(d), m.name AS module_name, m.language, COUNT(r.id) AS replicas, endpoint
FROM deployments d
JOIN modules m ON d.module_id = m.id
LEFT JOIN runners r ON d.id = r.deployment_id
Expand Down Expand Up @@ -138,6 +138,12 @@ SET min_replicas = $2, last_activated_at = CASE WHEN min_replicas = 0 THEN (NOW(
WHERE key = sqlc.arg('key')::deployment_key
RETURNING 1;

-- name: SetDeploymentEndpoint :exec
UPDATE deployments
SET endpoint = $2
WHERE key = sqlc.arg('key')::deployment_key
RETURNING 1;

-- name: GetExistingDeploymentForModule :one
SELECT *
FROM deployments d
Expand Down
32 changes: 26 additions & 6 deletions backend/controller/dal/internal/sql/queries.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions backend/controller/dal/model/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ type Deployment struct {
Schema *schema.Module
CreatedAt time.Time
Labels model.Labels
Endpoint optional.Option[string]
}

func (d Deployment) String() string { return d.Key.String() }
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- migrate:up
ALTER TABLE deployments ADD COLUMN endpoint VARCHAR;

-- migrate:down

Loading

0 comments on commit 9c0d75f

Please sign in to comment.