<commit_before>/*
 * Copyright (c) 2013-2014, <NAME> (<<EMAIL>>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Package serfin 
package serfin

import (
	"github.com/ctdk/goas/v2/logger"
	"github.com/ctdk/goiardi/config"
	serfclient "github.com/hashicorp/serf/client"
	"os"
	"encoding/json"
)

var Serfer *serfclient.RPCClient

// StartSerfin sets up the serf instance and starts listening for events and
// queries from other serf instances.
func StartSerfin() error {
	var err error
	Serfer, err = serfclient.NewRPCClient(config.Config.SerfAddr)
	if err != nil {
		logger.Criticalf(err.Error())
		os.Exit(1)
	}
	err = Serfer.UserEvent("goiardi-join", []byte(config.Config.Hostname), true)
	if err != nil {
		logger.Criticalf(err.Error())
		os.Exit(1)
	}
	errch := make(chan error, 1)
	go startEventMonitor(Serfer, errch)

	err = <-errch
	if err != nil {
		logger.Errorf(err.Error())
		os.Exit(1)
	}

	return nil
}

func startEventMonitor(sc *serfclient.RPCClient, errch chan<- error) {
	ch := make(chan map[string]interface{}, 1)
	sh, err := sc.Stream("*", ch)
	if err != nil {
		errch <- err
		return
	}
	errch <- nil

	defer sc.Stop(sh)
	// watch the events and queries
	for e := range ch {
		logger.Debugf("Got an event: %v", e)
	}
	return
}

func SendEvent(eventName string, payload interface{}) {
	jsonPayload, err := json.Marshal(payload)
	if err != nil {<commit_msg>Watch serf event stream for node statuses<commit_after>/*
 * Copyright (c) 2013-2014, <NAME> (<<EMAIL>>)
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Package serfin 
package serfin

import (
	"github.com/ctdk/goas/v2/logger"
	"github.com/ctdk/goiardi/config"
	"github.com/ctdk/goiardi/node"
	serfclient "github.com/hashicorp/serf/client"
	"os"
	"encoding/json"
)

var Serfer *serfclient.RPCClient

// StartSerfin sets up the serf instance and starts listening for events and
// queries from other serf instances.
func StartSerfin() error {
	var err error
	Serfer, err = serfclient.NewRPCClient(config.Config.SerfAddr)
	if err != nil {
		logger.Criticalf(err.Error())
		os.Exit(1)
	}
	err = Serfer.UserEvent("goiardi-join", []byte(config.Config.Hostname), true)
	if err != nil {
		logger.Criticalf(err.Error())
		os.Exit(1)
	}
	errch := make(chan error, 1)
	go startEventMonitor(Serfer, errch)

	err = <-errch
	if err != nil {
		logger.Errorf(err.Error())
		os.Exit(1)
	}

	return nil
}

func startEventMonitor(sc *serfclient.RPCClient, errch chan<- error) {
	ch := make(chan map[string]interface{}, 1)
	sh, err := sc.Stream("*", ch)
	if err != nil {
		errch <- err
		return
	}
	errch <- nil

	defer sc.Stop(sh)
	// watch the events and queries
	for e := range ch {
		logger.Debugf("Got an event: %v", e)
		switch e["Name"].(string) {
		case "node_status":
			jsonPayload := make(map[string]string)
			err = json.Unmarshal(e["Payload"].([]byte), &jsonPayload)
			if err != nil {
				logger.Errorf(err.Error())
				continue
			}
			n, _ := node.Get(jsonPayload["node"])
			if n == nil {
				logger.Errorf("No node %s", jsonPayload["node"])
				continue
			}
			err = n.UpdateStatus(jsonPayload["status"])
			if err != nil {
				logger.Errorf(err.Error())
				continue
			}
			r := map[string]string{ "response": "ok" }
			response, _ := json.Marshal(r)
			sc.Respond(uint64(e["ID"].(int64)), response)
		}
	}
	return
}

func SendEvent(eventName string, payload interface{}) {
	jsonPayload, err := json.Marshal(payload)
	if err != nil {
{'fraction_non_alphanumeric': 0.13309352517985612, 'fraction_numerical': 0.008992805755395683, 'mean_word_length': 3.7229299363057327, 'pattern_counts': {'":': 2, '<': 17, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '7612327', 'n_tokens_mistral': 1606, 'n_tokens_neox': 1490, 'n_words': 454}