1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/*
* Copyright (c) 2022 Vaughn Nugent
*
* Library: VNLib
* Package: VNLib.Plugins.Extensions.VNCache
* File: VNCacheExtensions.cs
*
* VNCacheExtensions.cs is part of VNLib.Plugins.Extensions.VNCache which is part of the larger
* VNLib collection of libraries and utilities.
*
* VNLib.Plugins.Extensions.VNCache is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* VNLib.Plugins.Extensions.VNCache is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see https://www.gnu.org/licenses/.
*/
using System;
using System.Text.Json;
using System.Threading.Tasks;
using System.Collections.Generic;
using VNLib.Utils.Logging;
using VNLib.Data.Caching;
using VNLib.Data.Caching.Extensions;
using VNLib.Plugins.Extensions.Loading;
namespace VNLib.Plugins.Extensions.VNCache
{
/// <summary>
/// Contains extension methods for aquiring a Plugin managed
/// global cache provider.
/// </summary>
public static class VNCacheExtensions
{
/// <summary>
/// Loads the shared cache provider for the current plugin
/// </summary>
/// <param name="pbase"></param>
/// <param name="localized">A localized log provider to write cache logging information to</param>
/// <returns>The shared <see cref="IGlobalCacheProvider"/> </returns>
/// <remarks>
/// The returned instance, background work, logging, and its lifetime
/// are managed by the current plugin. Beware when calling this method
/// network connections may be spawend and managed in the background by
/// this library.
/// </remarks>
public static VnCacheClient GetGlobalCache(this PluginBase pbase, ILogProvider? localized = null)
=> LoadingExtensions.GetOrCreateSingleton<VnCacheClient>(pbase, localized == null ? LoadCacheClient : (pbase) => LoadCacheClient(pbase, localized));
private static VnCacheClient LoadCacheClient(PluginBase pbase) => LoadCacheClient(pbase, pbase.Log);
private static VnCacheClient LoadCacheClient(PluginBase pbase, ILogProvider localized)
{
//Get config for client
IReadOnlyDictionary<string, JsonElement> config = pbase.GetConfigForType<VnCacheClient>();
//Init client
ILogProvider? debugLog = pbase.IsDebug() ? pbase.Log : null;
VnCacheClient client = new(debugLog);
//Begin cache connections by scheduling a task on the plugin's scheduler
_ = pbase.ObserveTask(() => RunClientAsync(pbase, config, localized, client), 250);
return client;
}
private static async Task RunClientAsync(PluginBase pbase, IReadOnlyDictionary<string, JsonElement> config, ILogProvider localized, VnCacheClient client)
{
ILogProvider Log = localized;
try
{
//Try loading config
await client.LoadConfigAsync(pbase, config);
Log.Verbose("VNCache client configration loaded successfully");
//Run and wait for exit
await client.RunAsync(Log, pbase.UnloadToken);
}
catch (OperationCanceledException)
{ }
catch (KeyNotFoundException e)
{
Log.Error("Missing required configuration variable for VnCache client: {0}", e.Message);
}
catch (FBMServerNegiationException fne)
{
Log.Error("Failed to negotiate connection with cache server {reason}", fne.Message);
}
catch (Exception ex)
{
Log.Error(ex, "Unhandled exception occured in background cache client listening task");
}
finally
{
client.Dispose();
}
Log.Information("Cache client exited");
}
}
}
|