Tweak DAL setup to prevent the server freezing
Errors are now logged and they don't kill the setup process. This will be improved.
This commit is contained in:
+24
-1
@@ -328,9 +328,10 @@ func (app *CortezaApp) InitServices(ctx context.Context) (err error) {
|
||||
}
|
||||
|
||||
// Init DAL and prepare default connection
|
||||
dalLogger := app.Log.Named("dal")
|
||||
if _, err = dal.InitGlobalService(
|
||||
ctx,
|
||||
app.Log.Named("dal"),
|
||||
dalLogger,
|
||||
app.Opt.Environment.IsDevelopment(),
|
||||
|
||||
// DB_DSN is the default connection with full capabilities
|
||||
@@ -481,6 +482,20 @@ func (app *CortezaApp) InitServices(ctx context.Context) (err error) {
|
||||
return
|
||||
}
|
||||
|
||||
// Initializing DAL components
|
||||
// - Sensitivity levels
|
||||
err = sysService.DefaultDalSensitivityLevel.ReloadSensitivityLevels(ctx, sysService.DefaultStore)
|
||||
if err != nil {
|
||||
// @note we're not erroring out here so Corteza can still be used
|
||||
dalLogger.Error("failed to initialize DAL sensitivity levels", zap.Error(err))
|
||||
}
|
||||
// - DAL connections
|
||||
err = sysService.DefaultDalConnection.ReloadConnections(ctx)
|
||||
if err != nil {
|
||||
// @note we're not erroring out here so Corteza can still be used
|
||||
dalLogger.Error("failed to initialize DAL connections", zap.Error(err))
|
||||
}
|
||||
|
||||
if app.Opt.Messagebus.Enabled {
|
||||
// initialize all the queue handlers
|
||||
messagebus.Service().Init(ctx, service.DefaultQueue)
|
||||
@@ -515,6 +530,14 @@ func (app *CortezaApp) InitServices(ctx context.Context) (err error) {
|
||||
return fmt.Errorf("could not initialize compose services: %w", err)
|
||||
}
|
||||
|
||||
// Initializing DAL components
|
||||
// - Models
|
||||
err = cmpService.DefaultModule.ReloadDALModels(ctx)
|
||||
if err != nil {
|
||||
// @note we're not erroring out here so Corteza can still be used
|
||||
dalLogger.Error("failed to initialize DAL models", zap.Error(err))
|
||||
}
|
||||
|
||||
corredor.Service().SetUserFinder(sysService.DefaultUser)
|
||||
corredor.Service().SetRoleFinder(sysService.DefaultRole)
|
||||
|
||||
|
||||
@@ -31,7 +31,11 @@ func init() {
|
||||
}
|
||||
|
||||
func Connect(ctx context.Context, dsn string) (_ store.Storer, err error) {
|
||||
db, cfg, err := connectBase(ctx, dsn)
|
||||
cfg, err := NewConfig(dsn)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
db, err := connectBase(ctx, cfg)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -51,11 +55,7 @@ func Connect(ctx context.Context, dsn string) (_ store.Storer, err error) {
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func connectBase(ctx context.Context, dsn string) (db *sqlx.DB, cfg *rdbms.ConnConfig, err error) {
|
||||
if cfg, err = NewConfig(dsn); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
func connectBase(ctx context.Context, cfg *rdbms.ConnConfig) (db *sqlx.DB, err error) {
|
||||
if db, err = rdbms.Connect(ctx, logger.Default(), cfg); err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -13,7 +13,18 @@ func init() {
|
||||
}
|
||||
|
||||
func dalConnector(ctx context.Context, dsn string, cc ...capabilities.Capability) (_ dal.Connection, err error) {
|
||||
db, _, err := connectBase(ctx, dsn)
|
||||
cfg, err := NewConfig(dsn)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
// @todo rework the config building a bit; this will do for now
|
||||
if cfg.ConnTryMax >= 99 {
|
||||
cfg.ConnTryMax = 2
|
||||
}
|
||||
|
||||
db, err := connectBase(ctx, cfg)
|
||||
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
@@ -39,16 +39,14 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func Connection(ctx context.Context, pcOpts types.DalConnection, dal dalConnections) (*dalConnection, error) {
|
||||
out := &dalConnection{
|
||||
func Connection(ctx context.Context, pcOpts types.DalConnection, dal dalConnections) *dalConnection {
|
||||
return &dalConnection{
|
||||
ac: DefaultAccessControl,
|
||||
actionlog: DefaultActionlog,
|
||||
store: DefaultStore,
|
||||
dal: dal,
|
||||
primaryConnection: pcOpts,
|
||||
}
|
||||
|
||||
return out, out.reloadConnections(ctx)
|
||||
}
|
||||
|
||||
func (svc *dalConnection) FindByID(ctx context.Context, ID uint64) (q *types.DalConnection, err error) {
|
||||
@@ -272,7 +270,7 @@ func (svc *dalConnection) Search(ctx context.Context, filter types.DalConnection
|
||||
return r, f, svc.recordAction(ctx, aProps, DalConnectionActionSearch, err)
|
||||
}
|
||||
|
||||
func (svc *dalConnection) reloadConnections(ctx context.Context) (err error) {
|
||||
func (svc *dalConnection) ReloadConnections(ctx context.Context) (err error) {
|
||||
// Get all available connections
|
||||
cc, _, err := store.SearchDalConnections(ctx, svc.store, types.DalConnectionFilter{})
|
||||
if err != nil {
|
||||
|
||||
@@ -31,15 +31,14 @@ type (
|
||||
}
|
||||
)
|
||||
|
||||
func SensitivityLevel(ctx context.Context, dal dalSensitivityLevels) (*dalSensitivityLevel, error) {
|
||||
out := &dalSensitivityLevel{
|
||||
func SensitivityLevel(ctx context.Context, dal dalSensitivityLevels) *dalSensitivityLevel {
|
||||
return &dalSensitivityLevel{
|
||||
ac: DefaultAccessControl,
|
||||
actionlog: DefaultActionlog,
|
||||
store: DefaultStore,
|
||||
dal: dal,
|
||||
}
|
||||
|
||||
return out, out.reloadSensitivityLevels(ctx, out.store)
|
||||
}
|
||||
|
||||
func (svc *dalSensitivityLevel) FindByID(ctx context.Context, ID uint64) (q *types.DalSensitivityLevel, err error) {
|
||||
@@ -93,7 +92,7 @@ func (svc *dalSensitivityLevel) Create(ctx context.Context, new *types.DalSensit
|
||||
|
||||
q = new
|
||||
|
||||
return svc.reloadSensitivityLevels(ctx, svc.store)
|
||||
return svc.ReloadSensitivityLevels(ctx, svc.store)
|
||||
}()
|
||||
|
||||
return q, svc.recordAction(ctx, qProps, DalSensitivityLevelActionCreate, err)
|
||||
@@ -130,7 +129,7 @@ func (svc *dalSensitivityLevel) Update(ctx context.Context, upd *types.DalSensit
|
||||
|
||||
q = upd
|
||||
|
||||
return svc.reloadSensitivityLevels(ctx, svc.store)
|
||||
return svc.ReloadSensitivityLevels(ctx, svc.store)
|
||||
}()
|
||||
|
||||
return q, svc.recordAction(ctx, qProps, DalSensitivityLevelActionUpdate, err)
|
||||
@@ -169,7 +168,7 @@ func (svc *dalSensitivityLevel) DeleteByID(ctx context.Context, ID uint64) (err
|
||||
return
|
||||
}
|
||||
|
||||
return svc.reloadSensitivityLevels(ctx, svc.store)
|
||||
return svc.ReloadSensitivityLevels(ctx, svc.store)
|
||||
}()
|
||||
|
||||
return svc.recordAction(ctx, qProps, DalSensitivityLevelActionDelete, err)
|
||||
@@ -203,7 +202,7 @@ func (svc *dalSensitivityLevel) UndeleteByID(ctx context.Context, ID uint64) (er
|
||||
return
|
||||
}
|
||||
|
||||
return svc.reloadSensitivityLevels(ctx, svc.store)
|
||||
return svc.ReloadSensitivityLevels(ctx, svc.store)
|
||||
}()
|
||||
|
||||
return svc.recordAction(ctx, qProps, DalSensitivityLevelActionDelete, err)
|
||||
@@ -238,7 +237,7 @@ func (svc *dalSensitivityLevel) Search(ctx context.Context, filter types.DalSens
|
||||
return r, f, svc.recordAction(ctx, aProps, DalSensitivityLevelActionSearch, err)
|
||||
}
|
||||
|
||||
func (svc *dalSensitivityLevel) reloadSensitivityLevels(ctx context.Context, s store.Storer) (err error) {
|
||||
func (svc *dalSensitivityLevel) ReloadSensitivityLevels(ctx context.Context, s store.Storer) (err error) {
|
||||
ll, err := svc.getSensitivityLevels(ctx, s)
|
||||
if err != nil {
|
||||
return
|
||||
|
||||
@@ -150,12 +150,9 @@ func Initialize(ctx context.Context, log *zap.Logger, s store.Storer, primaryCon
|
||||
DefaultSettings = Settings(ctx, DefaultStore, DefaultLogger, DefaultAccessControl, CurrentSettings)
|
||||
|
||||
primaryConnectionConfig = primaryConn
|
||||
DefaultDalConnection, err = Connection(ctx, primaryConnectionConfig, dal.Service())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
DefaultDalConnection = Connection(ctx, primaryConnectionConfig, dal.Service())
|
||||
|
||||
DefaultDalSensitivityLevel, err = SensitivityLevel(ctx, dal.Service())
|
||||
DefaultDalSensitivityLevel = SensitivityLevel(ctx, dal.Service())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user