Skip to content

Commit 2053281

Browse files
Ticket #118 : Move all the plugin options in plugin.json file
1 parent 6d99920 commit 2053281

File tree

20 files changed

+100
-81
lines changed

20 files changed

+100
-81
lines changed

src/EventMesh/FaasNet.EventMesh.Plugin/FaasNet.EventMesh.Plugin.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,7 @@
66
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="6.0.0" />
77
<PackageReference Include="McMaster.NETCore.Plugins" Version="1.4.0" />
88
</ItemGroup>
9+
<ItemGroup>
10+
<ProjectReference Include="..\..\Common\FaasNet.Common\FaasNet.Common.csproj" />
11+
</ItemGroup>
912
</Project>

src/EventMesh/FaasNet.EventMesh.Plugin/PluginConfigurationFile.cs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,19 +20,5 @@ public static PluginEntry Read(string directory)
2020
return pluginEntry;
2121
}
2222
}
23-
24-
public static void Write(string directory, PluginEntry entry)
25-
{
26-
lock (_lock)
27-
{
28-
var appsettingsFilePath = Path.Combine(directory, PluginConstants.ConfigurationFileName);
29-
if (!File.Exists(appsettingsFilePath)) return;
30-
var json = JsonSerializer.Serialize<PluginEntry>(entry, new JsonSerializerOptions
31-
{
32-
PropertyNameCaseInsensitive = true
33-
});
34-
File.WriteAllText(appsettingsFilePath, json);
35-
}
36-
}
3723
}
3824
}

src/EventMesh/FaasNet.EventMesh.Plugin/PluginEntry.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@ public class PluginEntry
55
public string Name { get; set; }
66
public string Description { get; set; }
77
public string DllName { get; set; }
8-
public object Options { get; set; }
98
}
109
}

src/EventMesh/FaasNet.EventMesh.Plugin/PluginEntryDiscovery.cs

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using McMaster.NETCore.Plugins;
1+
using FaasNet.Common;
2+
using McMaster.NETCore.Plugins;
23
using Microsoft.Extensions.DependencyInjection;
34
using System;
45
using System.Collections.Generic;
@@ -32,41 +33,43 @@ public static bool TryExtract(string pluginDirectoryPath, IEnumerable<string> ac
3233
var types = assembly.GetTypes();
3334
var pluginType = types.FirstOrDefault(t => t.GetInterfaces().Any(i => i.IsGenericType && i.GetGenericTypeDefinition() == typeof(IPlugin<>)));
3435
if (pluginType == null) return false;
35-
var optionType = pluginType.GetInterfaces()[0].GenericTypeArguments[0];
36-
var ttt = PluginEntryOption.Extract(optionType);
37-
dynamic pluginConfiguration = Activator.CreateInstance(optionType);
3836
var serializedPluginConfiguration = string.Empty;
39-
if (pluginEntry.Options != null) serializedPluginConfiguration = JsonSerializer.Serialize(pluginEntry.Options);
40-
if (!string.IsNullOrWhiteSpace(serializedPluginConfiguration)) pluginConfiguration = JsonSerializer.Deserialize(serializedPluginConfiguration, optionType, new JsonSerializerOptions
41-
{
42-
PropertyNameCaseInsensitive = true
43-
});
44-
discoveryPlugin = new DiscoveredPlugin(pluginType, pluginConfiguration);
37+
discoveryPlugin = new DiscoveredPlugin(pluginEntry.Name, pluginType);
4538
return true;
4639
}
4740

