Skip to content

Commit

Permalink
remove context
Browse files Browse the repository at this point in the history
Signed-off-by: Sammy Oina <[email protected]>
  • Loading branch information
SammyOina committed Jan 16, 2025
1 parent b63eefd commit aa7f392
Showing 1 changed file with 11 additions and 17 deletions.
28 changes: 11 additions & 17 deletions agent/algorithm/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ type docker struct {
logger *slog.Logger
stderr io.Writer
stdout io.Writer
ctx context.Context
cancel context.CancelFunc
}

func NewAlgorithm(logger *slog.Logger, eventsSvc events.Service, algoFile string, cmpID string) algorithm.Algorithm {
Expand All @@ -45,8 +43,6 @@ func NewAlgorithm(logger *slog.Logger, eventsSvc events.Service, algoFile string
stdout: &logging.Stdout{Logger: logger},
}

d.ctx, d.cancel = context.WithCancel(context.Background())

return d
}

Expand All @@ -64,15 +60,16 @@ func (d *docker) Run() error {
}
defer imageFile.Close()

ctx := context.Background()

Check warning on line 63 in agent/algorithm/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

agent/algorithm/docker/docker.go#L63

Added line #L63 was not covered by tests
// Load the Docker image from the tar file.
resp, err := cli.ImageLoad(d.ctx, imageFile, true)
resp, err := cli.ImageLoad(ctx, imageFile, true)
if err != nil {
return fmt.Errorf("could not load Docker image from file: %v", err)
}
defer resp.Body.Close()

// List the loaded images to get the image ID.
images, err := cli.ImageList(d.ctx, image.ListOptions{})
images, err := cli.ImageList(ctx, image.ListOptions{})
if err != nil {
return fmt.Errorf("could not get the Docker image list: %v", err)
}
Expand All @@ -92,7 +89,7 @@ func (d *docker) Run() error {
}

// Create and start the container.
respContainer, err := cli.ContainerCreate(d.ctx, &container.Config{
respContainer, err := cli.ContainerCreate(ctx, &container.Config{
Image: dockerImageName,
Tty: true,
AttachStdout: true,
Expand All @@ -115,11 +112,11 @@ func (d *docker) Run() error {
return fmt.Errorf("could not create a Docker container: %v", err)
}

if err := cli.ContainerStart(d.ctx, respContainer.ID, container.StartOptions{}); err != nil {
if err := cli.ContainerStart(ctx, respContainer.ID, container.StartOptions{}); err != nil {
return fmt.Errorf("could not start a Docker container: %v", err)
}

stdout, err := cli.ContainerLogs(d.ctx, respContainer.ID, container.LogsOptions{ShowStdout: true, Follow: true})
stdout, err := cli.ContainerLogs(ctx, respContainer.ID, container.LogsOptions{ShowStdout: true, Follow: true})
if err != nil {
return fmt.Errorf("could not read stdout from the container: %v", err)
}
Expand All @@ -131,7 +128,7 @@ func (d *docker) Run() error {
}
}()

stderr, err := cli.ContainerLogs(d.ctx, respContainer.ID, container.LogsOptions{ShowStderr: true, Follow: true})
stderr, err := cli.ContainerLogs(ctx, respContainer.ID, container.LogsOptions{ShowStderr: true, Follow: true})
if err != nil {
d.logger.Warn(fmt.Sprintf("could not read stderr from the container: %v", err))
}
Expand All @@ -143,7 +140,7 @@ func (d *docker) Run() error {
}
}()

statusCh, errCh := cli.ContainerWait(d.ctx, respContainer.ID, container.WaitConditionNotRunning)
statusCh, errCh := cli.ContainerWait(ctx, respContainer.ID, container.WaitConditionNotRunning)
select {
case err := <-errCh:
if err != nil {
Expand All @@ -153,11 +150,11 @@ func (d *docker) Run() error {
}

defer func() {
if err = cli.ContainerRemove(d.ctx, respContainer.ID, container.RemoveOptions{Force: true}); err != nil {
if err = cli.ContainerRemove(ctx, respContainer.ID, container.RemoveOptions{Force: true}); err != nil {
d.logger.Warn(fmt.Sprintf("error could not remove container: %v", err))
}

if _, err := cli.ImageRemove(d.ctx, imageID, image.RemoveOptions{Force: true}); err != nil {
if _, err := cli.ImageRemove(ctx, imageID, image.RemoveOptions{Force: true}); err != nil {
d.logger.Warn(fmt.Sprintf("error could not remove image: %v", err))
}
}()
Expand All @@ -180,9 +177,6 @@ func writeToOut(readCloser io.ReadCloser, ioWriter io.Writer) error {
}

func (d *docker) Stop() error {
if d.cancel != nil {
d.cancel()
}

// To be supported later.
return nil

Check warning on line 181 in agent/algorithm/docker/docker.go

View check run for this annotation

Codecov / codecov/patch

agent/algorithm/docker/docker.go#L179-L181

Added lines #L179 - L181 were not covered by tests
}

0 comments on commit aa7f392

Please sign in to comment.