diff --git a/server/backend/device.go b/server/backend/device.go index 0250f4e7..3ab3068b 100644 --- a/server/backend/device.go +++ b/server/backend/device.go @@ -18,23 +18,22 @@ import ( // deviceBuffer stores all recent device calls in a buffer and flushes them to the database periodically type deviceBuffer struct { - lock sync.Mutex - devices map[string]*model.Devices // key is uuid - interval time.Duration // flush interval + lock sync.Mutex + devices map[string]*model.Devices // key is uuid } func newDeviceBuffer() *deviceBuffer { return &deviceBuffer{ - lock: sync.Mutex{}, - devices: make(map[string]*model.Devices), - interval: time.Minute, + lock: sync.Mutex{}, + devices: make(map[string]*model.Devices), } - } +const FlushingInterval = time.Minute + func (s *CampusServer) RunDeviceFlusher() error { for { - time.Sleep(s.deviceBuf.interval) + time.Sleep(FlushingInterval) if err := s.deviceBuf.flush(s.db); err != nil { log.WithError(err).Error("Error flushing device buffer") } diff --git a/server/backend/ios_notifications/ios_apns/iosAPNsRepository.go b/server/backend/ios_notifications/ios_apns/iosAPNsRepository.go index 05c03edb..339c4019 100644 --- a/server/backend/ios_notifications/ios_apns/iosAPNsRepository.go +++ b/server/backend/ios_notifications/ios_apns/iosAPNsRepository.go @@ -140,7 +140,7 @@ func NewCronRepository(db *gorm.DB) (*Repository, error) { token, err := ios_apns_jwt.NewToken() if err != nil { - log.WithError(err).Fatal("Could not create APNs token") + log.WithError(err).Error("Could not create APNs token") } return NewRepository(db, token), nil diff --git a/server/backend/rpcserver.go b/server/backend/rpcserver.go index 2bcf45f0..3544fef1 100644 --- a/server/backend/rpcserver.go +++ b/server/backend/rpcserver.go @@ -62,7 +62,7 @@ func NewIOSNotificationsService() *IOSNotificationsService { token, err := ios_apns_jwt.NewToken() if err != nil { - log.WithError(err).Fatal("failed to create new token") + log.WithError(err).Error("failed to create new token") } return &IOSNotificationsService{ diff --git a/server/main.go b/server/main.go index 9e624b66..8481f15d 100644 --- a/server/main.go +++ b/server/main.go @@ -108,7 +108,7 @@ func main() { grpc.WithUnaryInterceptor(addMethodNameInterceptor), } if err := pb.RegisterCampusHandlerFromEndpoint(context.Background(), grpcGatewayMux, httpPort, opts); err != nil { - log.WithError(err).Panic("could not RegisterCampusHandlerFromEndpoint") + log.WithError(err).Fatal("could not RegisterCampusHandlerFromEndpoint") } httpMux.Handle("/v1/", http.StripPrefix("/v1", grpcGatewayMux)) @@ -128,18 +128,15 @@ func main() { // setupDB connects to the database and migrates it if necessary func setupDB() *gorm.DB { - // Connect to DB - var conn gorm.Dialector - if dbHost := os.Getenv("DB_DSN"); dbHost == "" { + dbHost := os.Getenv("DB_DSN") + if dbHost == "" { log.Fatal("Failed to start! The 'DB_DSN' environment variable is not defined. Take a look at the README.md for more details.") - } else { - log.Info("Connecting to dsn") - conn = mysql.Open(dbHost) } - db, err := gorm.Open(conn, &gorm.Config{Logger: gorm_logrus.New()}) + log.Info("Connecting to dsn") + db, err := gorm.Open(mysql.Open(dbHost), &gorm.Config{Logger: gorm_logrus.New()}) if err != nil { - log.WithError(err).Panic("failed to connect database") + log.WithError(err).Fatal("failed to connect database") } // Migrate the schema