4841
private class DiscoveredPlugin : IDiscoveredPlugin
4942
{
43+
private readonly string _pluginName;
5044
private readonly Type _pluginType;
51-
private readonly dynamic _pluginOption;
5245

53-
public DiscoveredPlugin(Type pluginType, dynamic pluginOptions)
46+
public DiscoveredPlugin(string pluginName, Type pluginType)
5447
{
48+
_pluginName = pluginName;
5549
_pluginType = pluginType;
56-
_pluginOption = pluginOptions;
5750
}
5851

59-
public void Load(IServiceCollection services)
52+
public void Load(ServerBuilder serverBuilder)
6053
{
54+
var pluginStore = serverBuilder.ServiceProvider.GetRequiredService<IPluginStore>();
55+
var optionType = _pluginType.GetInterfaces()[0].GenericTypeArguments[0];
56+
dynamic pluginConfiguration = Activator.CreateInstance(optionType);
57+
var pluginOption = pluginStore.GetOption(_pluginName);
58+
var serializedPluginConfiguration = string.Empty;
59+
if (pluginOption != null) serializedPluginConfiguration = JsonSerializer.Serialize(pluginOption);
60+
if (!string.IsNullOrWhiteSpace(serializedPluginConfiguration)) pluginConfiguration = JsonSerializer.Deserialize(serializedPluginConfiguration, optionType, new JsonSerializerOptions
61+
{
62+
PropertyNameCaseInsensitive = true
63+
});
6164
var pluginInstance = Activator.CreateInstance(_pluginType);
6265
var loadFn = _pluginType.GetMethod("Load", BindingFlags.Public | BindingFlags.Instance);
63-
loadFn.Invoke(pluginInstance, new object[] { services, _pluginOption });
66+
loadFn.Invoke(pluginInstance, new object[] { serverBuilder.Services, pluginConfiguration });
6467
}
6568
}
6669
}
6770

6871
public interface IDiscoveredPlugin
6972
{
70-
void Load(IServiceCollection services);
73+
void Load(ServerBuilder services);
7174
}
7275
}

src/EventMesh/FaasNet.EventMesh.Runtime/Stores/PluginStore.cs renamed to src/EventMesh/FaasNet.EventMesh.Plugin/PluginStore.cs

Lines changed: 39 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44
using System.Linq;
55
using System.Text.Json;
66

7-
namespace FaasNet.EventMesh.Runtime.Stores
7+
namespace FaasNet.EventMesh.Plugin
88
{
99
public interface IPluginStore
1010
{
1111
IEnumerable<string> GetActivePlugins();
12+
object GetOption(string pluginName);
1213
void Enable(string pluginName);
1314
void Disable(string pluginName);
15+
bool UpdateOptions(string pluginName, object options);
1416
bool IsActive(string pluginName);
1517
}
1618

@@ -25,6 +27,12 @@ public IEnumerable<string> GetActivePlugins()
2527
return plugins.Where(p => p.IsActive).Select(p => p.Name);
2628
}
2729

30+
public object GetOption(string pluginName)
31+
{
32+
var plugins = Read();
33+
return plugins.FirstOrDefault(p => p.Name == pluginName)?.Options;
34+
}
35+
2836
public void Enable(string pluginName)
2937
{
3038
Update(pluginName, true);
@@ -35,6 +43,31 @@ public void Disable(string pluginName)
3543
Update(pluginName, false);
3644
}
3745

