<commit_before>package main

import (
	"errors"
	"fmt"
	"os"
	"reflect"
	"time"

	api "github.com/armon/consul-api"
)

const (
	// The amount of time to do a blocking query for
	defaultWaitTime = 60 * time.Second
)

type Watcher struct {
	// config is a Consul Template Config object to be read by the watcher
	config *Config
}

// NewWatcher accepts a Config and creates a new Watcher.
func NewWatcher(config *Config) (*Watcher, error) {
	if config == nil {
		return nil, errors.New("cannot specify empty config")
	}

	watcher := &Watcher{
		config: config,
	}

	return watcher, nil
}

// Watch starts the Watcher process, querying the Consul API and rendering any
// configuration file changes.
func (w *Watcher) Watch() error {
	client, err := w.client()
	if err != nil {
		return err
	}

	views, templates, ctemplates, err := w.createViews()
	if err != nil {
		return err
	}

	doneCh := make(chan struct{})
	viewCh := w.waitForChanges(views, client, doneCh)

	if w.config.Once {
		// If we are running in "once" mode, quit after the first poll
		close(doneCh)
	} else {
		// Ensure we stop background polling when we quit the Watcher
		defer close(doneCh)
	}

	for {
		select {
		case view := <-viewCh:
			println("received on viewCh")

			if w.config.Once && w.renderedAll(views) {
				// If we are in "once" mode and all the templates have been rendered,
				// exit gracefully
				return nil
			}

			for _, template := range view.Templates {
				deps := templates[template]
				context := &TemplateContext{
					Services:    make(map[string][]*Service),
					Keys:        make(map[string]string),
					KeyPrefixes: make(map[string][]*KeyPair),
				}

				// Continue if not all the required dependencies have been loaded
				// into the views
				if !w.ready(views, deps) {
					continue
				}

				for _, dep := range deps {
					v := views[dep]
					switch d := v.Dependency.(type) {
					case *ServiceDependency:
						context.Services[d.Key()] = v.Data.([]*Service)
					case *KeyDependency:
						context.Keys[d.Key()] = v.Data.(string)
					case *KeyPrefixDependency:
						context.KeyPrefixes[d.Key()] = v.Data.([]*KeyPair)
					default:<commit_msg>Move once check outside of the channel case<commit_after>package main

import (
	"errors"
	"fmt"
	"os"
	"reflect"
	"time"

	api "github.com/armon/consul-api"
)

const (
	// The amount of time to do a blocking query for
	defaultWaitTime = 60 * time.Second
)

type Watcher struct {
	// config is a Consul Template Config object to be read by the watcher
	config *Config
}

// NewWatcher accepts a Config and creates a new Watcher.
func NewWatcher(config *Config) (*Watcher, error) {
	if config == nil {
		return nil, errors.New("cannot specify empty config")
	}

	watcher := &Watcher{
		config: config,
	}

	return watcher, nil
}

// Watch starts the Watcher process, querying the Consul API and rendering any
// configuration file changes.
func (w *Watcher) Watch() error {
	client, err := w.client()
	if err != nil {
		return err
	}

	views, templates, ctemplates, err := w.createViews()
	if err != nil {
		return err
	}

	doneCh := make(chan struct{})
	viewCh := w.waitForChanges(views, client, doneCh)

	if w.config.Once {
		// If we are running in "once" mode, quit after the first poll
		close(doneCh)
	} else {
		// Ensure we stop background polling when we quit the Watcher
		defer close(doneCh)
	}

	for {
		if w.config.Once && w.renderedAll(views) {
			// If we are in "once" mode and all the templates have been rendered,
			// exit gracefully
			return nil
		}

		select {
		case view := <-viewCh:
			for _, template := range view.Templates {
				deps := templates[template]
				context := &TemplateContext{
					Services:    make(map[string][]*Service),
					Keys:        make(map[string]string),
					KeyPrefixes: make(map[string][]*KeyPair),
				}

				// Continue if not all the required dependencies have been loaded
				// into the views
				if !w.ready(views, deps) {
					continue
				}

				for _, dep := range deps {
					v := views[dep]
					switch d := v.Dependency.(type) {
					case *ServiceDependency:
						context.Services[d.Key()] = v.Data.([]*Service)
					case *KeyDependency:
						context.Keys[d.Key()] = v.Data.(string)
					case *KeyPrefixDependency:
						context.KeyPrefixes[d.Key()] = v.Data.([]*KeyPair)
					default:
{'fraction_non_alphanumeric': 0.12132438064366752, 'fraction_numerical': 0.0009261403102570039, 'mean_word_length': 3.0074211502782933, 'pattern_counts': {'":': 0, '<': 5, '<?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': '7263691', 'n_tokens_mistral': 1622, 'n_tokens_neox': 1388, 'n_words': 455}