@@ -24,6 +24,7 @@ import (
2424 "github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
2525 "github.com/langgenius/dify-plugin-daemon/internal/utils/cache/helper"
2626 "github.com/langgenius/dify-plugin-daemon/internal/utils/encryption"
27+ "github.com/langgenius/dify-plugin-daemon/internal/utils/log"
2728 "github.com/langgenius/dify-plugin-daemon/internal/utils/routine"
2829 "github.com/langgenius/dify-plugin-daemon/pkg/entities"
2930 "github.com/langgenius/dify-plugin-daemon/pkg/entities/endpoint_entities"
@@ -219,28 +220,45 @@ func Endpoint(
219220 }
220221}
221222
222- func EnableEndpoint (endpoint_id string , tenant_id string ) * entities.Response {
223-
224- if err := install_service . EnabledEndpoint ( endpoint_id , tenant_id ); err != nil {
225- return exception . InternalServerError ( errors . New ( "failed to enable endpoint" )). ToResponse ( )
223+ func EnableEndpoint (endpointID string , tenantID string ) * entities.Response {
224+ endpoint , err := install_service . EnabledEndpoint ( endpointID , tenantID )
225+ if err != nil {
226+ return handleEndpointStateError ( err , " enable" )
226227 }
227228
229+ invalidateEndpointCache (endpoint .HookID )
230+
228231 return entities .NewSuccessResponse (true )
229232}
230233
231- func DisableEndpoint (endpoint_id string , tenant_id string ) * entities.Response {
232- endpoint , err := install_service .DisabledEndpoint (endpoint_id , tenant_id )
234+ func DisableEndpoint (endpointID string , tenantID string ) * entities.Response {
235+ endpoint , err := install_service .DisabledEndpoint (endpointID , tenantID )
233236 if err != nil {
234- return exception . InternalServerError ( errors . New ( "failed to disable endpoint" )). ToResponse ( )
237+ return handleEndpointStateError ( err , " disable" )
235238 }
236239
237- // invalidate endpoint cache
238- endpointCacheKey := helper .EndpointCacheKey (endpoint .HookID )
239- _ , _ = cache.AutoDelete [models.Endpoint ](endpointCacheKey )
240+ invalidateEndpointCache (endpoint .HookID )
240241
241242 return entities .NewSuccessResponse (true )
242243}
243244
245+ func handleEndpointStateError (err error , action string ) * entities.Response {
246+ if err == nil {
247+ return nil
248+ }
249+ if errors .Is (err , db .ErrDatabaseNotFound ) {
250+ return exception .NotFoundError (errors .New ("endpoint not found" )).ToResponse ()
251+ }
252+ return exception .InternalServerError (fmt .Errorf ("failed to %s endpoint: %w" , action , err )).ToResponse ()
253+ }
254+
255+ func invalidateEndpointCache (hookID string ) {
256+ endpointCacheKey := helper .EndpointCacheKey (hookID )
257+ if _ , err := cache.AutoDelete [models.Endpoint ](endpointCacheKey ); err != nil {
258+ log .Warn ("failed to invalidate endpoint cache for hookID %s: %v" , hookID , err )
259+ }
260+ }
261+
244262func ListEndpoints (tenant_id string , page int , page_size int ) * entities.Response {
245263 endpoints , err := db .GetAll [models.Endpoint ](
246264 db .Equal ("tenant_id" , tenant_id ),
0 commit comments