46+
public bool UpdateOptions(string pluginName, object options)
47+
{
48+
var records = Read();
49+
var record = records.FirstOrDefault(r => r.Name == pluginName);
50+
if (record == null)
51+
{
52+
records.Add(new PluginRecord
53+
{
54+
Name = pluginName,
55+
IsActive = false,
56+
Options = options
57+
});
58+
}
59+
else record.Options = options;
60+
lock (_lock)
61+
{
62+
File.WriteAllText(GetPluginFilePath(), JsonSerializer.Serialize(records, new JsonSerializerOptions
63+
{
64+
PropertyNameCaseInsensitive = true
65+
}));
66+
}
67+
68+
return true;
69+
}
70+
3871
public bool IsActive(string pluginName)
3972
{
4073
var pluginRecords = Read();
@@ -54,7 +87,7 @@ private void Update(string pluginName, bool isEnabled)
5487
});
5588
}
5689
else record.IsActive = isEnabled;
57-
lock(_lock)
90+
lock (_lock)
5891
{
5992
File.WriteAllText(GetPluginFilePath(), JsonSerializer.Serialize(records, new JsonSerializerOptions
6093
{
@@ -65,7 +98,7 @@ private void Update(string pluginName, bool isEnabled)
6598

6699
private ICollection<PluginRecord> Read()
67100
{
68-
lock(_lock)
101+
lock (_lock)
69102
{
70103
var path = GetPluginFilePath();
71104
if (!File.Exists(path)) return new List<PluginRecord>();
@@ -79,6 +112,8 @@ private ICollection<PluginRecord> Read()
79112

80113
private static string GetPluginFilePath()
81114
{
115+
var env = Environment.GetEnvironmentVariable("EVENTMESH_PLUGIN");
116+
if (!string.IsNullOrWhiteSpace(env)) return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"plugin.{env}.json");
82117
var path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, FILE_NAME);
83118
return path;
84119
}
@@ -87,6 +122,7 @@ private class PluginRecord
87122
{
88123
public string Name { get; set; }
89124
public bool IsActive { get; set; }
125+
public object Options { get; set; }
90126
}
91127
}
92128
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
22
"Name": "ProtocolAmqp",
33
"Description": "Support AMQP protocol",
4-
"DllName": "FaasNet.EventMesh.Protocols.AMQP.dll",
5-
"Options": {
6-
}
4+
"DllName": "FaasNet.EventMesh.Protocols.AMQP.dll"
75
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
{
22
"Name": "ProtocolWebsocket",
33
"Description": "Support Websocket protocol",
4-
"DllName": "FaasNet.EventMesh.Protocols.WebSocket.dll",
5-
"Options": {
6-
}
4+
"DllName": "FaasNet.EventMesh.Protocols.WebSocket.dll"
75
}

src/EventMesh/FaasNet.EventMesh.Runtime/Handlers/DisablePluginMessageHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FaasNet.EventMesh.Client.Messages;
2+
using FaasNet.EventMesh.Plugin;
23
using FaasNet.EventMesh.Runtime.Stores;
34
using FaasNet.RaftConsensus.Core;
45
using Microsoft.Extensions.Options;

src/EventMesh/FaasNet.EventMesh.Runtime/Handlers/EnablePluginMessageHandler.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
using FaasNet.EventMesh.Client.Messages;
2+
using FaasNet.EventMesh.Plugin;
23
using FaasNet.EventMesh.Runtime.Stores;
34
using FaasNet.RaftConsensus.Core;
45
using Microsoft.Extensions.Options;

src/EventMesh/FaasNet.EventMesh.Runtime/Handlers/GetPluginConfigurationMessageHandler.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,12 @@ namespace FaasNet.EventMesh.Runtime.Handlers
1616
public class GetPluginConfigurationMessageHandler : IMessageHandler
1717
{
1818
private readonly EventMeshNodeOptions _options;
19+
private readonly IPluginStore _pluginStore;
1920

20-
public GetPluginConfigurationMessageHandler(IOptions<EventMeshNodeOptions> options)
21+
public GetPluginConfigurationMessageHandler(IOptions<EventMeshNodeOptions> options, IPluginStore pluginStore)
2122
{
2223
_options = options.Value;
24+
_pluginStore = pluginStore;
2325
}
2426

2527
public Commands Command => Commands.GET_PLUGIN_CONFIGURATION_REQUEST;
@@ -52,7 +54,8 @@ public bool TryExtractConfiguration(string subPath, string name, out List<Plugin
5254
dynamic pluginConfiguration = Activator.CreateInstance(optionType);
5355
var entries = PluginEntryOption.Extract(optionType);
5456
var serializedPluginConfiguration = string.Empty;
55-
if (pluginEntry.Options != null) serializedPluginConfiguration = JsonSerializer.Serialize(pluginEntry.Options);
57+
var pluginOption = _pluginStore.GetOption(pluginEntry.Name);
58+
if (pluginOption != null) serializedPluginConfiguration = JsonSerializer.Serialize(pluginOption);
5659
if (!string.IsNullOrWhiteSpace(serializedPluginConfiguration)) pluginConfiguration = JsonSerializer.Deserialize(serializedPluginConfiguration, optionType, new JsonSerializerOptions
5760
{
5861
PropertyNameCaseInsensitive = true

0 commit comments

Comments
 (0)