Fix grain price fetcher
This commit is contained in:
@@ -2,7 +2,7 @@
|
||||
|
||||
namespace Managing.Infrastructure.Databases.InfluxDb.Abstractions;
|
||||
|
||||
public interface IInfluxDbRepository
|
||||
public interface IInfluxDbRepository : IDisposable
|
||||
{
|
||||
string Organization { get; }
|
||||
|
||||
|
||||
@@ -111,9 +111,9 @@ public class CandleRepository : ICandleRepository
|
||||
return results;
|
||||
}
|
||||
|
||||
public async Task InsertCandle(Candle candle)
|
||||
public Task InsertCandle(Candle candle)
|
||||
{
|
||||
await _influxDbRepository.WriteAsync(write =>
|
||||
_influxDbRepository.Write(write =>
|
||||
{
|
||||
PriceDto price = PriceHelpers.Map(candle);
|
||||
write.WriteMeasurement(
|
||||
@@ -121,8 +121,25 @@ public class CandleRepository : ICandleRepository
|
||||
WritePrecision.Ns,
|
||||
_priceBucket,
|
||||
_influxDbRepository.Organization);
|
||||
return Task.CompletedTask;
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public Task InsertCandles(IEnumerable<Candle> candles)
|
||||
{
|
||||
_influxDbRepository.Write(write =>
|
||||
{
|
||||
foreach (var candle in candles)
|
||||
{
|
||||
PriceDto price = PriceHelpers.Map(candle);
|
||||
write.WriteMeasurement(
|
||||
price,
|
||||
WritePrecision.Ns,
|
||||
_priceBucket,
|
||||
_influxDbRepository.Organization);
|
||||
}
|
||||
});
|
||||
return Task.CompletedTask;
|
||||
}
|
||||
|
||||
public void Test(Candle candle)
|
||||
|
||||
@@ -3,10 +3,17 @@ using Managing.Infrastructure.Databases.InfluxDb.Abstractions;
|
||||
|
||||
namespace Managing.Infrastructure.Databases.InfluxDb;
|
||||
|
||||
public class InfluxDbRepository : IInfluxDbRepository
|
||||
public class InfluxDbRepository : IInfluxDbRepository, IDisposable
|
||||
{
|
||||
private readonly string _token;
|
||||
private readonly string _url;
|
||||
private readonly InfluxDBClient _client;
|
||||
private readonly WriteApi _writeApi;
|
||||
private readonly QueryApi _queryApi;
|
||||
private readonly SemaphoreSlim _writeSemaphore = new SemaphoreSlim(1, 1);
|
||||
private readonly SemaphoreSlim _querySemaphore = new SemaphoreSlim(1, 1);
|
||||
private bool _disposed = false;
|
||||
|
||||
public string Organization { get; set; }
|
||||
|
||||
public InfluxDbRepository(IInfluxDbSettings settings)
|
||||
@@ -14,26 +21,85 @@ public class InfluxDbRepository : IInfluxDbRepository
|
||||
_token = settings.Token;
|
||||
_url = settings.Url;
|
||||
Organization = settings.Organization;
|
||||
|
||||
// Create a single client instance that will be reused
|
||||
_client = new InfluxDBClient(_url, _token);
|
||||
_writeApi = _client.GetWriteApi();
|
||||
_queryApi = _client.GetQueryApi();
|
||||
}
|
||||
|
||||
public void Write(Action<WriteApi> action)
|
||||
{
|
||||
using var client = InfluxDBClientFactory.Create(_url, _token);
|
||||
using var write = client.GetWriteApi();
|
||||
action(write);
|
||||
if (_disposed) throw new ObjectDisposedException(nameof(InfluxDbRepository));
|
||||
|
||||
_writeSemaphore.Wait();
|
||||
try
|
||||
{
|
||||
action(_writeApi);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_writeSemaphore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public Task WriteAsync(Func<WriteApi, Task> action)
|
||||
public async Task WriteAsync(Func<WriteApi, Task> action)
|
||||
{
|
||||
// Get write API asynchronously
|
||||
using var client = new InfluxDBClient(_url, _token);
|
||||
using var write = client.GetWriteApi();
|
||||
return action(write);
|
||||
if (_disposed) throw new ObjectDisposedException(nameof(InfluxDbRepository));
|
||||
|
||||
await _writeSemaphore.WaitAsync();
|
||||
try
|
||||
{
|
||||
await action(_writeApi);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_writeSemaphore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public async Task<T> QueryAsync<T>(Func<QueryApi, Task<T>> action)
|
||||
{
|
||||
using var client = InfluxDBClientFactory.Create(_url, _token);
|
||||
var query = client.GetQueryApi();
|
||||
return await action(query);
|
||||
if (_disposed) throw new ObjectDisposedException(nameof(InfluxDbRepository));
|
||||
|
||||
await _querySemaphore.WaitAsync();
|
||||
try
|
||||
{
|
||||
return await action(_queryApi);
|
||||
}
|
||||
finally
|
||||
{
|
||||
_querySemaphore.Release();
|
||||
}
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
if (!_disposed)
|
||||
{
|
||||
try
|
||||
{
|
||||
// Give the WriteApi time to flush any pending writes
|
||||
_writeApi?.Dispose();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Ignore disposal errors
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
_client?.Dispose();
|
||||
}
|
||||
catch (Exception)
|
||||
{
|
||||
// Ignore disposal errors
|
||||
}
|
||||
|
||||
_writeSemaphore?.Dispose();
|
||||
_querySemaphore?.Dispose();
|
||||
|
||||
_disposed = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user