serverless.go 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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, err := model.GetDeclaration()
  30. if err != nil {
  31. return nil, err
  32. }
  33. // init runtime entity
  34. runtime_entity := plugin_entities.PluginRuntime{
  35. Config: *declaration,
  36. }
  37. runtime_entity.InitState()
  38. // convert to plugin runtime
  39. plugin_runtime := aws_manager.AWSPluginRuntime{
  40. PositivePluginRuntime: positive_manager.PositivePluginRuntime{
  41. BasicPluginRuntime: basic_manager.NewBasicPluginRuntime(p.mediaManager),
  42. InnerChecksum: model.Checksum,
  43. },
  44. PluginRuntime: runtime_entity,
  45. LambdaURL: model.FunctionURL,
  46. LambdaName: model.FunctionName,
  47. }
  48. if err := plugin_runtime.InitEnvironment(); err != nil {
  49. return nil, err
  50. }
  51. return &plugin_runtime, nil
  52. }
  53. func (p *PluginManager) getServerlessPluginRuntimeModel(
  54. identity plugin_entities.PluginUniqueIdentifier,
  55. ) (*models.ServerlessRuntime, error) {
  56. // check if plugin is a serverless runtime
  57. runtime, err := cache.Get[models.ServerlessRuntime](
  58. p.getServerlessRuntimeCacheKey(identity),
  59. )
  60. if err != nil && err != cache.ErrNotFound {
  61. return nil, errors.New("plugin not found")
  62. }
  63. if err == cache.ErrNotFound {
  64. runtime_model, err := db.GetOne[models.ServerlessRuntime](
  65. db.Equal("plugin_unique_identifier", identity.String()),
  66. )
  67. if err == db.ErrDatabaseNotFound {
  68. return nil, errors.New("plugin not found")
  69. }
  70. if err != nil {
  71. return nil, fmt.Errorf("failed to load serverless runtime from db: %v", err)
  72. }
  73. cache.Store(p.getServerlessRuntimeCacheKey(identity), runtime_model, time.Minute*30)
  74. runtime = &runtime_model
  75. } else if err != nil {
  76. return nil, fmt.Errorf("failed to load serverless runtime from cache: %v", err)
  77. }
  78. return runtime, nil
  79. }