Replace cache by concurrentDictionary task
This commit is contained in:
@@ -1,3 +1,4 @@
|
|||||||
|
using System.Collections.Concurrent;
|
||||||
using Managing.Application.Abstractions;
|
using Managing.Application.Abstractions;
|
||||||
using Managing.Application.Abstractions.Services;
|
using Managing.Application.Abstractions.Services;
|
||||||
using Managing.Application.Bots;
|
using Managing.Application.Bots;
|
||||||
@@ -14,20 +15,20 @@ namespace Managing.Application.ManageBot
|
|||||||
public class BotService : IBotService
|
public class BotService : IBotService
|
||||||
{
|
{
|
||||||
private readonly IBotRepository _botRepository;
|
private readonly IBotRepository _botRepository;
|
||||||
private readonly ITaskCache _taskCache;
|
|
||||||
private readonly IExchangeService _exchangeService;
|
private readonly IExchangeService _exchangeService;
|
||||||
private readonly IMessengerService _messengerService;
|
private readonly IMessengerService _messengerService;
|
||||||
private readonly IAccountService _accountService;
|
private readonly IAccountService _accountService;
|
||||||
private readonly ILogger<TradingBot> _tradingBotLogger;
|
private readonly ILogger<TradingBot> _tradingBotLogger;
|
||||||
private readonly ITradingService _tradingService;
|
private readonly ITradingService _tradingService;
|
||||||
|
|
||||||
|
private ConcurrentDictionary<string, BotTaskWrapper> _botTasks =
|
||||||
|
new ConcurrentDictionary<string, BotTaskWrapper>();
|
||||||
|
|
||||||
public BotService(IBotRepository botRepository, ITaskCache taskCache, IExchangeService exchangeService,
|
public BotService(IBotRepository botRepository, IExchangeService exchangeService,
|
||||||
IMessengerService messengerService, IAccountService accountService, ILogger<TradingBot> tradingBotLogger,
|
IMessengerService messengerService, IAccountService accountService, ILogger<TradingBot> tradingBotLogger,
|
||||||
ITradingService tradingService)
|
ITradingService tradingService)
|
||||||
{
|
{
|
||||||
_botRepository = botRepository;
|
_botRepository = botRepository;
|
||||||
_taskCache = taskCache;
|
|
||||||
_exchangeService = exchangeService;
|
_exchangeService = exchangeService;
|
||||||
_messengerService = messengerService;
|
_messengerService = messengerService;
|
||||||
_accountService = accountService;
|
_accountService = accountService;
|
||||||
@@ -66,20 +67,40 @@ namespace Managing.Application.ManageBot
|
|||||||
_botRepository.InsertBotAsync(botBackup);
|
_botRepository.InsertBotAsync(botBackup);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public class BotTaskWrapper
|
||||||
|
{
|
||||||
|
public Task Task { get; private set; }
|
||||||
|
public Type BotType { get; private set; }
|
||||||
|
public object BotInstance { get; private set; } // Add this line
|
||||||
|
|
||||||
|
public BotTaskWrapper(Task task, Type botType, object botInstance) // Update constructor
|
||||||
|
{
|
||||||
|
Task = task;
|
||||||
|
BotType = botType;
|
||||||
|
BotInstance = botInstance; // Set the bot instance
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public void AddSimpleBotToCache(IBot bot)
|
public void AddSimpleBotToCache(IBot bot)
|
||||||
{
|
{
|
||||||
_taskCache.AddOrGetExisting(bot.GetName(), () => Task.FromResult(bot));
|
var botTask =
|
||||||
|
new BotTaskWrapper(Task.Run(() => bot.Start()), bot.GetType(), bot); // Pass bot as the instance
|
||||||
|
_botTasks.AddOrUpdate(bot.GetName(), botTask, (key, existingVal) => botTask);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void AddTradingBotToCache(ITradingBot bot)
|
public void AddTradingBotToCache(ITradingBot bot)
|
||||||
{
|
{
|
||||||
_taskCache.AddOrGetExisting(bot.GetName(), () => Task.FromResult(bot));
|
var botTask = new BotTaskWrapper(Task.Run(() => bot.Start()), bot.GetType(), bot);
|
||||||
|
_botTasks.AddOrUpdate(bot.GetName(), botTask, (key, existingVal) => botTask);
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<ITradingBot> GetActiveBots()
|
public List<ITradingBot> GetActiveBots()
|
||||||
{
|
{
|
||||||
var cachedTask = _taskCache.GetCache<AsyncLazy<ITradingBot>>();
|
return _botTasks.Values
|
||||||
return cachedTask.Select(item => item.Value.Result).ToList();
|
.Where(wrapper => typeof(ITradingBot).IsAssignableFrom(wrapper.BotType))
|
||||||
|
.Select(wrapper => wrapper.BotInstance as ITradingBot)
|
||||||
|
.Where(bot => bot != null)
|
||||||
|
.ToList();
|
||||||
}
|
}
|
||||||
|
|
||||||
public IEnumerable<BotBackup> GetSavedBots()
|
public IEnumerable<BotBackup> GetSavedBots()
|
||||||
@@ -89,46 +110,47 @@ namespace Managing.Application.ManageBot
|
|||||||
|
|
||||||
public void StartBotFromBackup(BotBackup backupBot)
|
public void StartBotFromBackup(BotBackup backupBot)
|
||||||
{
|
{
|
||||||
|
object bot = null;
|
||||||
|
Task botTask = null;
|
||||||
|
|
||||||
switch (backupBot.BotType)
|
switch (backupBot.BotType)
|
||||||
{
|
{
|
||||||
case Enums.BotType.SimpleBot:
|
case Enums.BotType.SimpleBot:
|
||||||
Func<Task<IBot>> simpleBot = () =>
|
bot = CreateSimpleBot(backupBot.Name,
|
||||||
Task.FromResult(CreateSimpleBot(backupBot.Name, null));
|
null); // Assuming null is an acceptable parameter for workflow
|
||||||
var bot1 = _taskCache.AddOrGetExisting(backupBot.Name, simpleBot).Result;
|
botTask = Task.Run(() => ((IBot)bot).Start());
|
||||||
bot1.LoadBackup(backupBot);
|
|
||||||
bot1.Start();
|
|
||||||
break;
|
break;
|
||||||
case Enums.BotType.ScalpingBot:
|
case Enums.BotType.ScalpingBot:
|
||||||
var data = JsonConvert.DeserializeObject<TradingBotBackup>(backupBot.Data);
|
var scalpingBotData = JsonConvert.DeserializeObject<TradingBotBackup>(backupBot.Data);
|
||||||
Func<Task<ITradingBot>> scalpingBot = () => Task.FromResult(CreateScalpingBot(
|
bot = CreateScalpingBot(
|
||||||
data.AccountName,
|
scalpingBotData.AccountName,
|
||||||
data.MoneyManagement,
|
scalpingBotData.MoneyManagement,
|
||||||
data.Name,
|
backupBot.Name,
|
||||||
data.Ticker,
|
scalpingBotData.Ticker,
|
||||||
data.Scenario,
|
scalpingBotData.Scenario,
|
||||||
data.Timeframe,
|
scalpingBotData.Timeframe,
|
||||||
data.IsForWatchingOnly));
|
scalpingBotData.IsForWatchingOnly);
|
||||||
var bot2 = _taskCache.AddOrGetExisting(backupBot.Name, scalpingBot).Result;
|
botTask = Task.Run(() => ((ITradingBot)bot).Start());
|
||||||
bot2.LoadBackup(backupBot);
|
|
||||||
bot2.Start();
|
|
||||||
break;
|
break;
|
||||||
case Enums.BotType.FlippingBot:
|
case Enums.BotType.FlippingBot:
|
||||||
var dataFlippingBot = JsonConvert.DeserializeObject<TradingBotBackup>(backupBot.Data);
|
var flippingBotData = JsonConvert.DeserializeObject<TradingBotBackup>(backupBot.Data);
|
||||||
Func<Task<ITradingBot>> flippingBot = () => Task.FromResult(CreateFlippingBot(
|
bot = CreateFlippingBot(
|
||||||
dataFlippingBot.AccountName,
|
flippingBotData.AccountName,
|
||||||
dataFlippingBot.MoneyManagement,
|
flippingBotData.MoneyManagement,
|
||||||
dataFlippingBot.Name,
|
backupBot.Name,
|
||||||
dataFlippingBot.Ticker,
|
flippingBotData.Ticker,
|
||||||
dataFlippingBot.Scenario,
|
flippingBotData.Scenario,
|
||||||
dataFlippingBot.Timeframe,
|
flippingBotData.Timeframe,
|
||||||
dataFlippingBot.IsForWatchingOnly));
|
flippingBotData.IsForWatchingOnly);
|
||||||
var bot3 = _taskCache.AddOrGetExisting(backupBot.Name, flippingBot).Result;
|
botTask = Task.Run(() => ((ITradingBot)bot).Start());
|
||||||
bot3.LoadBackup(backupBot);
|
|
||||||
bot3.Start();
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
;
|
if (bot != null && botTask != null)
|
||||||
|
{
|
||||||
|
var botWrapper = new BotTaskWrapper(botTask, bot.GetType(), bot);
|
||||||
|
_botTasks.AddOrUpdate(backupBot.Name, botWrapper, (key, existingVal) => botWrapper);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public IBot CreateSimpleBot(string botName, Workflow workflow)
|
public IBot CreateSimpleBot(string botName, Workflow workflow)
|
||||||
|
|||||||
Reference in New Issue
Block a user