Ejemplo n.º 1
0
func (self *Dispatcher) Start() {
	ctx := api.NewContext(self.config)

	// Work with Server::Stareter
	baseListener, err := ss.NewListenerOrDefault("tcp", self.config.Dispatcher.Listen)
	if err != nil {
		panic(fmt.Sprintf("Failed to listen at %s: %s", self.config.Dispatcher.Listen, err))
	}
	ctx.Debugf("Listening on %s", baseListener.Addr())

	s := manners.NewServer()
	l := manners.NewListener(baseListener, s)
	err = http.Serve(l, self)
	if err != nil {
		log.Fatal(
			fmt.Sprintf("Error from server's ListenAndServe: %s\n", err),
		)
	}
}
Ejemplo n.º 2
0
func NewDrone(config *config.Config) *Drone {
	host, err := os.Hostname()
	if err != nil {
		log.Fatalf("Failed to get hostname: %s", err)
	}
	pid := os.Getpid()
	id := fmt.Sprintf("%s.%d.%d", host, pid, rand.Int31())
	return &Drone{
		id:               id,
		ctx:              api.NewContext(config),
		loop:             true,
		leader:           false,
		tasks:            nil,
		minions:          nil,
		waiter:           &sync.WaitGroup{},
		CmdChan:          make(chan DroneCmd, 1),
		sigchan:          make(chan os.Signal, 1),
		lastElectionTime: time.Time{},
	}
}
Ejemplo n.º 3
0
func (w *BaseWorker) Run() {
	closer := stf.LogMark("[Worker:%s]", w.name)
	defer closer()

	var configFile string

	pwd, err := os.Getwd()
	if err != nil {
		log.Fatalf("Could not determine current working directory")
	}

	defaultConfig := path.Join(pwd, "etc", "config.gcfg")

	flag.StringVar(
		&configFile,
		"config",
		defaultConfig,
		"Path to config file",
	)
	flag.Parse()

	os.Setenv("STF_CONFIG", configFile)

	cfg, err := config.BootstrapConfig()
	if err != nil {
		stf.Debugf("Failed to load config: %s", err)
		return
	}

	w.ctx = api.NewContext(cfg)

	w.SendCmd(CmdWorkerSlaveSpawn)

	w.MainLoop()
	stf.Debugf("Worker exiting")
}
Ejemplo n.º 4
0
func (self *TestEnv) Setup() {
	if home := os.Getenv("STF_HOME"); home != "" {
		oldpath := os.Getenv("PATH")
		newpath := path.Join(home, "bin")
		self.Test.Logf("Adding %s to path", newpath)
		os.Setenv("PATH", strings.Join([]string{newpath, oldpath}, ":"))
	}

	self.createTemporaryDir()
	self.startDatabase()
	self.startQueue()
	self.startMemcached()
	self.createTemporaryConfig()
	self.startTemporaryStorageServers()
	self.startWorkers()

	os.Setenv("STF_CONFIG", self.ConfigFile.Name())
	config, err := config.BootstrapConfig()
	if err != nil {
		self.Test.Fatalf("%s", err)
	}

	self.Ctx = api.NewContext(config)
}
Ejemplo n.º 5
0
func (self *Dispatcher) ServeHTTP(w http.ResponseWriter, r *http.Request) {
	ctx := &DispatcherContext{
		api.NewContext(self.config),
		w,
		r,
	}
	closer := ctx.LogMark("[%s %s]", r.Method, r.URL.Path)
	defer closer()

	defer apachelog.NewLoggingWriter(w, r, self.logger).EmitLog()

	// Generic catch-all handler
	defer func() {
		if err := recover(); err != nil {
			debug.PrintStack()
			ctx.Debugf("Error while serving request: %s", err)
			http.Error(w, http.StatusText(500), 500)
		}
	}()

	// First see if we have a proper URL that STF understands
	bucketName, objectName, err := parseObjectPath(r.URL.Path)
	if err != nil {
		http.Error(w, http.StatusText(404), 404)
		return
	}
	ctx.Debugf(
		"Parsed bucketName = '%s', objectName = '%s'\n",
		bucketName,
		objectName,
	)

	var resp *HTTPResponse
	switch r.Method {
	case "GET":
		resp = self.FetchObject(ctx, bucketName, objectName)
	case "DELETE":
		if objectName == "" {
			resp = self.DeleteBucket(ctx, bucketName)
		} else {
			resp = self.DeleteObject(ctx, bucketName, objectName)
		}
	case "PUT":
		// If the Content-Length is 0, then attempt to create the
		// Bucket Otherwise, try the bucket
		if cl := r.ContentLength; cl > 0 {
			resp = self.CreateObject(ctx, bucketName, objectName)
		} else {
			resp = self.CreateBucket(ctx, bucketName, objectName)
		}
	case "POST":
		resp = self.ModifyObject(ctx, bucketName, objectName)
	case "MOVE":
		dest := r.Header.Get("X-STF-Move-Destination")
		if objectName == "" {
			resp = self.RenameBucket(ctx, bucketName, dest)
		} else {
			resp = self.RenameObject(ctx, bucketName, objectName, dest)
		}
	default:
		resp = HTTPMethodNotAllowed
		return
	}

	if resp == nil {
		panic("Did not get a response object?!")
	}
	resp.Write(ctx.ResponseWriter)
}