| 440 | } |
| 441 | |
| 442 | func SetTaskPluginStatus(c *gin.Context) { |
| 443 | var request taskPluginStatusRequest |
| 444 | if err := c.ShouldBindJSON(&request); err != nil || request.Enabled == nil { |
| 445 | common.ApiErrorMsg(c, "enabled is required") |
| 446 | return |
| 447 | } |
| 448 | key := c.Param("key") |
| 449 | disabledChannels := 0 |
| 450 | if !*request.Enabled { |
| 451 | channels, inFlight, usageErr := model.GetTaskPluginUsage(key) |
| 452 | if usageErr != nil { |
| 453 | common.ApiError(c, usageErr) |
| 454 | return |
| 455 | } |
| 456 | cascade := c.Query("cascade") == "true" |
| 457 | force := c.Query("force") == "true" |
| 458 | if (len(channels) > 0 && !cascade) || (inFlight > 0 && !force) { |
| 459 | c.JSON(200, gin.H{"success": false, "message": "task plugin is still in use", "data": gin.H{"channels": channels, "in_flight_count": inFlight}}) |
| 460 | return |
| 461 | } |
| 462 | if cascade { |
| 463 | for _, channel := range channels { |
| 464 | if model.UpdateChannelStatus(channel.Id, "", common.ChannelStatusManuallyDisabled, "task plugin disabled") { |
| 465 | disabledChannels++ |
| 466 | } |
| 467 | } |
| 468 | } |
| 469 | } |
| 470 | _, lookupErr := model.GetTaskPluginVersion(key, "") |
| 471 | hasActiveOverride := lookupErr == nil |
| 472 | if lookupErr != nil && !errors.Is(lookupErr, gorm.ErrRecordNotFound) { |
| 473 | common.ApiError(c, lookupErr) |
| 474 | return |
| 475 | } |
| 476 | // The disabled set suppresses only the factory fallback layer. An enabled |
| 477 | // override for the same key keeps serving and is toggled independently. |
| 478 | if taskPluginHasFactory(key) && !hasActiveOverride { |
| 479 | keys := setting.GetTaskPluginDisabledFactoryKeys() |
| 480 | if *request.Enabled { |
| 481 | next := make([]string, 0, len(keys)) |
| 482 | for _, item := range keys { |
| 483 | if item != key { |
| 484 | next = append(next, item) |
| 485 | } |
| 486 | } |
| 487 | keys = next |
| 488 | } else { |
| 489 | keys = append(append([]string{}, keys...), key) |
| 490 | } |
| 491 | if err := setting.SetTaskPluginDisabledFactoryKeysOption(keys); err != nil { |
| 492 | common.ApiError(c, err) |
| 493 | return |
| 494 | } |
| 495 | encoded, err := common.Marshal(setting.GetTaskPluginDisabledFactoryKeys()) |
| 496 | if err != nil { |
| 497 | common.ApiError(c, err) |
| 498 | return |
| 499 | } |