Exemplo n.º 1
0
Arquivo: main.go Projeto: purak/lsd
func main() {
	host := flag.String("host", "", "Hostname to connect")
	name := flag.String("name", "", "Your name on the Newton cluster")
	fileDir := flag.String("fileDir", "", "Static file directory which will be served outside")
	flag.Parse()

	logger := log.NewLogfmtLogger(log.NewSyncWriter(os.Stdout))
	logger = log.NewContext(logger).With("ts", log.DefaultTimestampUTC, "caller", log.DefaultCaller)
	if *host == "" {
		logger.Log("error", "you have to give a host")
		return
	}

	if *name == "" {
		logger.Log("error", "you have to give a name")
		return
	}

	if *fileDir == "" {
		logger.Log("error", "you have to give a fileDir")
		return
	}

	if _, err := os.Stat(*fileDir); os.IsNotExist(err) {
		logger.Log("error", fmt.Sprintf("fileDir: %s could not be found"))
	}

	e := eventManager{
		host:      *host,
		name:      *name,
		fileDir:   *fileDir,
		logger:    logger,
		telegraph: telegraph.New(*host, *name, false, true, false),
	}

	go func() {
		logger.Log("info", fmt.Sprintf("starting to listen on %s", *host))
		if err := e.telegraph.ListenAndServe(); err != nil {
			fmt.Fprintf(os.Stderr, "error during listening: %s\n", err)
			return
		}
	}()

	go func() {
		<-e.telegraph.StartChan
		logger.Log("info", "consuming events")
		for event := range e.telegraph.Events {
			e.eventWG.Add(1)
			logger.Log("debug", "fire up new event")
			go e.processEvent(event)
		}
	}()

	go e.waitForInterrupt()

	<-e.telegraph.StopChan

	// Wait until all event workers shutdown gracefully.
	logger.Log("info", "waiting for running event workers")
	done := make(chan struct{})
	go func() {
		e.eventWG.Wait()
		close(done)
	}()

	select {
	case <-time.After(15 * time.Second):
		logger.Log("warning", "some event workers could not be closed gracefully")
	case <-done:
	}

	logger.Log("info", "good byte!")
}
Exemplo n.º 2
0
func TestSyncWriterConcurrency(t *testing.T) {
	var w io.Writer
	w = &bytes.Buffer{}
	w = log.NewSyncWriter(w)
	testConcurrency(t, log.NewLogfmtLogger(w), 10000)
}