<commit_before>package main

import (
	"encoding/json"
	"flag"
	"github.com/jetblack87/maestro/data"
	"io/ioutil"
	"os"
	"os/exec"
	"os/signal"
	"strings"
	"path"
	"github.com/samuel/go-zookeeper/zk"
	"time"
	"log"
)

const APP_VERSION = "0.1"

// The flag package provides a default help printer via -h switch
var versionFlag *bool = flag.Bool("v", false, "Print the version number.")
var zookeeper *string = flag.String("zookeeper", "localhost:2181", "The ZooKeeper connection string (defaults to 'localhost:2182').")
var agentName *string = flag.String("name", "", "REQUIRED: The name of the agent.")
var domainName *string = flag.String("domain", "", "REQUIRED: The name of the domain in which this agent lives.")
var agentConfig *string = flag.String("agentConfig", "", "Supply a json file that contains specific configuration for this agent.")
var processesConfig *string = flag.String("processesConfig", "", "Supply a json file that contains specific configuration any processes.")
var logfilePath *string = flag.String("logfile", "stdout", "The path to the logfile.")

var zkdao data.ZkDAO
var request *processStartRequest


const MAX_START_RETRIES = 3 

func main() {

	flag.Parse() // Scan the arguments list

	// Setup signal channel
	signalChannel := make(chan os.Signal, 1)
	signal.Notify(signalChannel, os.Interrupt, os.Kill)


    // Setup logging
	if *logfilePath != "stdout" {
		f, err := os.OpenFile(*logfilePath, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
		if err != nil {
		    log.Printf("error opening file: %v", err)
		}
		defer f.Close()
		log.SetOutput(f)
	}

	// Check the parameters
	if *versionFlag {
		log.Println("Version:", APP_VERSION)
		os.Exit(0)
	}
	if *agentName == "" {
		panic("-agent is required")
	}
	if *domainName == "" {
		panic("-domain is required")
	}
	
	log.Printf("maestro agent starting for domain '%s' and agent '%s'\n", *domainName, *domainName)

	zkdao, err := data.NewZkDAO(strings.Split(*zookeeper, ","))
	if err != nil {
		panic(err)
	}

	// Load config into ZK
	err = loadAgentConfig(*agentConfig, *agentName, *domainName)
	if err != nil {
		panic(err)
	}

	// Load processes data into ZK
	err = loadProcessesConfig(*processesConfig, *domainName)
	if err != nil {
		panic(err)
	}

	log.Println("Loading the agent configuration")
	agent, err := zkdao.LoadAgent(data.PathToKey("/maestro/"+*domainName+"/config/agents/"+*agentName), true)
	if err != nil {
		panic(err)
	}

	// Create channel used for watching ZK nodes
	watchChannel := make(chan zk.Event, 1)

	// Remove old runtime config for this agent
	err = zkdao.RemoveRecursive("/maestro/"+*domainName+"/runtime/agents/"+agent.Name)

	// Add processes to the runtime configuration, adding watches to admin_state
	for key := range agent.Processes {
		log.Println("Loading processes from config: " + agent.Processes[key].ProcessClass)
		agent.Processes[key], err = zkdao.LoadProcess(data.PathToKey(agent.Processes[key].ProcessClass), true)
		if err != nil {
			panic(err)
		}
		agent.Processes[key].Key = data.PathToKey("/maestro/"+*domainName+"/runtime/agents/"+agent.Name+"/processes/"+agent.Processes[key].Name)
		if agent.Processes[key].AdminState == "" {
			// Default to on
			log.Println("Defaulting admin_state to 'on'")
			agent.Processes[key].AdminState = "on"
		}
		agent.Processes[key].OperState = "off"
	}

	log.Println("Adding agent to runtime configuration")
	if err != nil {
		log.Printf("Failed to remove agent runtime configuration")
		panic(err)
	}
	err = zkdao.UpdateAgent(data.PathToKey("/maestro/"+*domainName+"/runtime/agents/"+agent.Name), agent, true)
	if err != nil {
		panic(err)
	}
	
	str, err := zkdao.CreateEphemeral("/maestro/"+*domainName+"/runtime/agents/"+agent.Name+"/eph", []byte("I am alive"))
	if err != nil {
		errMsg := "Error creating ephemeral node: " + str
		log.Println(errMsg)
		panic(err)
	}

	// Add watches for all admin_state nodes
	for key := range agent.Processes {
		adminStatePath := data.KeyToPath(agent.Processes[key].Key)+"/admin_state"
		log.Println("Adding watch to node: " + adminStatePath)
		err := zkdao.Watch(adminStatePath, watchChannel)
		if err != nil {
			log.Println("Failed to add watch to process node:\n" + err.Error())
		}
	}
	
	log.Println("Starting process monitoring")
<commit_msg>Moved an error handle<commit_after>package main

import (
	"encoding/json"
	"flag"
	"github.com/jetblack87/maestro/data"
	"io/ioutil"
	"os"
	"os/exec"
	"os/signal"
	"strings"
	"path"
	"github.com/samuel/go-zookeeper/zk"
	"time"
	"log"
)

const APP_VERSION = "0.1"

// The flag package provides a default help printer via -h switch
var versionFlag *bool = flag.Bool("v", false, "Print the version number.")
var zookeeper *string = flag.String("zookeeper", "localhost:2181", "The ZooKeeper connection string (defaults to 'localhost:2182').")
var agentName *string = flag.String("name", "", "REQUIRED: The name of the agent.")
var domainName *string = flag.String("domain", "", "REQUIRED: The name of the domain in which this agent lives.")
var agentConfig *string = flag.String("agentConfig", "", "Supply a json file that contains specific configuration for this agent.")
var processesConfig *string = flag.String("processesConfig", "", "Supply a json file that contains specific configuration any processes.")
var logfilePath *string = flag.String("logfile", "stdout", "The path to the logfile.")

var zkdao data.ZkDAO
var request *processStartRequest


const MAX_START_RETRIES = 3 

func main() {

	flag.Parse() // Scan the arguments list

	// Setup signal channel
	signalChannel := make(chan os.Signal, 1)
	signal.Notify(signalChannel, os.Interrupt, os.Kill)


    // Setup logging
	if *logfilePath != "stdout" {
		f, err := os.OpenFile(*logfilePath, os.O_RDWR | os.O_CREATE | os.O_APPEND, 0666)
		if err != nil {
		    log.Printf("error opening file: %v", err)
		}
		defer f.Close()
		log.SetOutput(f)
	}

	// Check the parameters
	if *versionFlag {
		log.Println("Version:", APP_VERSION)
		os.Exit(0)
	}
	if *agentName == "" {
		panic("-agent is required")
	}
	if *domainName == "" {
		panic("-domain is required")
	}
	
	log.Printf("maestro agent starting for domain '%s' and agent '%s'\n", *domainName, *domainName)

	zkdao, err := data.NewZkDAO(strings.Split(*zookeeper, ","))
	if err != nil {
		panic(err)
	}

	// Load config into ZK
	err = loadAgentConfig(*agentConfig, *agentName, *domainName)
	if err != nil {
		panic(err)
	}

	// Load processes data into ZK
	err = loadProcessesConfig(*processesConfig, *domainName)
	if err != nil {
		panic(err)
	}

	log.Println("Loading the agent configuration")
	agent, err := zkdao.LoadAgent(data.PathToKey("/maestro/"+*domainName+"/config/agents/"+*agentName), true)
	if err != nil {
		panic(err)
	}

	// Create channel used for watching ZK nodes
	watchChannel := make(chan zk.Event, 1)

	// Remove old runtime config for this agent
	err = zkdao.RemoveRecursive("/maestro/"+*domainName+"/runtime/agents/"+agent.Name)
	if err != nil {
		log.Printf("Failed to remove agent runtime configuration")
		panic(err)
	}

	// Add processes to the runtime configuration, adding watches to admin_state
	for key := range agent.Processes {
		log.Println("Loading processes from config: " + agent.Processes[key].ProcessClass)
		agent.Processes[key], err = zkdao.LoadProcess(data.PathToKey(agent.Processes[key].ProcessClass), true)
		if err != nil {
			panic(err)
		}
		agent.Processes[key].Key = data.PathToKey("/maestro/"+*domainName+"/runtime/agents/"+agent.Name+"/processes/"+agent.Processes[key].Name)
		if agent.Processes[key].AdminState == "" {
			// Default to on
			log.Println("Defaulting admin_state to 'on'")
			agent.Processes[key].AdminState = "on"
		}
		agent.Processes[key].OperState = "off"
	}

	log.Println("Adding agent to runtime configuration")
	err = zkdao.UpdateAgent(data.PathToKey("/maestro/"+*domainName+"/runtime/agents/"+agent.Name), agent, true)
	if err != nil {
		panic(err)
	}
	
	str, err := zkdao.CreateEphemeral("/maestro/"+*domainName+"/runtime/agents/"+agent.Name+"/eph", []byte("I am alive"))
	if err != nil {
		errMsg := "Error creating ephemeral node: " + str
		log.Println(errMsg)
		panic(err)
	}

	// Add watches for all admin_state nodes
	for key := range agent.Processes {
		adminStatePath := data.KeyToPath(agent.Processes[key].Key)+"/admin_state"
		log.Println("Adding watch to node: " + adminStatePath)
		err := zkdao.Watch(adminStatePath, watchChannel)
		if err != nil {
			log.Println("Failed to add watch to process node:\n" + err.Error())
		}
	}
	
	log.Println("Starting process monitoring")

{'fraction_non_alphanumeric': 0.14944951979386273, 'fraction_numerical': 0.004684937924572499, 'mean_word_length': 4.284034653465347, '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': '1557236', 'n_tokens_mistral': 3043, 'n_tokens_neox': 2900, 'n_words': 745}