MapResourceSyncProvider.cs
1.97 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
using MassTransit.Mediator;
using Microsoft.Extensions.Logging;
using Rcs.Application.MessageBus.Commands;
using Rcs.Application.Services.Sync;
using Rcs.Domain.Repositories;
namespace Rcs.Infrastructure.Services.Sync;
/// <summary>
/// 地图资源同步提供者
/// @author zzy
/// </summary>
public class MapResourceSyncProvider : ISyncProvider
{
private readonly ILogger<MapResourceSyncProvider> _logger;
private readonly IMapRepository _mapRepository;
private readonly IMediator _mediator;
public string TaskType => SyncTaskTypes.MapResource;
public MapResourceSyncProvider(
ILogger<MapResourceSyncProvider> logger,
IMapRepository mapRepository,
IMediator mediator)
{
_logger = logger;
_mapRepository = mapRepository;
_mediator = mediator;
}
public async Task<IEnumerable<ISyncTask>> GetSyncTasksAsync(CancellationToken cancellationToken = default)
{
var maps = await _mapRepository.GetAllAsync(cancellationToken);
return maps
.Where(m => m.ResourceAutoSync && !string.IsNullOrWhiteSpace(m.ResourceUrl))
.Select(MapSyncTask.FromMapResource);
}
public async Task ExecuteSyncAsync(ISyncTask task, CancellationToken cancellationToken = default)
{
if (task is not MapSyncTask mapTask)
return;
_logger.LogInformation("[地图资源同步] 开始同步: MapId={MapId}", mapTask.MapId);
var client = _mediator.CreateRequestClient<SyncMapResourceCommand>();
var response = await client.GetResponse<Application.Common.ApiResponse>(
new SyncMapResourceCommand { MapId = mapTask.MapId },
cancellationToken);
if (response.Message.Success)
_logger.LogInformation("[地图资源同步] 同步成功: MapId={MapId}", mapTask.MapId);
else
_logger.LogWarning("[地图资源同步] 同步失败: MapId={MapId}, Message={Message}", mapTask.MapId, response.Message.Message);
}
}