Skip to content

Commit

Permalink
fix: async install plugins task
Browse files Browse the repository at this point in the history
  • Loading branch information
Yeuoly committed Oct 16, 2024
1 parent d39d8ff commit fc7bfc9
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 5 deletions.
12 changes: 11 additions & 1 deletion internal/core/plugin_manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,17 @@ func (p *PluginManager) BackwardsInvocation() dify_invocation.BackwardsInvocatio

func (p *PluginManager) SavePackage(plugin_unique_identifier plugin_entities.PluginUniqueIdentifier, pkg []byte) error {
// save to storage
return os.WriteFile(filepath.Join(p.packageCachePath, plugin_unique_identifier.String()), pkg, 0644)
pkg_path := filepath.Join(p.packageCachePath, plugin_unique_identifier.String())
pkg_dir := filepath.Dir(pkg_path)
if err := os.MkdirAll(pkg_dir, 0755); err != nil {
return err
}

if err := os.WriteFile(pkg_path, pkg, 0644); err != nil {
return err
}

return nil
}

func (p *PluginManager) GetPackage(plugin_unique_identifier plugin_entities.PluginUniqueIdentifier) ([]byte, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/server/controllers/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func FetchPluginInstallationTasks(c *gin.Context) {
func FetchPluginInstallationTask(c *gin.Context) {
BindRequest(c, func(request struct {
TenantID string `uri:"tenant_id" validate:"required"`
TaskID string `uri:"task_id" validate:"required"`
TaskID string `uri:"id" validate:"required"`
}) {
c.JSON(http.StatusOK, service.FetchPluginInstallationTask(request.TenantID, request.TaskID))
})
Expand Down
2 changes: 1 addition & 1 deletion internal/server/http_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (app *App) endpointManagementGroup(group *gin.RouterGroup) {
func (app *App) pluginManagementGroup(group *gin.RouterGroup, config *app.Config) {
group.POST("/install/upload", controllers.UploadPlugin(config))
group.POST("/install/identifiers", controllers.InstallPluginFromIdentifiers(config))
group.GET("/install/task/:id", controllers.FetchPluginInstallationTask)
group.GET("/install/tasks/:id", controllers.FetchPluginInstallationTask)
group.GET("/install/tasks", controllers.FetchPluginInstallationTasks)
group.GET("/fetch/manifest", controllers.FetchPluginManifest)
group.GET("/fetch/identifier", controllers.FetchPluginFromIdentifier)
Expand Down
10 changes: 8 additions & 2 deletions internal/service/install_plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ func InstallPluginFromIdentifiers(

task := &models.InstallTask{
Status: models.InstallTaskStatusRunning,
TotalPlugins: len(plugins_wait_for_installation),
TenantID: tenant_id,
TotalPlugins: len(plugin_unique_identifiers),
CompletedPlugins: 0,
Plugins: []models.InstallTaskPluginStatus{},
}
Expand Down Expand Up @@ -276,10 +277,15 @@ func UninstallPlugin(
return entities.NewErrorResponse(-500, err.Error())
}

plugin_unique_identifier, err := plugin_entities.NewPluginUniqueIdentifier(installation.PluginUniqueIdentifier)
if err != nil {
return entities.NewErrorResponse(-500, fmt.Sprintf("failed to parse plugin unique identifier: %v", err))
}

// Uninstall the plugin
_, err = curd.UninstallPlugin(
tenant_id,
plugin_entities.PluginUniqueIdentifier(installation.PluginUniqueIdentifier),
plugin_unique_identifier,
installation.ID,
)
if err != nil {
Expand Down
1 change: 1 addition & 0 deletions internal/types/models/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ type InstallTaskPluginStatus struct {
type InstallTask struct {
Model
Status InstallTaskStatus `json:"status" gorm:"not null"`
TenantID string `json:"tenant_id" gorm:"type:uuid;not null"`
TotalPlugins int `json:"total_plugins" gorm:"not null"`
CompletedPlugins int `json:"completed_plugins" gorm:"not null"`
Plugins []InstallTaskPluginStatus `json:"plugins" gorm:"serializer:json"`
Expand Down

0 comments on commit fc7bfc9

Please sign in to comment.