Skip to content

Commit

Permalink
捕获更新失败时的异常
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Jul 9, 2024
1 parent 79c3d08 commit 9c265b0
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ public class BaseDeviceController : BaseController
private readonly IDeviceService _deviceService;
private readonly TokenService _tokenService;
private readonly ITracer? _tracer;
private readonly ILog? _log;

#region 构造
/// <summary>实例化设备控制器</summary>
Expand All @@ -35,6 +36,7 @@ public BaseDeviceController(IServiceProvider serviceProvider) : base(serviceProv
_deviceService = serviceProvider.GetRequiredService<IDeviceService>();
_tokenService = serviceProvider.GetRequiredService<TokenService>();
_tracer = serviceProvider.GetService<ITracer>();
_log = serviceProvider.GetService<ILog>();
}

/// <summary>验证身份</summary>
Expand Down Expand Up @@ -274,8 +276,8 @@ private async Task ConsumeMessage(WebSocket socket, String code, IProducerConsum
catch (OperationCanceledException) { }
catch (Exception ex)
{
XTrace.WriteLine("WebSocket异常 client={0} ip={1}", code, ip);
XTrace.WriteException(ex);
_log?.Error("WebSocket异常 client={0} ip={1}", code, ip);
_log?.Error(ex.ToString());
WriteLog("WebSocket断开", false, $"State={socket.State} CloseStatus={socket.CloseStatus} {ex}");
}
finally
Expand Down
24 changes: 16 additions & 8 deletions NewLife.Remoting/Clients/ClientBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,7 @@ protected virtual void FillLoginRequest(ILoginRequest request)
catch (Exception ex)
{
span?.SetError(ex, null);
XTrace.WriteException(ex);
Log?.Error(ex.ToString());

return null;
}
Expand Down Expand Up @@ -710,17 +710,19 @@ private async Task CheckUpgrade(Object? data)
WriteLog("检查更新");

// 清理旧版备份文件
var ug = new Upgrade { Log = XTrace.Log };
var ug = new Upgrade { Log = Log };
ug.DeleteBackup(".");

// 调用接口查询思否存在更新信息
var info = await UpgradeAsync(channel, cancellationToken);
if (info != null && info.Version != _lastVersion)
{
// _lastVersion避免频繁更新同一个版本
WriteLog("发现更新:{0}", info.ToJson(true));
this.WriteInfoEvent("Upgrade", $"准备从[{_lastVersion}]更新到[{info.Version}],开始下载 {info.Source}");
if (info == null || info.Version == _lastVersion) return info;

// _lastVersion避免频繁更新同一个版本
WriteLog("发现更新:{0}", info.ToJson(true));
this.WriteInfoEvent("Upgrade", $"准备从[{_lastVersion}]更新到[{info.Version}],开始下载 {info.Source}");

try
{
// 下载文件包
ug.Url = BuildUrl(info.Source!);
await ug.Download(cancellationToken);
Expand Down Expand Up @@ -760,6 +762,12 @@ private async Task CheckUpgrade(Object? data)
}
}
}
catch (Exception ex)
{
span?.SetError(ex, null);
//Log?.Error(ex.ToString());
throw;
}

return info;
}
Expand Down Expand Up @@ -892,7 +900,7 @@ protected virtual async Task OnPing(Object state)
// 有效期判断前把UTC转为本地时间
var now = GetNow();
var expire = model.Expire.ToLocalTime();
XTrace.WriteLine("[{0}] Got Command: {1}", source, model.ToJson());
WriteLog("[{0}] Got Command: {1}", source, model.ToJson());
if (model.Expire.Year < 2000 || model.Expire > now)
{
// 延迟执行
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Remoting/Clients/WsChannel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ private async Task DoPull(WebSocketClient socket, CancellationTokenSource source
catch (OperationCanceledException) { }
catch (Exception ex)
{
XTrace.WriteLine("WebSocket异常 {0}", ex.Message);
_client.Log?.Error("WebSocket异常 {0}", ex.Message);
}
finally
{
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Remoting/Clients/WsChannelCore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ private async Task DoPull(WebSocket socket, CancellationTokenSource source)
catch (OperationCanceledException) { }
catch (WebSocketException ex)
{
XTrace.WriteLine("WebSocket异常 {0}", ex.Message);
_client.Log?.Error("WebSocket异常 {0}", ex.Message);
}
finally
{
Expand Down

0 comments on commit 9c265b0

Please sign in to comment.