Example #1
0
func main() {

	// parse flag variables
	flag.StringVar(&dockerhost, "docker-host", "", "")
	flag.StringVar(&dockercert, "docker-cert", "", "")
	flag.StringVar(&dockerkey, "docker-key", "", "")
	flag.StringVar(&password, "password", "admin:admin", "")
	flag.StringVar(&driver, "driver", "sqlite3", "")
	flag.StringVar(&datasource, "datasource", "pub.sqlite", "")
	flag.Parse()

	// Create the worker pool and director.
	workers = pool.New()
	worker = director.New()

	// Create the Docker worker is provided via
	// the commandline. Else it is expected that
	// workers are added via the REST endpoint.
	if len(dockerhost) != 0 {
		var d, err = docker.New(dockerhost, dockercert, dockerkey)
		if err != nil {
			fmt.Println("ERROR creating Docker client.", err)
			os.Exit(1)
		}
		workers.Allocate(d)
	}

	// Create the database connection
	db = datasql.MustConnect(driver, datasource)
	datasql.New(db).KillBuilds()

	// Parse the Template files
	templates := rice.MustFindBox("website")
	handler.BuildTempl = template.Must(template.New("_").Parse(templates.MustString("build.tmpl")))
	handler.IndexTempl = template.Must(template.New("_").Parse(templates.MustString("index.tmpl")))

	// Include static resources
	assets := http.FileServer(rice.MustFindBox("website").HTTPBox())
	http.Handle("/static/", http.StripPrefix("/static", assets))

	// Create the router and add middleware
	mux := router.New()
	mux.Use(handler.SetHeaders)
	mux.Use(secureMiddleware)
	mux.Use(contextMiddleware)
	http.Handle("/", mux)

	if err := http.ListenAndServe(":8000", nil); err != nil {
		fmt.Println("ERROR starting web server.", err)
	}
}
Example #2
0
func TestDirector(t *testing.T) {

	g := goblin.Goblin(t)
	g.Describe("Director", func() {

		g.It("Should mark work as pending", func() {
			d := New()
			d.markPending(&worker.Work{})
			d.markPending(&worker.Work{})
			g.Assert(len(d.GetPending())).Equal(2)
		})

		g.It("Should mark work as started", func() {
			d := New()
			w1 := worker.Work{}
			w2 := worker.Work{}
			d.markPending(&w1)
			d.markPending(&w2)
			g.Assert(len(d.GetPending())).Equal(2)
			d.markStarted(&w1, &mockWorker{})
			g.Assert(len(d.GetStarted())).Equal(1)
			g.Assert(len(d.GetPending())).Equal(1)
			d.markStarted(&w2, &mockWorker{})
			g.Assert(len(d.GetStarted())).Equal(2)
			g.Assert(len(d.GetPending())).Equal(0)
		})

		g.It("Should mark work as complete", func() {
			d := New()
			w1 := worker.Work{}
			w2 := worker.Work{}
			d.markStarted(&w1, &mockWorker{})
			d.markStarted(&w2, &mockWorker{})
			g.Assert(len(d.GetStarted())).Equal(2)
			d.markComplete(&w1)
			g.Assert(len(d.GetStarted())).Equal(1)
			d.markComplete(&w2)
			g.Assert(len(d.GetStarted())).Equal(0)
		})

		g.It("Should get work assignments", func() {
			d := New()
			w1 := worker.Work{}
			w2 := worker.Work{}
			d.markStarted(&w1, &mockWorker{})
			d.markStarted(&w2, &mockWorker{})
			g.Assert(len(d.GetAssignemnts())).Equal(2)
		})

		g.It("Should recover from a panic", func() {
			d := New()
			d.Do(nil, nil)
			g.Assert(true).Equal(true)
		})

		g.It("Should distribute work to worker", func() {
			work := &worker.Work{}
			workr := &mockWorker{}
			c := context.Background()
			p := pool.New()
			p.Allocate(workr)
			c = pool.NewContext(c, p)

			d := New()
			d.do(c, work)
			g.Assert(workr.work).Equal(work) // verify mock worker gets work
		})

		g.It("Should add director to context", func() {
			d := New()
			c := context.Background()
			c = NewContext(c, d)
			g.Assert(worker.FromContext(c)).Equal(d)
		})
	})
}