<commit_before>package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/fsouza/go-dockerclient"
)

type DockerDynoDriver struct {
	d     *Docker
	state DynoState
	waiting          chan error
}

func (dd *DockerDynoDriver) Build(release *Release) error {
	if dd.d == nil {
		dd.d = &Docker{}
		if err := dd.d.Connect(); err != nil {
			dd.d = nil
			return err
		}
	}

	si, err := dd.d.StackStat("cedar-14")
	if err != nil {
		return err
	}

	imageName, err := dd.d.BuildSlugImage(si, release)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Built image successfully")

	release.imageName = imageName
	return nil
}

func (dd *DockerDynoDriver) State() DynoState {
	return dd.state
}

func (dd *DockerDynoDriver) Start(release *Release, ex *Executor) error {
	if dd.d == nil {
		dd.d = &Docker{}
		if err := dd.d.Connect(); err != nil {
			dd.d = nil
			return err
		}
	}

	ex.containers = make([]*docker.Container, 0)

	// Fill environment vector from Heroku configuration.
	env := make([]string, 0)
	for k, v := range release.config {
		env = append(env, k+"="+v)
	}

	container, err := dd.d.c.CreateContainer(docker.CreateContainerOptions{
		Name: fmt.Sprintf("%v-%v", release.imageName, int32(time.Now().Unix())),
		Config: &docker.Config{
			Cmd:   ex.Args(),
			Env:   env,
			Image: release.imageName,
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	ex.containers = append(ex.containers, container)

	for _, container := range ex.containers {
		err = dd.d.c.StartContainer(container.ID, &docker.HostConfig{})
		if err != nil {
			log.Fatal(err)
		}

		go dd.d.c.Logs(docker.LogsOptions{
			Container:    container.ID,
			Stdout:       true,
			Stderr:       true,
			Follow:       true,
			OutputStream: os.Stdout,
		})
	}

	dd.state = Started
	return nil
}

func (dd *DockerDynoDriver) Stop(ex *Executor) error {
	for _, container := range ex.containers {
		err := dd.d.c.StopContainer(container.ID, 10)
		return err
	}

	// @todo: need to be move this onto an executor instead of a driver
	dd.state = Stopped
	return nil
}
<commit_msg>Small refactoring within dyno driver to consolidate Docker connection checks<commit_after>package main

import (
	"fmt"
	"log"
	"os"
	"time"

	"github.com/fsouza/go-dockerclient"
)

type DockerDynoDriver struct {
	d     *Docker
	state DynoState
	waiting          chan error
}

func (dd *DockerDynoDriver) Build(release *Release) error {
	if err := dd.connectDocker(); err != nil {
		return err
	}

	si, err := dd.d.StackStat("cedar-14")
	if err != nil {
		return err
	}

	imageName, err := dd.d.BuildSlugImage(si, release)
	if err != nil {
		log.Fatal(err)
	}
	log.Println("Built image successfully")

	release.imageName = imageName
	return nil
}

func (dd *DockerDynoDriver) State() DynoState {
	return dd.state
}

func (dd *DockerDynoDriver) Start(release *Release, ex *Executor) error {
	ex.containers = make([]*docker.Container, 0)

	// Fill environment vector from Heroku configuration.
	env := make([]string, 0)
	for k, v := range release.config {
		env = append(env, k+"="+v)
	}

	container, err := dd.d.c.CreateContainer(docker.CreateContainerOptions{
		Name: fmt.Sprintf("%v-%v", release.imageName, int32(time.Now().Unix())),
		Config: &docker.Config{
			Cmd:   ex.Args(),
			Env:   env,
			Image: release.imageName,
		},
	})
	if err != nil {
		log.Fatal(err)
	}
	ex.containers = append(ex.containers, container)

	for _, container := range ex.containers {
		err = dd.d.c.StartContainer(container.ID, &docker.HostConfig{})
		if err != nil {
			log.Fatal(err)
		}

		go dd.d.c.Logs(docker.LogsOptions{
			Container:    container.ID,
			Stdout:       true,
			Stderr:       true,
			Follow:       true,
			OutputStream: os.Stdout,
		})
	}

	dd.state = Started
	return nil
}

func (dd *DockerDynoDriver) Stop(ex *Executor) error {
	for _, container := range ex.containers {
		err := dd.d.c.StopContainer(container.ID, 10)
		return err
	}

	// @todo: need to be move this onto an executor instead of a driver
	dd.state = Stopped
	return nil
}

func (dd *DockerDynoDriver) connectDocker() error {
	if dd.d == nil {
		dd.d = &Docker{}
		if err := dd.d.Connect(); err != nil {
			dd.d = nil
			return err
		}
	}

	return nil
}

{'fraction_non_alphanumeric': 0.142549629275293, 'fraction_numerical': 0.003826835685242765, 'mean_word_length': 3.186186186186186, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '730029', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1549, 'n_words': 391}