Пример #1
0
// CloseSession puts the connection back into the pool
func CloseSession(sessionId string, mongoSession *mgo.Session) {
	defer helper.CatchPanic(nil, sessionId, "CloseSession")

	tracelog.Started(sessionId, "CloseSession")
	mongoSession.Close()
	tracelog.Completed(sessionId, "CloseSession")
}
Пример #2
0
// FindStation retrieves the specified station
func FindStation(service *services.Service, stationId string) (buoyStation *buoyModels.BuoyStation, err error) {
	defer helper.CatchPanic(&err, service.UserId, "FindStation")

	tracelog.Started(service.UserId, "FindStation")

	queryMap := bson.M{"station_id": stationId}

	buoyStation = &buoyModels.BuoyStation{}
	err = service.DBAction(Config.Database, "buoy_stations",
		func(collection *mgo.Collection) error {
			tracelog.Trace(service.UserId, "FindStation", "Query : %s", mongo.ToString(queryMap))
			return collection.Find(queryMap).One(buoyStation)
		})

	if err != nil {
		if strings.Contains(err.Error(), "not found") == false {
			tracelog.CompletedError(err, service.UserId, "FindStation")
			return buoyStation, err
		}

		err = nil
	}

	tracelog.Completed(service.UserId, "FindStation")
	return buoyStation, err
}
Пример #3
0
// CloseSession puts the connection back into the pool
func CloseSession(sessionId string, mongoSession *mgo.Session) {
	defer helper.CatchPanic(nil, sessionId, "CloseSession")

	tracelog.STARTED(sessionId, "CloseSession")

	mongoSession.Close()

	tracelog.COMPLETED(sessionId, "CloseSession")
}
Пример #4
0
// Finish is called after the controller.
func (service *Service) Finish() (err error) {
	defer helper.CatchPanic(&err, service.UserID, "Service.Finish")

	if service.MongoSession != nil {
		mongo.CloseSession(service.UserID, service.MongoSession)
		service.MongoSession = nil
	}

	return err
}
Пример #5
0
func (this *Service) Finish() (err error) {
	defer helper.CatchPanic(&err, this.UserId, "Service.Finish")

	if this.MongoSession != nil {
		mongo.CloseSession(this.UserId, this.MongoSession)
		this.MongoSession = nil
	}

	return err
}
Пример #6
0
// Shutdown systematically brings the manager down gracefully
func Shutdown(sessionId string) (err error) {
	defer helper.CatchPanic(&err, sessionId, "Shutdown")

	tracelog.STARTED(sessionId, "Shutdown")

	// Close the databases
	for _, session := range _This.sessions {
		CloseSession(sessionId, session.mongoSession)
	}

	tracelog.COMPLETED(sessionId, "Shutdown")
	return err
}
Пример #7
0
// Shutdown systematically brings the manager down gracefully
func Shutdown(sessionId string) (err error) {
	defer helper.CatchPanic(&err, sessionId, "Shutdown")

	tracelog.Started(sessionId, "Shutdown")

	// Close the databases
	for _, session := range singleton.sessions {
		CloseSession(sessionId, session.mongoSession)
	}

	tracelog.Completed(sessionId, "Shutdown")
	return err
}
Пример #8
0
// CreateSession creates a connection pool for use
func CreateSession(sessionId string, mode string, sessionName string, hosts []string, databaseName string, username string, password string) (err error) {
	defer helper.CatchPanic(nil, sessionId, "CreateSession")

	tracelog.STARTEDf(sessionId, "CreateSession", "Mode[%s] SessionName[%s] Hosts[%s] DatabaseName[%s] Username[%s]", mode, sessionName, hosts, databaseName, username)

	// Create the database object
	mongoSession := &mongoSession{
		mongoDBDialInfo: &mgo.DialInfo{
			Addrs:    hosts,
			Timeout:  60 * time.Second,
			Database: databaseName,
			Username: username,
			Password: password,
		},
	}

	// Establish the master session
	mongoSession.mongoSession, err = mgo.DialWithInfo(mongoSession.mongoDBDialInfo)
	if err != nil {
		tracelog.COMPLETED_ERROR(err, sessionId, "CreateSession")
		return err
	}

	switch mode {
	case "strong":
		// Reads and writes will always be made to the master server using a
		// unique connection so that reads and writes are fully consistent,
		// ordered, and observing the most up-to-date data.
		// http://godoc.org/labix.org/v2/mgo#Session.SetMode
		mongoSession.mongoSession.SetMode(mgo.Strong, true)
		break

	case "monotonic":
		// Reads may not be entirely up-to-date, but they will always see the
		// history of changes moving forward, the data read will be consistent
		// across sequential queries in the same session, and modifications made
		// within the session will be observed in following queries (read-your-writes).
		// http://godoc.org/labix.org/v2/mgo#Session.SetMode
		mongoSession.mongoSession.SetMode(mgo.Monotonic, true)
	}

	// Have the session check for errors
	// http://godoc.org/labix.org/v2/mgo#Session.SetSafe
	mongoSession.mongoSession.SetSafe(&mgo.Safe{})

	// Add the database to the map
	_This.sessions[sessionName] = mongoSession

	tracelog.COMPLETED(sessionId, "CreateSession")
	return err
}
Пример #9
0
// CopySession makes a clone of the specified session for client use
func CloneSession(sessionId string, useSession string) (mongoSession *mgo.Session, err error) {
	defer helper.CatchPanic(nil, sessionId, "CopySession")

	tracelog.STARTEDf(sessionId, "CloneSession", "UseSession[%s]", useSession)

	// Find the session object
	session := _This.sessions[useSession]

	if session == nil {
		err = fmt.Errorf("Unable To Locate Session %s", useSession)
		tracelog.COMPLETED_ERROR(err, sessionId, "CloneSession")
		return mongoSession, err
	}

	// Clone the master session
	mongoSession = session.mongoSession.Clone()

	tracelog.COMPLETED(sessionId, "CloneSession")
	return mongoSession, err
}
Пример #10
0
// CopySession makes a clone of the specified session for client use
func CloneSession(sessionId string, useSession string) (mongoSession *mgo.Session, err error) {
	defer helper.CatchPanic(nil, sessionId, "CopySession")

	tracelog.Startedf(sessionId, "CloneSession", "UseSession[%s]", useSession)

	// Find the session object
	session := singleton.sessions[useSession]

	if session == nil {
		err = fmt.Errorf("Unable To Locate Session %s", useSession)
		tracelog.CompletedError(err, sessionId, "CloneSession")
		return mongoSession, err
	}

	// Clone the master session
	mongoSession = session.mongoSession.Clone()

	tracelog.Completed(sessionId, "CloneSession")
	return mongoSession, err
}
Пример #11
0
// FindRegion retrieves the stations for the specified region
func FindRegion(service *services.Service, region string) (buoyStations []*buoyModels.BuoyStation, err error) {
	defer helper.CatchPanic(&err, service.UserId, "FindRegion")

	tracelog.Started(service.UserId, "FindRegion")

	queryMap := bson.M{"region": region}

	buoyStations = []*buoyModels.BuoyStation{}
	err = service.DBAction(Config.Database, "buoy_stations",
		func(collection *mgo.Collection) error {
			tracelog.Trace(service.UserId, "FindRegion", "Query : %s", mongo.ToString(queryMap))
			return collection.Find(queryMap).All(&buoyStations)
		})

	if err != nil {
		tracelog.CompletedError(err, service.UserId, "FindRegion")
		return buoyStations, err
	}

	tracelog.Completed(service.UserId, "FindRegion")
	return buoyStations, err
}
Пример #12
0
// FindStation retrieves the specified station
func FindStation(service *services.Service, stationId string) (buoyStation *buoyModels.BuoyStation, err error) {
	defer helper.CatchPanic(&err, service.UserId, "FindStation")

	tracelog.STARTED(service.UserId, "FindStation")

	queryMap := bson.M{"station_id": stationId}
	tracelog.TRACE(service.UserId, "FindStation", "Query : %s", mongo.ToString(queryMap))

	buoyStation = &buoyModels.BuoyStation{}
	err = service.DBAction(Config.Database, "buoy_stations",
		func(collection *mgo.Collection) error {
			return collection.Find(queryMap).One(buoyStation)
		})

	if err != nil {
		tracelog.COMPLETED_ERROR(err, service.UserId, "FindStation")
		return buoyStation, err
	}

	tracelog.COMPLETED(service.UserId, "FindStation")
	return buoyStation, err
}
Пример #13
0
// Startup brings the manager to a running state
func Startup(sessionId string) (err error) {
	defer helper.CatchPanic(&err, sessionId, "Startup")

	// If the system has already been started ignore the call
	if _This != nil {
		return err
	}

	tracelog.STARTED(sessionId, "Startup")

	// Pull in the configuration
	config := mongoConfiguration{}
	err = envconfig.Process("mgo", &config)
	if err != nil {
		tracelog.COMPLETED_ERROR(err, sessionId, "Startup")
		return err
	}

	// Create the Mongo Manager
	_This = &mongoManager{
		sessions: map[string]*mongoSession{},
	}

	// Log the mongodb connection straps
	tracelog.TRACE(sessionId, "Startup", "MongoDB : Hosts[%s]", config.Hosts)
	tracelog.TRACE(sessionId, "Startup", "MongoDB : Database[%s]", config.Database)
	tracelog.TRACE(sessionId, "Startup", "MongoDB : Username[%s]", config.UserName)

	hosts := strings.Split(config.Hosts, ",")

	// Create the strong and monotonic sessions
	err = CreateSession(sessionId, "strong", MASTER_SESSION, hosts, config.Database, config.UserName, config.Password)
	err = CreateSession(sessionId, "monotonic", MONOTONIC_SESSION, hosts, config.Database, config.UserName, config.Password)

	tracelog.COMPLETED(sessionId, "Startup")
	return err
}
Пример #14
0
// FindRegion retrieves the stations for the specified region
func FindRegion(service *services.Service, region string) (buoyStations []*buoyModels.BuoyStation, err error) {
	defer helper.CatchPanic(&err, service.UserId, "FindRegion")

	tracelog.STARTED(service.UserId, "FindRegion")

	// Find the specified region
	queryMap := bson.M{"region": region}
	tracelog.TRACE(service.UserId, "FindRegion", "Query : %s", mongo.ToString(queryMap))

	// Capture the specified buoy
	buoyStations = []*buoyModels.BuoyStation{}
	err = service.DBAction(Config.Database, "buoy_stations",
		func(collection *mgo.Collection) error {
			return collection.Find(queryMap).All(&buoyStations)
		})

	if err != nil {
		tracelog.COMPLETED_ERROR(err, service.UserId, "FindRegion")
		return buoyStations, err
	}

	tracelog.COMPLETED(service.UserId, "FindRegion")
	return buoyStations, err
}