尝试 WorkerService 托管星尘代理
This commit is contained in:
parent
25483c073e
commit
020bb423f3
|
@ -0,0 +1,17 @@
|
|||
<?xml version="1.0" encoding="utf-8"?>
|
||||
<!--
|
||||
https://go.microsoft.com/fwlink/?LinkID=208121.
|
||||
-->
|
||||
<Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
|
||||
<PropertyGroup>
|
||||
<PublishProtocol>FileSystem</PublishProtocol>
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Any CPU</Platform>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<PublishDir>..\BinClient\publish\</PublishDir>
|
||||
<RuntimeIdentifier>linux-arm</RuntimeIdentifier>
|
||||
<SelfContained>false</SelfContained>
|
||||
<PublishSingleFile>False</PublishSingleFile>
|
||||
<PublishReadyToRun>False</PublishReadyToRun>
|
||||
</PropertyGroup>
|
||||
</Project>
|
|
@ -0,0 +1,38 @@
|
|||
using System;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using NewLife;
|
||||
using NewLife.Log;
|
||||
|
||||
namespace StarAgent3
|
||||
{
|
||||
public class Program
|
||||
{
|
||||
public static void Main(string[] args)
|
||||
{
|
||||
XTrace.UseConsole();
|
||||
|
||||
XTrace.WriteLine("FullPath:{0}", ".".GetFullPath());
|
||||
XTrace.WriteLine("BasePath:{0}", ".".GetBasePath());
|
||||
XTrace.WriteLine("TempPath:{0}", Path.GetTempPath());
|
||||
|
||||
var mi = MachineInfo.Current ?? MachineInfo.RegisterAsync().Result;
|
||||
|
||||
foreach (var pi in mi.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance))
|
||||
{
|
||||
XTrace.WriteLine("{0}:\t{1}", pi.Name, mi.GetValue(pi));
|
||||
}
|
||||
|
||||
CreateHostBuilder(args).Build().Run();
|
||||
}
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.ConfigureServices((hostContext, services) =>
|
||||
{
|
||||
services.AddHostedService<Worker>();
|
||||
});
|
||||
}
|
||||
}
|
|
@ -0,0 +1,10 @@
|
|||
{
|
||||
"profiles": {
|
||||
"StarAgent3": {
|
||||
"commandName": "Project",
|
||||
"environmentVariables": {
|
||||
"DOTNET_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,19 @@
|
|||
<Project Sdk="Microsoft.NET.Sdk.Worker">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>netcoreapp3.1</TargetFramework>
|
||||
<UserSecretsId>dotnet-StarAgent3-6FAF7BE2-45DA-4EA4-9F84-01C0E7AE0675</UserSecretsId>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<Compile Include="..\StarAgent\Setting.cs" Link="Setting.cs" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="3.1.2" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Stardust\Stardust.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
|
@ -0,0 +1,121 @@
|
|||
using System;
|
||||
using System.Diagnostics;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using NewLife;
|
||||
using NewLife.Log;
|
||||
using NewLife.Threading;
|
||||
using Stardust;
|
||||
|
||||
namespace StarAgent3
|
||||
{
|
||||
public class Worker : BackgroundService
|
||||
{
|
||||
private readonly ILogger<Worker> _logger;
|
||||
|
||||
public Worker(ILogger<Worker> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
}
|
||||
|
||||
public override Task StartAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
XTrace.WriteLine("StartAsync");
|
||||
|
||||
return base.StartAsync(cancellationToken);
|
||||
}
|
||||
|
||||
public override Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
XTrace.WriteLine("StopAsync");
|
||||
|
||||
return base.StopAsync(cancellationToken);
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
var set = StarAgent.Setting.Current;
|
||||
|
||||
StartClient();
|
||||
|
||||
// 应用服务管理
|
||||
_Manager = new ServiceManager
|
||||
{
|
||||
Services = set.Services,
|
||||
|
||||
Log = XTrace.Log,
|
||||
};
|
||||
_Manager.Start();
|
||||
}
|
||||
|
||||
private static TimerX _timer;
|
||||
private static StarClient _Client;
|
||||
private static ServiceManager _Manager;
|
||||
private static void StartClient()
|
||||
{
|
||||
var set = StarAgent.Setting.Current;
|
||||
var server = set.Server;
|
||||
if (server.IsNullOrEmpty()) return;
|
||||
|
||||
XTrace.WriteLine("初始化服务端地址:{0}", server);
|
||||
|
||||
var client = new StarClient(server)
|
||||
{
|
||||
Code = set.Code,
|
||||
Secret = set.Secret,
|
||||
Log = XTrace.Log,
|
||||
};
|
||||
|
||||
// 登录后保存证书
|
||||
client.OnLogined += (s, e) =>
|
||||
{
|
||||
var inf = client.Info;
|
||||
if (inf != null && !inf.Code.IsNullOrEmpty())
|
||||
{
|
||||
set.Code = inf.Code;
|
||||
set.Secret = inf.Secret;
|
||||
set.Save();
|
||||
}
|
||||
};
|
||||
|
||||
// 可能需要多次尝试
|
||||
_timer = new TimerX(TryConnectServer, client, 0, 5_000) { Async = true };
|
||||
|
||||
_Client = client;
|
||||
}
|
||||
|
||||
private static void TryConnectServer(Object state)
|
||||
{
|
||||
var client = state as StarClient;
|
||||
var set = StarAgent.Setting.Current;
|
||||
//Task.Run(client.Login).ContinueWith(t => CheckUpgrade(client, set.Channel));
|
||||
client.Login().Wait();
|
||||
CheckUpgrade(client, set.Channel);
|
||||
|
||||
// 登录成功,销毁定时器
|
||||
//TimerX.Current.Period = 0;
|
||||
_timer.TryDispose();
|
||||
_timer = null;
|
||||
}
|
||||
|
||||
private static void CheckUpgrade(StarClient client, String channel)
|
||||
{
|
||||
// 检查更新
|
||||
var ur = client.Upgrade(channel).Result;
|
||||
if (ur != null)
|
||||
{
|
||||
var rs = client.ProcessUpgrade(ur);
|
||||
|
||||
// 强制更新时,马上重启
|
||||
if (rs && ur.Force)
|
||||
{
|
||||
var p = Process.GetCurrentProcess();
|
||||
p.Close();
|
||||
p.Kill(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
}
|
||||
}
|
|
@ -0,0 +1,9 @@
|
|||
{
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft": "Warning",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
}
|
||||
}
|
8
星尘.sln
8
星尘.sln
|
@ -27,7 +27,9 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution
|
|||
EndProject
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "ClientTest", "ClientTest\ClientTest.csproj", "{7DE10A4D-1749-4474-A6B2-F52CA8462813}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StarAgent2", "StarAgent2\StarAgent2.csproj", "{8F868ED3-102A-446C-AC1B-D635FF7EA772}"
|
||||
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "StarAgent2", "StarAgent2\StarAgent2.csproj", "{8F868ED3-102A-446C-AC1B-D635FF7EA772}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "StarAgent3", "StarAgent3\StarAgent3.csproj", "{29BD9661-DE43-4AA5-A2D0-0106CBE5AF54}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
|
@ -67,6 +69,10 @@ Global
|
|||
{8F868ED3-102A-446C-AC1B-D635FF7EA772}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{8F868ED3-102A-446C-AC1B-D635FF7EA772}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{8F868ED3-102A-446C-AC1B-D635FF7EA772}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{29BD9661-DE43-4AA5-A2D0-0106CBE5AF54}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{29BD9661-DE43-4AA5-A2D0-0106CBE5AF54}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{29BD9661-DE43-4AA5-A2D0-0106CBE5AF54}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{29BD9661-DE43-4AA5-A2D0-0106CBE5AF54}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
|
|
Loading…
Reference in New Issue