fix
This commit is contained in:
@@ -5,4 +5,5 @@ public interface IBotRepository
|
|||||||
Task InsertBotAsync(BotBackup bot);
|
Task InsertBotAsync(BotBackup bot);
|
||||||
IEnumerable<BotBackup> GetBots();
|
IEnumerable<BotBackup> GetBots();
|
||||||
Task UpdateBackupBot(BotBackup bot);
|
Task UpdateBackupBot(BotBackup bot);
|
||||||
}
|
void DeleteBotBackup(string botName);
|
||||||
|
}
|
||||||
@@ -28,4 +28,7 @@ public interface IBotService
|
|||||||
string scenario, Enums.Timeframe interval, bool isForWatchingOnly);
|
string scenario, Enums.Timeframe interval, bool isForWatchingOnly);
|
||||||
|
|
||||||
IBot CreateSimpleBot(string botName, Workflow workflow);
|
IBot CreateSimpleBot(string botName, Workflow workflow);
|
||||||
|
Task<string> StopBot(string requestName);
|
||||||
|
Task<bool> DeleteBot(string requestName);
|
||||||
|
Task<string> RestartBot(string requestName);
|
||||||
}
|
}
|
||||||
@@ -158,6 +158,46 @@ namespace Managing.Application.ManageBot
|
|||||||
return new SimpleBot(botName, _tradingBotLogger, workflow, this);
|
return new SimpleBot(botName, _tradingBotLogger, workflow, this);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public async Task<string> StopBot(string botName)
|
||||||
|
{
|
||||||
|
if (_botTasks.TryGetValue(botName, out var botWrapper))
|
||||||
|
{
|
||||||
|
if (botWrapper.BotInstance is IBot bot)
|
||||||
|
{
|
||||||
|
await Task.Run(() =>
|
||||||
|
bot.Stop()); // Assuming Stop is an asynchronous process wrapped in Task.Run for synchronous methods
|
||||||
|
return bot.GetStatus();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Enums.BotStatus.Down.ToString();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<bool> DeleteBot(string botName)
|
||||||
|
{
|
||||||
|
if (_botTasks.TryRemove(botName, out _))
|
||||||
|
{
|
||||||
|
_botRepository.DeleteBotBackup(botName);
|
||||||
|
return Task.FromResult(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.FromResult(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
public Task<string> RestartBot(string botName)
|
||||||
|
{
|
||||||
|
if (_botTasks.TryGetValue(botName, out var botWrapper))
|
||||||
|
{
|
||||||
|
if (botWrapper.BotInstance is IBot bot)
|
||||||
|
{
|
||||||
|
bot.Restart();
|
||||||
|
return Task.FromResult(bot.GetStatus());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return Task.FromResult(Enums.BotStatus.Down.ToString());
|
||||||
|
}
|
||||||
|
|
||||||
public ITradingBot CreateScalpingBot(string accountName, MoneyManagement moneyManagement, string name,
|
public ITradingBot CreateScalpingBot(string accountName, MoneyManagement moneyManagement, string name,
|
||||||
Enums.Ticker ticker, string scenario, Enums.Timeframe interval, bool isForWatchingOnly)
|
Enums.Ticker ticker, string scenario, Enums.Timeframe interval, bool isForWatchingOnly)
|
||||||
{
|
{
|
||||||
|
|||||||
@@ -8,25 +8,16 @@ namespace Managing.Application.ManageBot;
|
|||||||
public class DeleteBotCommandHandler : IRequestHandler<DeleteBotCommand, bool>
|
public class DeleteBotCommandHandler : IRequestHandler<DeleteBotCommand, bool>
|
||||||
{
|
{
|
||||||
private readonly ILogger<DeleteBotCommandHandler> _log;
|
private readonly ILogger<DeleteBotCommandHandler> _log;
|
||||||
private readonly ITaskCache _taskCache;
|
private readonly IBotService _botService;
|
||||||
|
|
||||||
public DeleteBotCommandHandler(ITaskCache taskCache, ILogger<DeleteBotCommandHandler> log)
|
public DeleteBotCommandHandler(ILogger<DeleteBotCommandHandler> log, IBotService botService)
|
||||||
{
|
{
|
||||||
_taskCache = taskCache;
|
|
||||||
_log = log;
|
_log = log;
|
||||||
|
_botService = botService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<bool> Handle(DeleteBotCommand request, CancellationToken cancellationToken)
|
public Task<bool> Handle(DeleteBotCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
try
|
return _botService.DeleteBot(request.Name);
|
||||||
{
|
|
||||||
_taskCache.Invalidate(request.Name);
|
|
||||||
return Task.FromResult(true);
|
|
||||||
}
|
|
||||||
catch (Exception e)
|
|
||||||
{
|
|
||||||
_log.LogError(e.Message);
|
|
||||||
return Task.FromResult(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,32 +8,16 @@ namespace Managing.Application.ManageBot
|
|||||||
{
|
{
|
||||||
public class RestartBotCommandHandler : IRequestHandler<RestartBotCommand, string>
|
public class RestartBotCommandHandler : IRequestHandler<RestartBotCommand, string>
|
||||||
{
|
{
|
||||||
private readonly ITaskCache _taskCache;
|
private readonly IBotService _botService;
|
||||||
|
|
||||||
public RestartBotCommandHandler(ITaskCache taskCache)
|
public RestartBotCommandHandler(IBotService botService)
|
||||||
{
|
{
|
||||||
_taskCache = taskCache;
|
_botService = botService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<string> Handle(RestartBotCommand request, CancellationToken cancellationToken)
|
public Task<string> Handle(RestartBotCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
switch (request.BotType)
|
return _botService.RestartBot(request.Name);
|
||||||
{
|
|
||||||
case BotType.SimpleBot:
|
|
||||||
var simpleBot = _taskCache.Get<IBot>(request.Name);
|
|
||||||
simpleBot.Restart();
|
|
||||||
return Task.FromResult(simpleBot.GetStatus());
|
|
||||||
case BotType.ScalpingBot:
|
|
||||||
var scalpingBot = _taskCache.Get<ITradingBot>(request.Name);
|
|
||||||
scalpingBot.Restart();
|
|
||||||
return Task.FromResult(scalpingBot.GetStatus());
|
|
||||||
case BotType.FlippingBot:
|
|
||||||
var flippingBot = _taskCache.Get<ITradingBot>(request.Name);
|
|
||||||
flippingBot.Restart();
|
|
||||||
return Task.FromResult(flippingBot.GetStatus());
|
|
||||||
default:
|
|
||||||
return Task.FromResult(BotStatus.Down.ToString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -8,32 +8,16 @@ namespace Managing.Application.ManageBot
|
|||||||
{
|
{
|
||||||
public class StopBotCommandHandler : IRequestHandler<StopBotCommand, string>
|
public class StopBotCommandHandler : IRequestHandler<StopBotCommand, string>
|
||||||
{
|
{
|
||||||
private readonly ITaskCache _taskCache;
|
private readonly IBotService _botService;
|
||||||
|
|
||||||
public StopBotCommandHandler(ITaskCache taskCache)
|
public StopBotCommandHandler(IBotService botService)
|
||||||
{
|
{
|
||||||
_taskCache = taskCache;
|
_botService = botService;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Task<string> Handle(StopBotCommand request, CancellationToken cancellationToken)
|
public Task<string> Handle(StopBotCommand request, CancellationToken cancellationToken)
|
||||||
{
|
{
|
||||||
switch (request.BotType)
|
return _botService.StopBot(request.Name);
|
||||||
{
|
|
||||||
case BotType.SimpleBot:
|
|
||||||
var simpleBot = _taskCache.Get<IBot>(request.Name);
|
|
||||||
simpleBot.Stop();
|
|
||||||
return Task.FromResult(simpleBot.GetStatus());
|
|
||||||
case BotType.ScalpingBot:
|
|
||||||
var scalpingBot = _taskCache.Get<ITradingBot>(request.Name);
|
|
||||||
scalpingBot.Stop();
|
|
||||||
return Task.FromResult(scalpingBot.GetStatus());
|
|
||||||
case BotType.FlippingBot:
|
|
||||||
var flippingBot = _taskCache.Get<ITradingBot>(request.Name);
|
|
||||||
flippingBot.Stop();
|
|
||||||
return Task.FromResult(flippingBot.GetStatus());
|
|
||||||
default:
|
|
||||||
return Task.FromResult(BotStatus.Down.ToString());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -32,9 +32,8 @@ public class BotRepository : IBotRepository
|
|||||||
_botRepository.Update(dto);
|
_botRepository.Update(dto);
|
||||||
}
|
}
|
||||||
|
|
||||||
public void DeleteBotByName(string name)
|
public void DeleteBotBackup(string botName)
|
||||||
{
|
{
|
||||||
var bot = _botRepository.FindOne(b => b.Name == name);
|
_botRepository.DeleteOne(b => b.Name == botName);
|
||||||
_botRepository.DeleteById(bot.Id.ToString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user