You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@devlake.apache.org by ly...@apache.org on 2024/06/25 08:07:33 UTC

(incubator-devlake) 01/01: refactor(framework): add some logs and dispart `Init` from `CreateAndRunApiServer`

This is an automated email from the ASF dual-hosted git repository.

lynwee pushed a commit to branch dev0625-1
in repository https://gitbox.apache.org/repos/asf/incubator-devlake.git

commit 2fb67c9fe8cfee58b22f8c91b5fcf714c5b61f6a
Author: d4x1 <15...@qq.com>
AuthorDate: Tue Jun 25 16:07:21 2024 +0800

    refactor(framework): add some logs and dispart `Init` from `CreateAndRunApiServer`
---
 backend/core/migration/migrator.go | 5 +++++
 backend/server/api/api.go          | 2 --
 backend/server/main.go             | 2 ++
 backend/server/services/init.go    | 5 ++++-
 backend/test/helper/client.go      | 5 ++++-
 5 files changed, 15 insertions(+), 4 deletions(-)

diff --git a/backend/core/migration/migrator.go b/backend/core/migration/migrator.go
index f30a75b94..d60c9b8f9 100644
--- a/backend/core/migration/migrator.go
+++ b/backend/core/migration/migrator.go
@@ -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 {
diff --git a/backend/server/api/api.go b/backend/server/api/api.go
index fd659627b..f5a34190d 100644
--- a/backend/server/api/api.go
+++ b/backend/server/api/api.go
@@ -66,8 +66,6 @@ func Init() {
 // @host localhost:8080
 // @BasePath /
 func CreateAndRunApiServer() {
-	// Setup and run the server
-	Init()
 	router := CreateApiServer()
 	SetupApiServer(router)
 	RunApiServer(router)
diff --git a/backend/server/main.go b/backend/server/main.go
index 664eaf0b8..579df08c0 100644
--- a/backend/server/main.go
+++ b/backend/server/main.go
@@ -30,5 +30,7 @@ func main() {
 	if encryptionSecret == "" {
 		panic("ENCRYPTION_SECRET must be set in environment variable or .env file")
 	}
+	// Setup and run the server
+	api.Init()
 	api.CreateAndRunApiServer()
 }
diff --git a/backend/server/services/init.go b/backend/server/services/init.go
index 3a99c02e3..72a4fd952 100644
--- a/backend/server/services/init.go
+++ b/backend/server/services/init.go
@@ -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")
 }
 
@@ -94,15 +94,18 @@ func Init() {
 
 	// now, load the plugins
 	errors.Must(runner.LoadPlugins(basicRes))
+	logger.Info("all plugins have been loaded")
 
 	// 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())
 		}
 	}
 
 	// 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())
diff --git a/backend/test/helper/client.go b/backend/test/helper/client.go
index dcc30cba2..d6184957b 100644
--- a/backend/test/helper/client.go
+++ b/backend/test/helper/client.go
@@ -172,7 +172,10 @@ 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.Init()
+				api.CreateAndRunApiServer()
+			})
 		}()
 		req, err2 := http.NewRequest(http.MethodGet, fmt.Sprintf("%s/proceed-db-migration", addr), nil)
 		require.NoError(t, err2)