< Summary

Line coverage
0%
Covered lines: 0
Uncovered lines: 84
Coverable lines: 84
Total lines: 215
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 16
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

File(s)

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/obj/Debug/net9.0/Microsoft.Extensions.Logging.Generators/Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator/LoggerMessage.g.cs

File '/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/obj/Debug/net9.0/Microsoft.Extensions.Logging.Generators/Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator/LoggerMessage.g.cs' does not exist (any more).

/builds/yukaitung/lgdxrobot2-cloud/LGDXRobotCloud.API/Repositories/AutoTaskRepository.cs

#LineLine coverage
 1using System.Text.Json;
 2using LGDXRobotCloud.Data.Models.Redis;
 3using LGDXRobotCloud.Protos;
 4using LGDXRobotCloud.Utilities.Enums;
 5using LGDXRobotCloud.Utilities.Helpers;
 6using NRedisStack.RedisStackCommands;
 7using NRedisStack.Search;
 8using StackExchange.Redis;
 9using static StackExchange.Redis.RedisChannel;
 10
 11namespace LGDXRobotCloud.API.Repositories;
 12
 13public interface IAutoTaskRepository
 14{
 15  Task<Guid?> SchedulerHoldAnyRobotAsync(int realmId);
 16  Task<bool> SchedulerHoldRobotAsync(int realmId, Guid robotId);
 17  Task SchedulerReleaseRobotAsync(int realmId, Guid robotId);
 18  Task AddAutoTaskAsync(int realmId, Guid robotId, RobotClientsAutoTask autoTask);
 19  Task AutoTaskHasUpdateAsync(int realmId, AutoTaskUpdate autoTaskUpdate);
 20}
 21
 022public partial class AutoTaskRepository(
 023    IConnectionMultiplexer redisConnection,
 024    ILogger<AutoTaskRepository> logger,
 025    IRobotDataRepository robotDataRepository
 026  ) : IAutoTaskRepository
 27{
 028  private readonly IConnectionMultiplexer _redisConnection = redisConnection ?? throw new ArgumentNullException(nameof(r
 029  private readonly IRobotDataRepository _robotDataRepository = robotDataRepository ?? throw new ArgumentNullException(na
 30
 31  [LoggerMessage(EventId = 0, Level = LogLevel.Error, Message = "Redis AutoTaskRepository Exception: {Msg}")]
 32  public partial void LogException(string msg);
 33
 34  public async Task<Guid?> SchedulerHoldAnyRobotAsync(int realmId)
 035  {
 036    var db = _redisConnection.GetDatabase();
 037    Guid? result = null;
 038    int robotStatus = (int)RobotStatus.Idle;
 39    try
 040    {
 041      var search = await db.FT().SearchAsync(RedisHelper.GetRobotDataIndex(realmId),
 042      new Query($"@{nameof(RobotData.RobotStatus)}:[{robotStatus} {robotStatus}] @{nameof(RobotData.PauseTaskAssignment)
 043          .Limit(0, 1)
 044          .ReturnFields(["__key"]));
 045      string? robotId = search.Documents.FirstOrDefault()?.Id.Replace(RedisHelper.GetRobotDataPrefix(realmId), string.Em
 046      if (robotId == null)
 047      {
 048        return null;
 49      }
 050      if (await db.HashSetAsync(RedisHelper.GetSchedulerHold(realmId, Guid.Parse(robotId)), "Value", "1", When.NotExists
 051      {
 052        result = Guid.Parse(robotId);
 053      }
 054    }
 055    catch (Exception ex)
 056    {
 057      LogException(ex.Message);
 058    }
 059    return result;
 060  }
 61
 62  public async Task<bool> SchedulerHoldRobotAsync(int realmId, Guid robotId)
 063  {
 064    var robotData = await _robotDataRepository.GetRobotDataAsync(realmId, robotId);
 065    if (robotData == null ||
 066        robotData.RobotStatus != RobotStatus.Idle ||
 067        robotData.PauseTaskAssignment == true)
 068    {
 069      return false;
 70    }
 71
 072    bool result = false;
 073    var db = _redisConnection.GetDatabase();
 74    try
 075    {
 076      result = await db.HashSetAsync(RedisHelper.GetSchedulerHold(realmId, robotId), "Value", "1", When.NotExists);
 077    }
 078    catch (Exception ex)
 079    {
 080      LogException(ex.Message);
 081    }
 082    return result;
 083  }
 84
 85  public async Task SchedulerReleaseRobotAsync(int realmId, Guid robotId)
 086  {
 087    var db = _redisConnection.GetDatabase();
 88    try
 089    {
 090      await db.KeyDeleteAsync(RedisHelper.GetSchedulerHold(realmId, robotId));
 091    }
 092    catch (Exception ex)
 093    {
 094      LogException(ex.Message);
 095    }
 096  }
 97
 98  public async Task AddAutoTaskAsync(int realmId, Guid robotId, RobotClientsAutoTask autoTask)
 099  {
 0100    var subscriber = _redisConnection.GetSubscriber();
 0101    var data = new RobotClientsResponse { Task = autoTask };
 0102    var base64 = SerialiserHelper.ToBase64(data);
 103    try
 0104    {
 0105      await subscriber.PublishAsync(new RedisChannel(RedisHelper.GetRobotExchangeQueue(robotId), PatternMode.Literal), b
 0106    }
 0107    catch (Exception ex)
 0108    {
 0109      LogException(ex.Message);
 0110    }
 0111  }
 112
 113  public async Task AutoTaskHasUpdateAsync(int realmId, AutoTaskUpdate autoTaskUpdate)
 0114  {
 0115    var subscriber = _redisConnection.GetSubscriber();
 0116    var json = JsonSerializer.Serialize(autoTaskUpdate);
 117    try
 0118    {
 0119      await subscriber.PublishAsync(new RedisChannel(RedisHelper.GetAutoTaskUpdateQueue(realmId), PatternMode.Literal), 
 0120    }
 0121    catch (Exception ex)
 0122    {
 0123      LogException(ex.Message);
 0124    }
 0125  }
 126}