修复运行时删除设备导致线程检测异常

This commit is contained in:
2248356998 qq.com
2023-03-06 17:28:02 +08:00
parent 44c09c85cb
commit 7fd1aceb96
2 changed files with 17 additions and 8 deletions

View File

@@ -41,7 +41,7 @@ public class CollectDeviceHostService : BackgroundService
/// <summary>
/// 更新设备线程
/// </summary>
public async Task UpDeviceThread(long devId)
public async Task UpDeviceThread(long devId, bool isUpdateDb = true)
{
if (!_stoppingToken.IsCancellationRequested)
{
@@ -51,8 +51,13 @@ public class CollectDeviceHostService : BackgroundService
if (devcore == null) { throw Oops.Bah($"更新设备线程失败,不存在{devId}为id的设备"); }
//这里先停止采集,操作会使线程取消,需要重新恢复线程
devcore.StopThread();
var dev = (await _collectDeviceService.GetCollectDeviceRuntime(devId)).FirstOrDefault();
if (dev == null) { throw Oops.Bah($"更新设备线程失败,不存在{devId}为id的设备"); }
CollectDeviceRunTime dev = null;
if (isUpdateDb)
dev = (await _collectDeviceService.GetCollectDeviceRuntime(devId)).FirstOrDefault();
else
dev = devcore.Device;
if (dev == null) { _logger.LogError($"更新设备线程失败,不存在{devId}为id的设备"); }
devcore.Init(dev);
devcore.StartThread();
@@ -301,7 +306,7 @@ public class CollectDeviceHostService : BackgroundService
if (devcore.Device.DeviceStatus == DeviceStatusEnum.Pause)
continue;
_logger?.LogWarning(devcore.Device.Name + "采集线程假死,重启线程中");
await UpDeviceThread(devcore.DeviceId);
await UpDeviceThread(devcore.DeviceId,false);
i--;
num--;

View File

@@ -106,7 +106,7 @@ public class UploadDeviceHostService : BackgroundService
/// <summary>
/// 更新设备线程
/// </summary>
public void UpDeviceThread(long devId)
public void UpDeviceThread(long devId,bool isUpdateDb=true)
{
if (!_stoppingToken.IsCancellationRequested)
{
@@ -115,8 +115,12 @@ public class UploadDeviceHostService : BackgroundService
if (devcore == null) { throw Oops.Bah($"更新设备线程失败,不存在{devId}为id的设备"); }
//这里先停止上传,操作会使线程取消,需要重新恢复线程
devcore.StopThread();
var dev = (_uploadDeviceService.GetUploadDeviceRuntime(devId)).FirstOrDefault();
if (dev == null) { throw Oops.Bah($"更新设备线程失败,不存在{devId}为id的设备"); }
UploadDeviceRunTime dev = null;
if (isUpdateDb)
dev = (_uploadDeviceService.GetUploadDeviceRuntime(devId)).FirstOrDefault();
else
dev = devcore.Device;
if (dev == null) { _logger.LogError($"更新设备线程失败,不存在{devId}为id的设备"); }
devcore.Init(dev);
devcore.StartThread();
@@ -245,7 +249,7 @@ public class UploadDeviceHostService : BackgroundService
if (devcore.Device.DeviceStatus == DeviceStatusEnum.Pause)
continue;
_logger?.LogWarning(devcore.Device.Name + "上传线程假死,重启线程中");
UpDeviceThread(devcore.DeviceId);
UpDeviceThread(devcore.DeviceId,false);
i--;
num--;