소스 검색

feat: delete install task when there are no unsucceed plugins left

Yeuoly 8 달 전
부모
커밋
b6b136fc8d
1개의 변경된 파일47개의 추가작업 그리고 19개의 파일을 삭제
  1. 47 19
      internal/service/install_plugin.go

+ 47 - 19
internal/service/install_plugin.go

@@ -155,7 +155,21 @@ func InstallPluginRuntimeToTenant(
 					}
 
 					modifier(taskPointer, pluginStatus)
-					return db.Update(taskPointer, tx)
+
+					successes := 0
+					for _, plugin := range taskPointer.Plugins {
+						if plugin.Status == models.InstallTaskStatusSuccess {
+							successes++
+						}
+					}
+
+					// delete the task if all plugins are installed successfully,
+					// otherwise update the task status
+					if successes == len(taskPointer.Plugins) {
+						return db.Delete(taskPointer, tx)
+					} else {
+						return db.Update(taskPointer, tx)
+					}
 				}); err != nil {
 					log.Error("failed to update install task status %s", err.Error())
 				}
@@ -446,28 +460,42 @@ func DeletePluginInstallationItemFromTask(
 	task_id string,
 	identifier plugin_entities.PluginUniqueIdentifier,
 ) *entities.Response {
-	item, err := db.GetOne[models.InstallTask](
-		db.Equal("task_id", task_id),
-		db.Equal("tenant_id", tenant_id),
-	)
+	err := db.WithTransaction(func(tx *gorm.DB) error {
+		item, err := db.GetOne[models.InstallTask](
+			db.WithTransactionContext(tx),
+			db.Equal("task_id", task_id),
+			db.Equal("tenant_id", tenant_id),
+			db.WLock(),
+		)
 
-	if err != nil {
-		return entities.NewErrorResponse(-500, err.Error())
-	}
+		if err != nil {
+			return err
+		}
 
-	plugins := []models.InstallTaskPluginStatus{}
-	for _, plugin := range item.Plugins {
-		if plugin.PluginUniqueIdentifier != identifier {
-			plugins = append(plugins, plugin)
+		plugins := []models.InstallTaskPluginStatus{}
+		for _, plugin := range item.Plugins {
+			if plugin.PluginUniqueIdentifier != identifier {
+				plugins = append(plugins, plugin)
+			}
 		}
-	}
 
-	if len(plugins) == 0 {
-		err = db.Delete(&item)
-	} else {
-		item.Plugins = plugins
-		err = db.Update(&item)
-	}
+		successes := 0
+		for _, plugin := range plugins {
+			if plugin.Status == models.InstallTaskStatusSuccess {
+				successes++
+			}
+		}
+
+		if len(plugins) == successes {
+			// delete the task if all plugins are installed successfully
+			err = db.Delete(&item, tx)
+		} else {
+			item.Plugins = plugins
+			err = db.Update(&item, tx)
+		}
+
+		return err
+	})
 
 	if err != nil {
 		return entities.NewErrorResponse(-500, err.Error())