特定的单身人士服务

时间:2019-11-04 00:42:24

标签: c# asp.net-core

IProcessingService是作用域服务。我想为每个cron迭代保留_savedValues_logicOne_logicTwo的值。如果您查看BotHostedService,它会产生一些重复的作业,并且每x分钟执行一次processingService.Execute()(基于cron)。换句话说,_savedValues每次都会重置,这弄乱了我的逻辑。

public class BotHostedService : IHostedService
{
    private readonly ILogger _logger;
    private readonly IServiceProvider _serviceProvider;

    public BotHostedService(ILogger<BotHostedService> logger, IServiceProvider serviceProvider)
    {
        _logger = logger;
        _serviceProvider = serviceProvider;
    }

    public async Task StartAsync(CancellationToken cancellationToken)
    {
        _logger.LogInformation("Bot service is starting.");

        // Get services
        using var scope = _serviceProvider.CreateScope();
        var botService = scope.ServiceProvider
            .GetRequiredService<IBotService>();
        var processingService = scope.ServiceProvider
            .GetRequiredService<IProcessingService>();

        var bots = await botService.GetAllAsync();

        foreach (var bot in bots)
        {
            if (!bot.Status)
                continue;

            _logger.LogInformation($"{bot.Name} is starting. Symbol: {bot.CryptoPair.Description}, Interval: {bot.TimeInterval.Description}");
            RecurringJob.AddOrUpdate(bot.Name, () => processingService.Execute(bot.Name), bot.TimeInterval.CronExpression, TimeZoneInfo.Local);
        }
    }

    public async Task StopAsync(CancellationToken cancellationToken)
    {
        _logger.LogInformation("Bot service is stopping.");
    }
}
public class ProcessingService : IProcessingService
{
    private readonly ILogger _logger;
    private readonly IBotService _botService;
    private readonly INotificationService _notificationService;
    private readonly IBinanceService _binanceService;

    public ProcessingService (
        ILogger<BotHostedService> logger, 
        IBotService botService, 
        INotificationService notificationService,
        IBinanceService binanceService)
    {
        _logger = logger;
        _botService = botService;
        _notificationService = notificationService;
        _binanceService = binanceService;
    }

    public void Execute(string botName)
        => ExecuteAsync(botName).GetAwaiter().GetResult();

    public async Task ExecuteAsync(string botName)
    {
        await PerformAsync(bot);
    }

    private readonly List<string> _savedValues = new List<string>();
    private bool _logicOne = false;
    private bool _loginTwo = false;

    private void ResetCounter()
    {
        _savedValues.Clear();
        _logicOne = false;
        _loginTwo = false;
    }

    private async Task PerformAsync(Bot bot)
    {
        // Logic

        // At some point
        ResetCounter();
    }
}

我认为我可以改为使用IProcessingService DI IServiceProvider并使服务单例。

问题是:

当我像现在一样有一些重复性工作时,每个人都会有自己的_savedValues,并且_savedValues会在cron迭代之间保留其值吗?

简体:

我的逻辑依赖于_savedValues,并且当前每次重置。我只想使其不重置,并且_savedValues对于不同的重复作业应具有不同的值。

public class ProcessingService : IProcessingService
{
    private readonly ILogger _logger;
    private readonly IServiceProvider _serviceProvider;

    public ProcessingService(ILogger<BotHostedService> logger, IServiceProvider serviceProvider)
    {
        _logger = logger;
        _serviceProvider = serviceProvider;
    }

    public void Execute(string botName)
        => ExecuteAsync(botName).GetAwaiter().GetResult();

    public async Task ExecuteAsync(string botName)
    {
        using var scope = _serviceProvider.CreateScope();
        var botService = scope.ServiceProvider
            .GetRequiredService<IBotService>();

        var bot = await botService.GetByNameAsync(botName);

        _logger.LogInformation($"{bot.Name} is working. Current time: {DateTime.Now.ToLocalTime().ToString("dd.MM.yyyy hh:mm tt", new CultureInfo("en-US"))}");

        // Algorithm
        await PerformAsync(bot);
    }

    private readonly List<string> _savedValues = new List<string>();
    private bool _logicOne = false;
    private bool _loginTwo = false;

    private void ResetCounter()
    {
        _savedValues.Clear();
        _logicOne = false;
        _loginTwo = false;
    }
}

0 个答案:

没有答案