<commit_before>package runner

import (
	"errors"
	"flag"
	"fmt"
	"net/url"
	"os"
	"os/signal"
	"socialapi/config"
	"socialapi/workers/helper"
	"strconv"
	"syscall"
	"github.com/koding/kite"
	kiteconfig "github.com/koding/kite/config"
	"github.com/koding/kite/protocol"

	"github.com/koding/bongo"
	"github.com/koding/broker"
	"github.com/koding/logging"
)

var (
	flagConfFile = flag.String("c", "", "Configuration profile from file")
	flagRegion   = flag.String("r", "", "Region name")
	flagDebug    = flag.Bool("d", false, "Debug mode")
	flagVersion  = flag.Int("v", 0, "Worker Version")

	flagKiteInit       = flag.Bool("kite-init", false, "Init kite system with the worker.")
	flagKiteLocal      = flag.Bool("kite-local", false, "Start kite system in local mode.")
	flagKiteProxy      = flag.Bool("kite-proxy", false, "Start kite system behind a proxy")
	flagKiteKontrolURL = flag.String("kite-kontrol-url", "", "Change kite's register URL to kontrol")
)

type Runner struct {
	Log             logging.Logger
	Conf            *config.Config
	Bongo           *bongo.Bongo
	Name            string
	ShutdownHandler func()
	Done            chan error
	Kite            *kite.Kite
}

func New(name string) *Runner {
	return &Runner{Name: name}
}

func WrapWithVersion(name string, version *int) string {
	return fmt.Sprintf("%s:%d", name, *version)
}

func (r *Runner) Init() error {
	flag.Parse()
	if *flagConfFile == "" {
		return fmt.Errorf("Please define config file with -c Exiting...")
	}

	return r.InitWithConfigFile(*flagConfFile)
}

// InitWithConfigFile used for externally setting config file.
// This is used for testing purposes, and usage of Init method is encouraged
func (r *Runner) InitWithConfigFile(flagConfFile string) error {
	r.Conf = config.MustRead(flagConfFile)
	r.Conf.FlagDebugMode = *flagDebug

	// create logger for our package
	r.Log = helper.CreateLogger(
		WrapWithVersion(r.Name, flagVersion),
		*flagDebug,
	)

	// panics if not successful
	r.Bongo = helper.MustInitBongo(
		WrapWithVersion(r.Name, flagVersion),
		WrapWithVersion(r.Conf.EventExchangeName, flagVersion),
		r.Conf,
		r.Log,
		*flagDebug,
	)

	r.ShutdownHandler = func() {}
	r.Done = make(chan error, 1)
	r.RegisterSignalHandler()

	if *flagKiteInit {
		if err := r.initKite(); err != nil {
			return err
		}
	}

	return nil
}

func (r *Runner) initKite() error {
	// init kite here
	k := kite.New(r.Name, "0.0."+strconv.Itoa(*flagVersion))

	// TODO use get
	k.Config = kiteconfig.MustGet()
	// no need to set, will be set randomly.<commit_msg>Social: inject metrics into bongo<commit_after>package runner

import (
	"errors"
	"flag"
	"fmt"
	"net/url"
	"os"
	"os/signal"
	"socialapi/config"
	"socialapi/workers/helper"
	"strconv"
	"syscall"

	"github.com/koding/kite"
	kiteconfig "github.com/koding/kite/config"
	"github.com/koding/kite/protocol"
	"github.com/koding/metrics"

	"github.com/koding/bongo"
	"github.com/koding/broker"
	"github.com/koding/logging"
)

var (
	flagConfFile = flag.String("c", "", "Configuration profile from file")
	flagRegion   = flag.String("r", "", "Region name")
	flagDebug    = flag.Bool("d", false, "Debug mode")
	flagVersion  = flag.Int("v", 0, "Worker Version")

	flagOutputMetrics = flag.Bool("outputMetrics", false, "Output metrics")

	flagKiteInit       = flag.Bool("kite-init", false, "Init kite system with the worker.")
	flagKiteLocal      = flag.Bool("kite-local", false, "Start kite system in local mode.")
	flagKiteProxy      = flag.Bool("kite-proxy", false, "Start kite system behind a proxy")
	flagKiteKontrolURL = flag.String("kite-kontrol-url", "", "Change kite's register URL to kontrol")
)

type Runner struct {
	Log             logging.Logger
	Conf            *config.Config
	Bongo           *bongo.Bongo
	Name            string
	ShutdownHandler func()
	Done            chan error
	Kite            *kite.Kite
	Metrics         *metrics.Metrics
}

func New(name string) *Runner {
	return &Runner{Name: name}
}

func WrapWithVersion(name string, version *int) string {
	return fmt.Sprintf("%s:%d", name, *version)
}

func (r *Runner) Init() error {
	flag.Parse()
	if *flagConfFile == "" {
		return fmt.Errorf("Please define config file with -c Exiting...")
	}

	return r.InitWithConfigFile(*flagConfFile)
}

// InitWithConfigFile used for externally setting config file.
// This is used for testing purposes, and usage of Init method is encouraged
func (r *Runner) InitWithConfigFile(flagConfFile string) error {
	r.Conf = config.MustRead(flagConfFile)
	r.Conf.FlagDebugMode = *flagDebug

	// create logger for our package
	r.Log = helper.CreateLogger(
		WrapWithVersion(r.Name, flagVersion),
		*flagDebug,
	)

	metrics = helper.CreateMetrics(r.Name, r.Log, *flagOutputMetrics)

	// panics if not successful
	r.Bongo = helper.MustInitBongo(
		WrapWithVersion(r.Name, flagVersion),
		WrapWithVersion(r.Conf.EventExchangeName, flagVersion),
		r.Conf,
		r.Log,
		metrics,
		*flagDebug,
	)

	r.ShutdownHandler = func() {}
	r.Done = make(chan error, 1)
	r.RegisterSignalHandler()

	if *flagKiteInit {
		if err := r.initKite(); err != nil {
			return err
		}
	}

	return nil
}

func (r *Runner) initKite() error {
	// init kite here
	k := kite.New(r.Name, "0.0."+strconv.Itoa(*flagVersion))

	// TODO use get
	k.Config = kiteconfig.MustGet()
	// no need to set, will be set randomly.
{'fraction_non_alphanumeric': 0.1355868544600939, 'fraction_numerical': 0.0015023474178403756, 'mean_word_length': 3.619254119687771, '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': '6195042', 'n_tokens_mistral': 1918, 'n_tokens_neox': 1850, 'n_words': 464}