mirror of
https://github.com/navidrome/navidrome.git
synced 2025-07-13 23:21:21 +03:00
* refactor: implement OnSchedulerCallback method in wasmSchedulerCallback Added the OnSchedulerCallback method to the wasmSchedulerCallback struct, enabling it to handle scheduler callback events. This method constructs a SchedulerCallbackRequest and invokes the corresponding plugin method, facilitating better integration with the scheduling system. The changes improve the plugin's ability to respond to scheduled events, enhancing overall functionality. Signed-off-by: Deluan <deluan@navidrome.org> * fix(plugins): update executeCallback method to use callMethod Modified the executeCallback method to accept an additional parameter, methodName, which specifies the callback method to be executed. This change ensures that the correct method is called for each WebSocket event, improving the accuracy of callback execution for plugins. Signed-off-by: Deluan <deluan@navidrome.org> * fix(plugins): capture OnInit metrics Signed-off-by: Deluan <deluan@navidrome.org> * fix(plugins): improve logging for metrics in callMethod Updated the logging statement in the callMethod function to include the elapsed time as a separate key in the log output. This change enhances the clarity of the logged metrics, making it easier to analyze the performance of plugin requests and troubleshoot any issues that may arise. Signed-off-by: Deluan <deluan@navidrome.org> * fix(plugins): enhance logging for schedule callback execution Signed-off-by: Deluan <deluan@navidrome.org> * refactor(server): streamline scrobbler stopping logic Refactored the logic for stopping scrobbler instances when they are removed. The new implementation introduces a `stoppableScrobbler` interface to simplify the type assertion process, allowing for a more concise and readable code structure. This change ensures that any scrobbler implementing the `Stop` method is properly stopped before removal, improving the overall reliability of the plugin management system. Signed-off-by: Deluan <deluan@navidrome.org> * fix(plugins): improve plugin lifecycle management and error handling Enhanced the plugin lifecycle management by implementing error handling in the OnInit method. The changes include the addition of specific error conditions that can be returned during plugin initialization, allowing for better management of plugin states. Additionally, the unregisterPlugin method was updated to ensure proper cleanup of plugins that fail to initialize, improving overall stability and reliability of the plugin system. Signed-off-by: Deluan <deluan@navidrome.org> * refactor(plugins): remove unused LoadAllPlugins and related methods Eliminated the LoadAllPlugins, LoadAllMediaAgents, and LoadAllScrobblers methods from the manager implementation as they were not utilized in the codebase. This cleanup reduces complexity and improves maintainability by removing redundant code, allowing for a more streamlined plugin management process. Signed-off-by: Deluan <deluan@navidrome.org> * fix(plugins): update logging configuration for plugins Configured logging for multiple plugins to remove timestamps and source file/line information, while adding specific prefixes for better identification. Signed-off-by: Deluan <deluan@navidrome.org> * fix(plugins): clear initialization state when unregistering a plugin Added functionality to clear the initialization state of a plugin in the lifecycle manager when it is unregistered. This change ensures that the lifecycle state is accurately maintained, preventing potential issues with plugins that may be re-registered after being unregistered. The new method `clearInitialized` was implemented to handle this state management. Signed-off-by: Deluan <deluan@navidrome.org> * test: add unit tests for convertError function, rename to checkErr Added comprehensive unit tests for the convertError function to ensure correct behavior across various scenarios, including handling nil responses, typed nils, and responses implementing errorResponse. These tests validate that the function returns the expected results without panicking and correctly wraps original errors when necessary. Signed-off-by: Deluan <deluan@navidrome.org> * fix(plugins): update plugin base implementation and method calls Refactored the plugin base implementation by renaming `wasmBasePlugin` to `baseCapability` across multiple files. Updated method calls in the `wasmMediaAgent`, `wasmSchedulerCallback`, and `wasmScrobblerPlugin` to align with the new base structure. These changes improve code clarity and maintainability by standardizing the plugin architecture, ensuring consistent usage of the base capabilities across different plugin types. Signed-off-by: Deluan <deluan@navidrome.org> * fix(discord): handle failed connections and improve heartbeat checks Added a new method to clean up failed connections, which cancels the heartbeat schedule, closes the WebSocket connection, and removes cache entries. Enhanced the heartbeat check to log failures and trigger the cleanup process on the first failure. These changes ensure better management of user connections and improve the overall reliability of the RPC system. Signed-off-by: Deluan <deluan@navidrome.org> --------- Signed-off-by: Deluan <deluan@navidrome.org>
167 lines
5.7 KiB
Go
167 lines
5.7 KiB
Go
package plugins
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/navidrome/navidrome/core/agents"
|
|
"github.com/navidrome/navidrome/log"
|
|
"github.com/navidrome/navidrome/plugins/api"
|
|
"github.com/tetratelabs/wazero"
|
|
)
|
|
|
|
// NewWasmMediaAgent creates a new adapter for a MetadataAgent plugin
|
|
func newWasmMediaAgent(wasmPath, pluginID string, m *managerImpl, runtime api.WazeroNewRuntime, mc wazero.ModuleConfig) WasmPlugin {
|
|
loader, err := api.NewMetadataAgentPlugin(context.Background(), api.WazeroRuntime(runtime), api.WazeroModuleConfig(mc))
|
|
if err != nil {
|
|
log.Error("Error creating media metadata service plugin", "plugin", pluginID, "path", wasmPath, err)
|
|
return nil
|
|
}
|
|
return &wasmMediaAgent{
|
|
baseCapability: newBaseCapability[api.MetadataAgent, *api.MetadataAgentPlugin](
|
|
wasmPath,
|
|
pluginID,
|
|
CapabilityMetadataAgent,
|
|
m.metrics,
|
|
loader,
|
|
func(ctx context.Context, l *api.MetadataAgentPlugin, path string) (api.MetadataAgent, error) {
|
|
return l.Load(ctx, path)
|
|
},
|
|
),
|
|
}
|
|
}
|
|
|
|
// wasmMediaAgent adapts a MetadataAgent plugin to implement the agents.Interface
|
|
type wasmMediaAgent struct {
|
|
*baseCapability[api.MetadataAgent, *api.MetadataAgentPlugin]
|
|
}
|
|
|
|
func (w *wasmMediaAgent) AgentName() string {
|
|
return w.id
|
|
}
|
|
|
|
func (w *wasmMediaAgent) mapError(err error) error {
|
|
if err != nil && (err.Error() == api.ErrNotFound.Error() || err.Error() == api.ErrNotImplemented.Error()) {
|
|
return agents.ErrNotFound
|
|
}
|
|
return err
|
|
}
|
|
|
|
// Album-related methods
|
|
|
|
func (w *wasmMediaAgent) GetAlbumInfo(ctx context.Context, name, artist, mbid string) (*agents.AlbumInfo, error) {
|
|
res, err := callMethod(ctx, w, "GetAlbumInfo", func(inst api.MetadataAgent) (*api.AlbumInfoResponse, error) {
|
|
return inst.GetAlbumInfo(ctx, &api.AlbumInfoRequest{Name: name, Artist: artist, Mbid: mbid})
|
|
})
|
|
if err != nil {
|
|
return nil, w.mapError(err)
|
|
}
|
|
if res == nil || res.Info == nil {
|
|
return nil, agents.ErrNotFound
|
|
}
|
|
info := res.Info
|
|
return &agents.AlbumInfo{
|
|
Name: info.Name,
|
|
MBID: info.Mbid,
|
|
Description: info.Description,
|
|
URL: info.Url,
|
|
}, nil
|
|
}
|
|
|
|
func (w *wasmMediaAgent) GetAlbumImages(ctx context.Context, name, artist, mbid string) ([]agents.ExternalImage, error) {
|
|
res, err := callMethod(ctx, w, "GetAlbumImages", func(inst api.MetadataAgent) (*api.AlbumImagesResponse, error) {
|
|
return inst.GetAlbumImages(ctx, &api.AlbumImagesRequest{Name: name, Artist: artist, Mbid: mbid})
|
|
})
|
|
if err != nil {
|
|
return nil, w.mapError(err)
|
|
}
|
|
return convertExternalImages(res.Images), nil
|
|
}
|
|
|
|
// Artist-related methods
|
|
|
|
func (w *wasmMediaAgent) GetArtistMBID(ctx context.Context, id string, name string) (string, error) {
|
|
res, err := callMethod(ctx, w, "GetArtistMBID", func(inst api.MetadataAgent) (*api.ArtistMBIDResponse, error) {
|
|
return inst.GetArtistMBID(ctx, &api.ArtistMBIDRequest{Id: id, Name: name})
|
|
})
|
|
if err != nil {
|
|
return "", w.mapError(err)
|
|
}
|
|
return res.GetMbid(), nil
|
|
}
|
|
|
|
func (w *wasmMediaAgent) GetArtistURL(ctx context.Context, id, name, mbid string) (string, error) {
|
|
res, err := callMethod(ctx, w, "GetArtistURL", func(inst api.MetadataAgent) (*api.ArtistURLResponse, error) {
|
|
return inst.GetArtistURL(ctx, &api.ArtistURLRequest{Id: id, Name: name, Mbid: mbid})
|
|
})
|
|
if err != nil {
|
|
return "", w.mapError(err)
|
|
}
|
|
return res.GetUrl(), nil
|
|
}
|
|
|
|
func (w *wasmMediaAgent) GetArtistBiography(ctx context.Context, id, name, mbid string) (string, error) {
|
|
res, err := callMethod(ctx, w, "GetArtistBiography", func(inst api.MetadataAgent) (*api.ArtistBiographyResponse, error) {
|
|
return inst.GetArtistBiography(ctx, &api.ArtistBiographyRequest{Id: id, Name: name, Mbid: mbid})
|
|
})
|
|
if err != nil {
|
|
return "", w.mapError(err)
|
|
}
|
|
return res.GetBiography(), nil
|
|
}
|
|
|
|
func (w *wasmMediaAgent) GetSimilarArtists(ctx context.Context, id, name, mbid string, limit int) ([]agents.Artist, error) {
|
|
resp, err := callMethod(ctx, w, "GetSimilarArtists", func(inst api.MetadataAgent) (*api.ArtistSimilarResponse, error) {
|
|
return inst.GetSimilarArtists(ctx, &api.ArtistSimilarRequest{Id: id, Name: name, Mbid: mbid, Limit: int32(limit)})
|
|
})
|
|
if err != nil {
|
|
return nil, w.mapError(err)
|
|
}
|
|
artists := make([]agents.Artist, 0, len(resp.GetArtists()))
|
|
for _, a := range resp.GetArtists() {
|
|
artists = append(artists, agents.Artist{
|
|
Name: a.GetName(),
|
|
MBID: a.GetMbid(),
|
|
})
|
|
}
|
|
return artists, nil
|
|
}
|
|
|
|
func (w *wasmMediaAgent) GetArtistImages(ctx context.Context, id, name, mbid string) ([]agents.ExternalImage, error) {
|
|
resp, err := callMethod(ctx, w, "GetArtistImages", func(inst api.MetadataAgent) (*api.ArtistImageResponse, error) {
|
|
return inst.GetArtistImages(ctx, &api.ArtistImageRequest{Id: id, Name: name, Mbid: mbid})
|
|
})
|
|
if err != nil {
|
|
return nil, w.mapError(err)
|
|
}
|
|
return convertExternalImages(resp.Images), nil
|
|
}
|
|
|
|
func (w *wasmMediaAgent) GetArtistTopSongs(ctx context.Context, id, artistName, mbid string, count int) ([]agents.Song, error) {
|
|
resp, err := callMethod(ctx, w, "GetArtistTopSongs", func(inst api.MetadataAgent) (*api.ArtistTopSongsResponse, error) {
|
|
return inst.GetArtistTopSongs(ctx, &api.ArtistTopSongsRequest{Id: id, ArtistName: artistName, Mbid: mbid, Count: int32(count)})
|
|
})
|
|
if err != nil {
|
|
return nil, w.mapError(err)
|
|
}
|
|
songs := make([]agents.Song, 0, len(resp.GetSongs()))
|
|
for _, s := range resp.GetSongs() {
|
|
songs = append(songs, agents.Song{
|
|
Name: s.GetName(),
|
|
MBID: s.GetMbid(),
|
|
})
|
|
}
|
|
return songs, nil
|
|
}
|
|
|
|
// Helper function to convert ExternalImage objects from the API to the agents package
|
|
func convertExternalImages(images []*api.ExternalImage) []agents.ExternalImage {
|
|
result := make([]agents.ExternalImage, 0, len(images))
|
|
for _, img := range images {
|
|
result = append(result, agents.ExternalImage{
|
|
URL: img.GetUrl(),
|
|
Size: int(img.GetSize()),
|
|
})
|
|
}
|
|
return result
|
|
}
|