Ejemplo n.º 1
0
Archivo: main.go Proyecto: pressly/qmd
func main() {
	flags.Parse(os.Args[1:])

	// Override config file by the CONFIG env var, if specified.
	if os.Getenv("CONFIG") != "" {
		*confFile = os.Getenv("CONFIG")
	}

	// Read Config.
	conf, err := config.New(*confFile)
	if err != nil {
		log.Fatal(err)
	}

	// Run QMD.
	app, err := qmd.New(conf)
	if err != nil {
		log.Fatal(err)
	}
	go app.WatchScripts()
	go app.StartWorkers()
	go app.ListenQueue()

	graceful.AddSignal(syscall.SIGINT, syscall.SIGTERM)
	graceful.PreHook(app.Close)

	// Start the API server.
	log.Printf("Starting QMD API at %s\n", conf.Bind)
	err = graceful.ListenAndServe(conf.Bind, rest.Routes(app))
	if err != nil {
		log.Fatal(err)
	}
	graceful.Wait()
}
Ejemplo n.º 2
0
func TestPing(t *testing.T) {
	conf, _ := config.New("../etc/qmd.conf.sample")

	qmd := &qmd.Qmd{
		Config:             conf,
		DB:                 nil,
		Queue:              nil,
		ClosingListenQueue: make(chan struct{}),
		ClosingWorkers:     make(chan struct{}),
	}

	ts := httptest.NewServer(rest.Routes(qmd))
	defer ts.Close()

	res, err := http.Get(ts.URL + "/ping")
	if err != nil {
		t.Error(err)
	}

	dot, err := ioutil.ReadAll(res.Body)
	if err != nil {
		t.Error(err)
	}
	defer res.Body.Close()

	if res.StatusCode != 200 {
		t.Error("unexpected response status code")
	}

	if string(dot) != "." {
		t.Error("unexpected response body")
	}
}
Ejemplo n.º 3
0
func TestStartWaitJob(t *testing.T) {
	run := exec.Command("bash", "-c", "echo -n stdout; echo -n stderr >&2; echo -n result >$QMD_OUT")

	conf, err := config.New("./etc/qmd.conf.sample")
	if err != nil {
		log.Fatal(err)
	}

	Qmd := &qmd.Qmd{
		Config:             conf,
		DB:                 nil,
		Queue:              nil,
		ClosingListenQueue: make(chan struct{}),
		ClosingWorkers:     make(chan struct{}),
	}

	cmd, err := Qmd.Cmd(run)
	if err != nil {
		t.Error(err)
	}
	if cmd.State != qmd.Initialized {
		t.Error("unexpected value")
	}
	if cmd.Duration != 0 {
		t.Error("unexpected value")
	}

	err = cmd.Start()
	if err != nil {
		t.Error(err)
	}
	if cmd.State != qmd.Running {
		t.Error("unexpected value")
	}

	err = cmd.Wait()
	if err != nil {
		t.Error(err)
	}
	if cmd.State != qmd.Finished {
		t.Error("unexpected value")
	}
	if cmd.Duration == 0 {
		t.Error("unexpected value")
	}

	// Test the cmd's STDOUT and STDERR.
	if e := "stdoutstderr"; cmd.CmdOut.String() != e {
		t.Errorf(`expected "%s", got "%s"`, e, cmd.CmdOut.String())
	}
	if e := "result"; cmd.QmdOut.String() != e {
		t.Errorf(`expected "%s", got "%s"`, e, cmd.CmdOut.String())
	}
}