Pārlūkot izejas kodu

feat: support detailed logs

Yeuoly 8 mēneši atpakaļ
vecāks
revīzija
06ab89550a

+ 1 - 1
.env.example

@@ -16,7 +16,7 @@ AWS_SECRET_KEY=
 AWS_REGION=
 
 # services storage
-PLUGIN_STORAGE_TYPE=local # local or aws_s3
+PLUGIN_STORAGE_TYPE=local
 PLUGIN_STORAGE_OSS_BUCKET=
 PLUGIN_STORAGE_LOCAL_ROOT=./storage
 

+ 3 - 0
internal/cluster/cluster.go

@@ -47,6 +47,8 @@ type Cluster struct {
 	notifyNodeUpdateChan              chan bool
 	notifyNodeUpdateCompletedChan     chan bool
 	notifyClusterStoppedChan          chan bool
+
+	showLog bool
 }
 
 func NewCluster(config *app.Config, plugin_manager *plugin_manager.PluginManager) *Cluster {
@@ -54,6 +56,7 @@ func NewCluster(config *app.Config, plugin_manager *plugin_manager.PluginManager
 		id:       uuid.New().String(),
 		port:     uint16(config.ServerPort),
 		stopChan: make(chan bool),
+		showLog:  config.DisplayClusterLog,
 
 		manager: plugin_manager,
 

+ 41 - 2
internal/cluster/plugin.go

@@ -28,6 +28,10 @@ func (c *Cluster) RegisterPlugin(lifetime plugin_entities.PluginLifetime) error
 		return err
 	}
 
+	if c.showLog {
+		log.Info("registering plugin %s", identity.String())
+	}
+
 	if c.plugins.Exists(identity.String()) {
 		return errors.New("plugin has been registered")
 	}
@@ -57,7 +61,9 @@ func (c *Cluster) RegisterPlugin(lifetime plugin_entities.PluginLifetime) error
 	}
 	c.pluginLock.Unlock()
 
-	log.Info("start to schedule plugin %s", identity)
+	if c.showLog {
+		log.Info("start to schedule plugin %s", identity)
+	}
 
 	return nil
 }
@@ -83,6 +89,10 @@ func (c *Cluster) getScanPluginsByIdKey(plugin_id string) string {
 // as for the plugin has exited, normally, it will be removed automatically
 // but once a plugin is not removed, it will be gc by the master node
 func (c *Cluster) schedulePlugins() error {
+	if c.showLog {
+		log.Info("scheduling %d plugins", c.plugins.Len())
+	}
+
 	c.notifyPluginSchedule()
 	defer c.notifyPluginScheduleCompleted()
 
@@ -90,15 +100,26 @@ func (c *Cluster) schedulePlugins() error {
 		if time.Since(value.lastScheduledAt) < PLUGIN_SCHEDULER_INTERVAL {
 			return true
 		}
+		if c.showLog {
+			log.Info("scheduling plugin %s", key)
+		}
 		// do plugin state update
 		err := c.doPluginStateUpdate(value)
 		if err != nil {
 			log.Error("failed to update plugin state: %s", err.Error())
 		}
 
+		if c.showLog {
+			log.Info("scheduled plugin %s", key)
+		}
+
 		return true
 	})
 
+	if c.showLog {
+		log.Info("scheduled %d plugins", c.plugins.Len())
+	}
+
 	return nil
 }
 
@@ -115,6 +136,10 @@ func (c *Cluster) doPluginStateUpdate(lifetime *pluginLifeTime) error {
 		return err
 	}
 
+	if c.showLog {
+		log.Info("updating plugin state %s", identity.String())
+	}
+
 	scheduleState := &pluginState{
 		Identity:           identity.String(),
 		PluginRuntimeState: state,
@@ -124,12 +149,18 @@ func (c *Cluster) doPluginStateUpdate(lifetime *pluginLifeTime) error {
 
 	// check if the plugin has been removed
 	if !c.plugins.Exists(identity.String()) {
+		if c.showLog {
+			log.Info("removing plugin state %s due no longer exists", identity.String())
+		}
 		// remove state
 		err = c.removePluginState(c.id, hashIdentity)
 		if err != nil {
 			return err
 		}
 	} else {
+		if c.showLog {
+			log.Info("updating plugin state %s", identity.String())
+		}
 		// update plugin state
 		scheduleState.ScheduledAt = &[]time.Time{time.Now()}[0]
 		err = cache.SetMapOneField(PLUGIN_STATE_MAP_KEY, stateKey, scheduleState)
@@ -137,6 +168,9 @@ func (c *Cluster) doPluginStateUpdate(lifetime *pluginLifeTime) error {
 			return err
 		}
 		lifetime.lifetime.UpdateScheduledAt(*scheduleState.ScheduledAt)
+		if c.showLog {
+			log.Info("updated plugin state %s", identity.String())
+		}
 	}
 
 	lifetime.lastScheduledAt = time.Now()
@@ -145,12 +179,17 @@ func (c *Cluster) doPluginStateUpdate(lifetime *pluginLifeTime) error {
 }
 
 func (c *Cluster) removePluginState(nodeId string, hashed_identity string) error {
+	if c.showLog {
+		log.Info("removing plugin state %s", hashed_identity)
+	}
 	err := cache.DelMapField(PLUGIN_STATE_MAP_KEY, c.getPluginStateKey(nodeId, hashed_identity))
 	if err != nil {
 		return err
 	}
 
-	log.Info("plugin %s has been removed from node %s", hashed_identity, c.id)
+	if c.showLog {
+		log.Info("plugin %s has been removed from node %s", hashed_identity, c.id)
+	}
 
 	return nil
 }

+ 2 - 0
internal/types/app/config.go

@@ -82,6 +82,8 @@ type Config struct {
 	MaxAWSLambdaTransactionTimeout int   `envconfig:"MAX_AWS_LAMBDA_TRANSACTION_TIMEOUT"`
 
 	PythonInterpreterPath string `envconfig:"PYTHON_INTERPRETER_PATH"`
+
+	DisplayClusterLog bool `envconfig:"DISPLAY_CLUSTER_LOG"`
 }
 
 func (c *Config) Validate() error {