serverless.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. package plugin_manager
  2. import (
  3. "errors"
  4. "fmt"
  5. "time"
  6. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/aws_manager"
  7. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/basic_manager"
  8. "github.com/langgenius/dify-plugin-daemon/internal/core/plugin_manager/positive_manager"
  9. "github.com/langgenius/dify-plugin-daemon/internal/db"
  10. "github.com/langgenius/dify-plugin-daemon/internal/types/entities/plugin_entities"
  11. "github.com/langgenius/dify-plugin-daemon/internal/types/models"
  12. "github.com/langgenius/dify-plugin-daemon/internal/utils/cache"
  13. )
  14. const (
  15. PLUGIN_SERVERLESS_CACHE_KEY = "serverless:runtime:%s"
  16. )
  17. func (p *PluginManager) getServerlessRuntimeCacheKey(
  18. identity plugin_entities.PluginUniqueIdentifier,
  19. ) string {
  20. return fmt.Sprintf(PLUGIN_SERVERLESS_CACHE_KEY, identity.String())
  21. }
  22. func (p *PluginManager) getServerlessPluginRuntime(
  23. identity plugin_entities.PluginUniqueIdentifier,
  24. ) (plugin_entities.PluginLifetime, error) {
  25. model, err := p.getServerlessPluginRuntimeModel(identity)
  26. if err != nil {
  27. return nil, err
  28. }
  29. declaration := model.Declaration
  30. // init runtime entity
  31. runtime_entity := plugin_entities.PluginRuntime{
  32. Config: declaration,
  33. }
  34. runtime_entity.InitState()
  35. // convert to plugin runtime
  36. plugin_runtime := aws_manager.AWSPluginRuntime{
  37. PositivePluginRuntime: positive_manager.PositivePluginRuntime{
  38. BasicPluginRuntime: basic_manager.NewBasicPluginRuntime(p.mediaManager),
  39. InnerChecksum: model.Checksum,
  40. },
  41. PluginRuntime: runtime_entity,
  42. LambdaURL: model.FunctionURL,
  43. LambdaName: model.FunctionName,
  44. }
  45. if err := plugin_runtime.InitEnvironment(); err != nil {
  46. return nil, err
  47. }
  48. return &plugin_runtime, nil
  49. }
  50. func (p *PluginManager) getServerlessPluginRuntimeModel(
  51. identity plugin_entities.PluginUniqueIdentifier,
  52. ) (*models.ServerlessRuntime, error) {
  53. // check if plugin is a serverless runtime
  54. runtime, err := cache.Get[models.ServerlessRuntime](
  55. p.getServerlessRuntimeCacheKey(identity),
  56. )
  57. if err != nil && err != cache.ErrNotFound {
  58. return nil, errors.New("plugin not found")
  59. }
  60. if err == cache.ErrNotFound {
  61. runtime_model, err := db.GetOne[models.ServerlessRuntime](
  62. db.Equal("plugin_unique_identifier", identity.String()),
  63. )
  64. if err == db.ErrDatabaseNotFound {
  65. return nil, errors.New("plugin not found")
  66. }
  67. if err != nil {
  68. return nil, fmt.Errorf("failed to load serverless runtime from db: %v", err)
  69. }
  70. cache.Store(p.getServerlessRuntimeCacheKey(identity), runtime_model, time.Minute*30)
  71. runtime = &runtime_model
  72. } else if err != nil {
  73. return nil, fmt.Errorf("failed to load serverless runtime from cache: %v", err)
  74. }
  75. return runtime, nil
  76. }