Skip to content

Commit

Permalink
Add more logs and update the steps when initing app (apache#7662)
Browse files Browse the repository at this point in the history
* refactor(framework): add some logs and dispart `Init` from `CreateAndRunApiServer`

* refactor(framework): update initial steps
  • Loading branch information
d4x1 committed Jun 25, 2024
1 parent 12164c1 commit b355752
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 13 deletions.
5 changes: 5 additions & 0 deletions backend/core/migration/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,12 +81,17 @@ func (m *migratorImpl) Register(scripts []plugin.MigrationScript, comment string
}
}

func (m *migratorImpl) Info(stage string) {
m.logger.Info("[%s] pending scripts: %d, executed scripts: %d, total: %d", stage, len(m.pending), len(m.executed), len(m.scripts))
}

// Execute all registered migration script in order and mark them as executed in migration_history table
func (m *migratorImpl) Execute() errors.Error {
// sort the scripts by version
sort.Slice(m.pending, func(i, j int) bool {
return m.pending[i].script.Version() < m.pending[j].script.Version()
})
m.Info("Execute")
// execute them one by one
db := m.basicRes.GetDal()
for _, swc := range m.pending {
Expand Down
1 change: 1 addition & 0 deletions backend/server/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ func Init() {
func CreateAndRunApiServer() {
// Setup and run the server
Init()
services.InitExecuteMigration()
router := CreateApiServer()
SetupApiServer(router)
RunApiServer(router)
Expand Down
32 changes: 20 additions & 12 deletions backend/server/services/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ func InitResources() {
if err != nil {
panic(err)
}
logger.Info("migration initialized")
logger.Info("migrator has been initialized")
migrator.Register(migrationscripts.All(), "Framework")
}

Expand All @@ -84,25 +84,19 @@ func GetMigrator() plugin.Migrator {
return migrator
}

// Init the services module
// Should not be called concurrently
func Init() {
InitResources()

// lock the database to avoid multiple devlake instances from sharing the same one
lockDatabase()

// now, load the plugins
errors.Must(runner.LoadPlugins(basicRes))

func registerPluginsMigrationScripts() {
// pull migration scripts from plugins to migrator
for _, pluginInst := range plugin.AllPlugins() {
if migratable, ok := pluginInst.(plugin.PluginMigration); ok {
logger.Info("register plugin:%s's migrations scripts", pluginInst.Name())
migrator.Register(migratable.MigrationScripts(), pluginInst.Name())
}
}
}

func InitExecuteMigration() {
// check if there are pending migration
logger.Info("has pending scripts? %v, FORCE_MIGRATION: %s", migrator.HasPendingScripts(), cfg.GetBool("FORCE_MIGRATION"))
if migrator.HasPendingScripts() {
if cfg.GetBool("FORCE_MIGRATION") {
errors.Must(ExecuteMigration())
Expand All @@ -117,6 +111,20 @@ func Init() {
}
}

// Init the services module
// Should not be called concurrently
func Init() {
InitResources()

// lock the database to avoid multiple devlake instances from sharing the same one
lockDatabase()

// now, load the plugins
errors.Must(runner.LoadPlugins(basicRes))
logger.Info("all plugins have been loaded")
registerPluginsMigrationScripts()
}

var statusLock sync.Mutex

// ExecuteMigration executes all pending migration scripts and initialize services module
Expand Down
4 changes: 3 additions & 1 deletion backend/test/helper/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,9 @@ func ConnectLocalServer(t *testing.T, clientConfig *LocalClientConfig) *DevlakeC
cfg.Set("PLUGIN_DIR", throwawayDir)
cfg.Set("LOGGING_DIR", throwawayDir)
go func() {
initService.Do(func() { api.CreateAndRunApiServer() })
initService.Do(func() {
api.CreateAndRunApiServer()
})
}()
req, err2 := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/proceed-db-migration", addr), nil)
require.NoError(t, err2)
Expand Down

0 comments on commit b355752

Please sign in to comment.