| 89 | } |
| 90 | |
| 91 | func doLoad(memoryLoader pluginsLoader, dirLoader pluginsLoader, logger *log.Helper) (plugins sdk.AvailablePlugins, err error) { |
| 92 | defer func() { |
| 93 | if err != nil { |
| 94 | // If there is an error, we need to clean up the plugins that were loaded |
| 95 | plugins.Cleanup() |
| 96 | } |
| 97 | }() |
| 98 | |
| 99 | // Get actual plugins running processes |
| 100 | plugins, err = dirLoader.load() |
| 101 | if err != nil { |
| 102 | return plugins, fmt.Errorf("failed to load plugins: %w", err) |
| 103 | } |
| 104 | |
| 105 | fromMemoryPlugins, err := memoryLoader.load() |
| 106 | if err != nil { |
| 107 | return plugins, fmt.Errorf("failed to load plugins: %w", err) |
| 108 | } |
| 109 | |
| 110 | // load built-in plugins but skip them if they are loaded already as actual plugins |
| 111 | // It will enable us to rollout plugins iteratively |
| 112 | BUILT_IN_LOOP: |
| 113 | for _, builtInPlugin := range fromMemoryPlugins { |
| 114 | for _, p := range plugins { |
| 115 | if p.Describe().ID == builtInPlugin.Describe().ID { |
| 116 | logger.Infow("msg", "plugin already loaded", "type", "built-in", "plugin", p.String()) |
| 117 | continue BUILT_IN_LOOP |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | logger.Infow("msg", "loaded", "type", "built-in", "plugin", builtInPlugin.String()) |
| 122 | plugins = append(plugins, &sdk.FanOutP{FanOut: builtInPlugin, DisposeFunc: func() {}}) |
| 123 | } |
| 124 | |
| 125 | sort.Slice(plugins, func(i, j int) bool { |
| 126 | return plugins[i].Describe().ID < plugins[j].Describe().ID |
| 127 | }) |
| 128 | |
| 129 | return plugins, nil |
| 130 | } |
| 131 | |
| 132 | func (l *memoryLoader) load() (sdk.AvailablePlugins, error) { |
| 133 | res := make(sdk.AvailablePlugins, 0, len(l.plugins)) |