Skip to content

Commit

Permalink
chore: refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
jvmakine committed Jan 17, 2025
1 parent da7be27 commit d8aab44
Show file tree
Hide file tree
Showing 8 changed files with 33 additions and 13 deletions.
9 changes: 5 additions & 4 deletions backend/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -310,11 +310,12 @@ func (s *Service) Status(ctx context.Context, req *connect.Request[ftlv1.StatusR
return nil, err
}
deployments, err := slices.MapErr(maps.Values(status), func(d *state.Deployment) (*ftlv1.StatusResponse_Deployment, error) {
dkey := d.Schema.GetRuntime().GetDeployment().DeploymentKey
return &ftlv1.StatusResponse_Deployment{
Key: d.Schema.GetRuntime().GetDeployment().DeploymentKey.String(),
Key: dkey.String(),
Name: d.Schema.Name,
MinReplicas: d.Schema.GetRuntime().GetScaling().GetMinReplicas(),
Replicas: replicas[d.Schema.GetRuntime().GetDeployment().DeploymentKey.String()],
Replicas: replicas[dkey.String()],
Schema: d.Schema.ToProto(),
}, nil
})
Expand Down Expand Up @@ -918,8 +919,8 @@ func (s *Service) CreateDeployment(ctx context.Context, req *connect.Request[ftl
}

dkey := key.NewDeploymentKey(module.Name)
module.GetRuntime().ModDeployment().DeploymentKey = dkey
err = s.schemaState.Publish(ctx, &state.DeploymentCreatedEvent{
Key: dkey,
CreatedAt: time.Now(),
Schema: module,
})
Expand Down Expand Up @@ -1063,7 +1064,7 @@ func (s *Service) watchModuleChanges(ctx context.Context, sendChange func(respon
case *state.DeploymentCreatedEvent:
err := sendChange(&ftlv1.PullSchemaResponse{ //nolint:forcetypeassert
ModuleName: event.Schema.Name,
DeploymentKey: proto.String(event.Key.String()),
DeploymentKey: proto.String(event.Schema.GetRuntime().GetDeployment().DeploymentKey.String()),
Schema: event.Schema.ToProto(),
ChangeType: ftlv1.DeploymentChangeType_DEPLOYMENT_CHANGE_TYPE_ADDED,
})
Expand Down
7 changes: 5 additions & 2 deletions backend/controller/state/controllerstate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,12 @@ func TestDeploymentState(t *testing.T) {
deploymentKey := key.NewDeploymentKey("test-deployment")
create := time.Now()
err = cs.Publish(ctx, &state.DeploymentCreatedEvent{
Key: deploymentKey,
CreatedAt: create,
Schema: &schema.Module{Name: "test"},
Schema: &schema.Module{Name: "test", Runtime: &schema.ModuleRuntime{
Deployment: &schema.ModuleRuntimeDeployment{
DeploymentKey: deploymentKey,
},
}},
})
assert.NoError(t, err)
view, err = cs.View(ctx)
Expand Down
7 changes: 3 additions & 4 deletions backend/controller/state/deployments.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,22 +49,21 @@ var _ SchemaEvent = (*DeploymentSchemaUpdatedEvent)(nil)
var _ SchemaEvent = (*DeploymentReplicasUpdatedEvent)(nil)

type DeploymentCreatedEvent struct {
Key key.Deployment
CreatedAt time.Time
Schema *schema.Module
}

func (r *DeploymentCreatedEvent) Handle(t SchemaState) (SchemaState, error) {
if existing := t.deployments[r.Key]; existing != nil {
key := r.Schema.GetRuntime().GetDeployment().DeploymentKey
if existing := t.deployments[key]; existing != nil {
return t, nil
}

n := Deployment{
Schema: r.Schema,
}
n.Schema.ModRuntime().ModDeployment().DeploymentKey = r.Key
n.Schema.ModRuntime().ModDeployment().CreatedAt = r.CreatedAt
t.deployments[r.Key] = &n
t.deployments[key] = &n
return t, nil
}

Expand Down
1 change: 0 additions & 1 deletion backend/controller/state/eventextractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ func EventExtractor(diff tuple.Pair[SchemaState, SchemaState]) iter.Seq[SchemaEv
pd, ok := previousAll[key]
if !ok {
events = append(events, &DeploymentCreatedEvent{
Key: key,
CreatedAt: deployment.Schema.GetRuntime().GetDeployment().GetCreatedAt(),
Schema: deployment.Schema,
})
Expand Down
1 change: 0 additions & 1 deletion backend/controller/state/eventextractor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ func TestEventExtractor(t *testing.T) {
},
want: []SchemaEvent{
&DeploymentCreatedEvent{
Key: deploymentKey(t, "dpl-test-sjkfislfjslfas"),
CreatedAt: now,
Schema: &schema.Module{Name: "test", Runtime: &schema.ModuleRuntime{
Base: schema.ModuleRuntimeBase{Language: "go"},
Expand Down
3 changes: 3 additions & 0 deletions backend/provisioner/controller_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ func NewControllerProvisioner(client ftlv1connect.ControllerServiceClient) *InMe

return &RuntimeEvent{
Module: &schema.ModuleRuntimeDeployment{
CreatedAt: module.GetRuntime().GetDeployment().GetCreatedAt(),
ActivatedAt: module.GetRuntime().GetDeployment().GetActivatedAt(),
Endpoint: module.GetRuntime().GetDeployment().GetEndpoint(),
DeploymentKey: deploymentKey,
},
}, nil
Expand Down
4 changes: 3 additions & 1 deletion backend/provisioner/runner_scaling_provisioner.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ func provisionRunner(scaling scaling.RunnerScaling) InMemResourceProvisionerFn {
return nil, fmt.Errorf("expected module, got %T", rc)
}

deployment := module.Runtime.Deployment.DeploymentKey
deployment := module.GetRuntime().GetDeployment().GetDeploymentKey()
if deployment.IsZero() {
return nil, fmt.Errorf("failed to find deployment for runner")
}
Expand Down Expand Up @@ -108,6 +108,8 @@ func provisionRunner(scaling scaling.RunnerScaling) InMemResourceProvisionerFn {
}
return &RuntimeEvent{
Module: &schema.ModuleRuntimeDeployment{
CreatedAt: module.GetRuntime().GetDeployment().GetCreatedAt(),
ActivatedAt: module.GetRuntime().GetDeployment().GetActivatedAt(),
DeploymentKey: deployment,
Endpoint: endpointURI,
},
Expand Down
14 changes: 14 additions & 0 deletions common/schema/moduleruntime.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,20 @@ func (m *ModuleRuntimeDeployment) GetCreatedAt() time.Time {
return m.CreatedAt
}

func (m *ModuleRuntimeDeployment) GetActivatedAt() time.Time {
if m == nil {
return time.Time{}
}
return m.ActivatedAt
}

func (m *ModuleRuntimeDeployment) GetEndpoint() string {
if m == nil {
return ""
}
return m.Endpoint
}

func (m *ModuleRuntimeDeployment) GetDeploymentKey() key.Deployment {
if m == nil {
return key.Deployment{}
Expand Down

0 comments on commit d8aab44

Please sign in to comment.