| 709 | } |
| 710 | |
| 711 | func SetTaskPluginStatus(c *gin.Context) { |
| 712 | var request taskPluginStatusRequest |
| 713 | if err := c.ShouldBindJSON(&request); err != nil || request.Enabled == nil { |
| 714 | common.ApiErrorMsg(c, "enabled is required") |
| 715 | return |
| 716 | } |
| 717 | key := c.Param("key") |
| 718 | disabledChannels := 0 |
| 719 | unboundChannels := 0 |
| 720 | if !*request.Enabled { |
| 721 | channels, inFlight, usageErr := model.GetTaskPluginUsage(key) |
| 722 | if usageErr != nil { |
| 723 | common.ApiError(c, usageErr) |
| 724 | return |
| 725 | } |
| 726 | cascade := c.Query("cascade") == "true" |
| 727 | force := c.Query("force") == "true" |
| 728 | if (len(channels) > 0 && !cascade) || (inFlight > 0 && !force) { |
| 729 | c.JSON(200, gin.H{"success": false, "message": "task plugin is still in use", "data": gin.H{"channels": channels, "in_flight_count": inFlight}}) |
| 730 | return |
| 731 | } |
| 732 | if cascade { |
| 733 | for _, channel := range channels { |
| 734 | if channel.Type == constant.ChannelTypeNewAPI { |
| 735 | // A gateway channel also carries ordinary traffic, so the |
| 736 | // cascade only drops this plugin from its bindings. |
| 737 | unbound, unbindErr := model.UnbindTaskPlugin(channel.Id, key) |
| 738 | if unbindErr != nil { |
| 739 | common.ApiError(c, unbindErr) |
| 740 | return |
| 741 | } |
| 742 | if unbound { |
| 743 | unboundChannels++ |
| 744 | } |
| 745 | continue |
| 746 | } |
| 747 | if model.UpdateChannelStatus(channel.Id, "", common.ChannelStatusManuallyDisabled, "task plugin disabled") { |
| 748 | disabledChannels++ |
| 749 | } |
| 750 | } |
| 751 | if unboundChannels > 0 { |
| 752 | model.InitChannelCache() |
| 753 | } |
| 754 | } |
| 755 | } |
| 756 | active, lookupErr := model.GetTaskPluginVersion(key, "") |
| 757 | hasActiveOverride := lookupErr == nil |
| 758 | if lookupErr != nil && !errors.Is(lookupErr, gorm.ErrRecordNotFound) { |
| 759 | common.ApiError(c, lookupErr) |
| 760 | return |
| 761 | } |
| 762 | // Switching on serves the active version, or the factory built-in when the |
| 763 | // key has none. |
| 764 | if *request.Enabled && rejectTaskPluginRoutingConflict(c, key, active) { |
| 765 | return |
| 766 | } |
| 767 | response := gin.H{"plugin_enabled": *request.Enabled, "disabled_channels": disabledChannels, "unbound_channels": unboundChannels} |
| 768 | // Switching a key off must silence every layer that can serve it. The |