<commit_before>package main

import (
	"os/exec"
	"strings"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/samalba/dockerclient"
)

var (
	// TODO: should this be shared struct property?
	docker dockerclient.Client
)

func getNamesFromRawLinks(rawLinks []string) ([]string, error) {
	names := []string{}
	for _, rawLinks := range rawLinks {
		// ORIGINAL : "/redis:/adoring_stallman/redis"
		// DESIRED  : "redis" (the last bit of the string)
		// TODO: can this be simplified?
		name := strings.Split(strings.Split(rawLinks, ":")[1], "/")[2]
		names = append(names, name)
	}
	return names, nil
}

func getLinks(info dockerclient.ContainerInfo) ([]string, error) {
	rawLinks := info.HostConfig.Links
	links, err := getNamesFromRawLinks(rawLinks)
	if err != nil {
		return []string{}, err
	}
	log.Infoln("links for", info.Name, "are", links)
	return links, nil
}

func pollRestart(c dockerclient.Container, done chan string, rollers chan string) {
	log.Infoln("Initiating poll restart for ", c.Id)
	depRemaining := make(map[string]bool)

	// TODO: should we just scrap getLinks() function
	// altogether if I need to inline this anyway?
	info, err := docker.InspectContainer(c.Id)
	if err != nil {
		log.Fatal(err)
	}

	deps, err := getLinks(*info)
	if err != nil {
		log.Fatal(err)
	}
	for _, link := range deps {
		depRemaining[link] = true
	}

	if len(deps) > 0 {
	DepCheck:
		for lastRestarted := range rollers {
			log.Debugln("Read", lastRestarted, "from rollers in goroutine for", c.Id[:7])
			depRemaining[lastRestarted] = false
			log.Infoln("depRemaining is", depRemaining)
			for _, depRemains := range depRemaining {
				if depRemains {
					continue DepCheck
				}
			}
			break DepCheck
		}
	}

	log.Debugln("Starting", c.Id)

	// Alright, all the deps are done restarting,
	// so let's rock and roll!
	if err := docker.StartContainer(c.Id, nil); err != nil {
		log.Fatal(err)
	}

	// lose the "/" on the beginning of container name
	done <- info.Name[1:]

	// Just catch the rest until the channel closes
	for lastRestarted := range rollers {
		log.Debugln("Read", lastRestarted, "from rollers in goroutine for", c.Id[:7])
	}
}

func main() {
	var (
		err error
	)
	docker, err = dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
	if err != nil {
		log.Fatal(err)
	}

	restartDone := make(chan string)

	// todo: should this be slice?
	// i.e. is there any reason to track the id in key?
	rMsgs := []chan string{}

	containers, err := docker.ListContainers(false, false, "")
	if err != nil {
		log.Fatal(err)
	}

	stopMsgs := make(chan bool)
	for _, c := range containers {
		log.Infoln(c.Id, c.Names)
		rMsgs = append(rMsgs, make(chan string))
		go func(id string) {
			log.Infoln("Stopping container", id)
			if err := docker.StopContainer(id, 30); err != nil {
				log.Fatal(err)
			}
			stopMsgs <- true
		}(c.Id)
	}

	// wait for all containers to be stopped
	for i := 0; i < len(containers); i++ {
		<-stopMsgs
	}
	close(stopMsgs)

	log.Info("All containers stopped")

	// restart the daemon<commit_msg>Continue cleaning up logging output<commit_after>package main

import (
	"os/exec"
	"strings"
	"time"

	log "github.com/Sirupsen/logrus"
	"github.com/samalba/dockerclient"
)

var (
	// TODO: should this be shared struct property?
	docker dockerclient.Client
)

func getNamesFromRawLinks(rawLinks []string) ([]string, error) {
	names := []string{}
	for _, rawLinks := range rawLinks {
		// ORIGINAL : "/redis:/adoring_stallman/redis"
		// DESIRED  : "redis" (the last bit of the string)
		// TODO: can this be simplified?
		name := strings.Split(strings.Split(rawLinks, ":")[1], "/")[2]
		names = append(names, name)
	}
	return names, nil
}

func getLinks(info dockerclient.ContainerInfo) ([]string, error) {
	rawLinks := info.HostConfig.Links
	links, err := getNamesFromRawLinks(rawLinks)
	if err != nil {
		return []string{}, err
	}
	log.Debugln("links for", info.Name, "are", links)
	return links, nil
}

func pollRestart(c dockerclient.Container, done chan string, rollers chan string) {
	log.Infoln("Initiating restart for ", c.Id)
	depRemaining := make(map[string]bool)

	// TODO: should we just scrap getLinks() function
	// altogether if I need to inline this anyway?
	info, err := docker.InspectContainer(c.Id)
	if err != nil {
		log.Fatal(err)
	}

	deps, err := getLinks(*info)
	if err != nil {
		log.Fatal(err)
	}
	for _, link := range deps {
		depRemaining[link] = true
	}

	if len(deps) > 0 {
	DepCheck:
		for lastRestarted := range rollers {
			log.Debugln("Read", lastRestarted, "from rollers in goroutine for", c.Id[:7])
			depRemaining[lastRestarted] = false
			log.Debugln("depRemaining is", depRemaining)
			for _, depRemains := range depRemaining {
				if depRemains {
					continue DepCheck
				}
			}
			break DepCheck
		}
	}

	log.Debugln("Starting", c.Id)

	// Alright, all the deps are done restarting,
	// so let's rock and roll!
	if err := docker.StartContainer(c.Id, nil); err != nil {
		log.Fatal(err)
	}

	// lose the "/" on the beginning of container name
	done <- info.Name[1:]

	// Just catch the rest until the channel closes
	for lastRestarted := range rollers {
		log.Debugln("Read", lastRestarted, "from rollers in goroutine for", c.Id[:7])
	}
}

func main() {
	var (
		err error
	)
	docker, err = dockerclient.NewDockerClient("unix:///var/run/docker.sock", nil)
	if err != nil {
		log.Fatal(err)
	}

	restartDone := make(chan string)

	// todo: should this be slice?
	// i.e. is there any reason to track the id in key?
	rMsgs := []chan string{}

	containers, err := docker.ListContainers(false, false, "")
	if err != nil {
		log.Fatal(err)
	}

	stopMsgs := make(chan bool)
	for _, c := range containers {
		rMsgs = append(rMsgs, make(chan string))
		go func(id string) {
			log.Infoln("Initiating stop for container", id)
			if err := docker.StopContainer(id, 30); err != nil {
				log.Fatal(err)
			}
			stopMsgs <- true
		}(c.Id)
	}

	// wait for all containers to be stopped
	for i := 0; i < len(containers); i++ {
		<-stopMsgs
	}
	close(stopMsgs)

	log.Info("All containers stopped")

	// restart the daemon
{'fraction_non_alphanumeric': 0.14238845144356954, 'fraction_numerical': 0.002952755905511811, 'mean_word_length': 3.539836187639613, 'pattern_counts': {'":': 2, '<': 11, '<?xml version=': 0, '>': 5, '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': '2284510', 'n_tokens_mistral': 2381, 'n_tokens_neox': 2161, 'n_words': 612}