Skip to content

Commit

Permalink
move stop build onto build instead of scheduler
Browse files Browse the repository at this point in the history
  • Loading branch information
dleviminzi committed Jan 10, 2025
1 parent 10fda4e commit ec786e1
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 4 deletions.
23 changes: 20 additions & 3 deletions pkg/abstractions/image/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ type Builder struct {
registry *common.ImageRegistry
containerRepo repository.ContainerRepository
tailscale *network.Tailscale
eventBus *common.EventBus
}

type BuildStep struct {
Expand Down Expand Up @@ -130,13 +131,14 @@ func (o *BuildOpts) addPythonRequirements() {
o.PythonPackages = append(filteredPythonPackages, baseRequirementsSlice...)
}

func NewBuilder(config types.AppConfig, registry *common.ImageRegistry, scheduler *scheduler.Scheduler, tailscale *network.Tailscale, containerRepo repository.ContainerRepository) (*Builder, error) {
func NewBuilder(config types.AppConfig, registry *common.ImageRegistry, scheduler *scheduler.Scheduler, tailscale *network.Tailscale, containerRepo repository.ContainerRepository, eventBus *common.EventBus) (*Builder, error) {
return &Builder{
config: config,
scheduler: scheduler,
tailscale: tailscale,
registry: registry,
containerRepo: containerRepo,
eventBus: eventBus,
}, nil
}

Expand Down Expand Up @@ -245,7 +247,7 @@ func (b *Builder) Build(ctx context.Context, opts *BuildOpts, outputChan chan co

go func() {
<-ctx.Done() // If user cancels the build, send a stop-build event to the scheduler
err := b.scheduler.StopBuild(containerId)
err := b.stopBuild(containerId)
if err != nil {
log.Error().Str("container_id", containerId).Err(err).Msg("failed to stop build")
}
Expand Down Expand Up @@ -347,7 +349,7 @@ func (b *Builder) Build(ctx context.Context, opts *BuildOpts, outputChan chan co
}

if time.Since(start) > containerSpinupTimeout {
err := b.scheduler.StopBuild(containerId)
err := b.stopBuild(containerId)
if err != nil {
log.Error().Str("container_id", containerId).Err(err).Msg("failed to stop build")
}
Expand Down Expand Up @@ -741,3 +743,18 @@ func extractPackageName(pkg string) string {
// Handle regular packages
return strings.FieldsFunc(pkg, func(c rune) bool { return c == '=' || c == '>' || c == '<' || c == '[' || c == ';' })[0]
}

func (b *Builder) stopBuild(containerId string) error {
_, err := b.eventBus.Send(&common.Event{
Type: common.EventType("stop-build" + "-" + containerId),
Args: map[string]any{"container_id": containerId},
LockAndDelete: false,
})
if err != nil {
log.Error().Err(err).Msg("failed to send stop build event")
return err
}

log.Info().Str("container_id", containerId).Msg("sent stop build event")
return nil
}
3 changes: 2 additions & 1 deletion pkg/abstractions/image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ type ImageServiceOpts struct {
BackendRepo repository.BackendRepository
Scheduler *scheduler.Scheduler
Tailscale *network.Tailscale
EventBus *common.EventBus
}

func NewRuncImageService(
Expand All @@ -45,7 +46,7 @@ func NewRuncImageService(
return nil, err
}

builder, err := NewBuilder(opts.Config, registry, opts.Scheduler, opts.Tailscale, opts.ContainerRepo)
builder, err := NewBuilder(opts.Config, registry, opts.Scheduler, opts.Tailscale, opts.ContainerRepo, opts.EventBus)
if err != nil {
return nil, err
}
Expand Down
1 change: 1 addition & 0 deletions pkg/gateway/gateway.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,7 @@ func (g *Gateway) registerServices() error {
Scheduler: g.Scheduler,
Tailscale: g.Tailscale,
BackendRepo: g.BackendRepo,
EventBus: common.NewEventBus(g.RedisClient),
})
if err != nil {
return err
Expand Down

0 comments on commit ec786e1

Please sign in to comment.