Skip to content

Commit

Permalink
整理项目,减少对Zero.Data的依赖,尽量让模版项目创建出来以后,直接就可以运行
Browse files Browse the repository at this point in the history
  • Loading branch information
nnhy committed Oct 24, 2024
1 parent b9cf15e commit efb63b7
Show file tree
Hide file tree
Showing 31 changed files with 445 additions and 499 deletions.
2 changes: 1 addition & 1 deletion NewLife.Templates/Web/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "零代数据管理平台,基于魔方,快速开发各种管理后台",
"identity": "NewLife.Web.2024",
"groupIdentity": "NewLife.Web",
"shortName": "cube",
"shortName": "nweb",
"tags": {
"language": "C#",
"type": "project"
Expand Down
2 changes: 1 addition & 1 deletion NewLife.Templates/WebApi/.template.config/template.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
"description": "数据接口,带有Swagger的标准WebApi接口项目",
"identity": "NewLife.WebApi.2024",
"groupIdentity": "NewLife.WebApi",
"shortName": "cubeapi",
"shortName": "napi",
"tags": {
"language": "C#",
"type": "project"
Expand Down
131 changes: 131 additions & 0 deletions Zero.Agent/MyServices.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
using System;
using NewLife.Agent;
using NewLife.Log;
using NewLife.Threading;
using Stardust;

namespace Zero.Agent;

/// <summary>代理服务例子。自定义服务程序可参照该类实现,核心是StartWork/StopWork。</summary>
public class MyServices : ServiceBase
{
#region 属性
/// <summary>性能跟踪器</summary>
public ITracer Tracer { get; set; }

private StarFactory _star;
#endregion

#region 构造函数
/// <summary>实例化一个代理服务</summary>
public MyServices()
{
// 一般在构造函数里面指定服务名
ServiceName = "XAgent";

DisplayName = "新生命服务代理";
Description = "用于承载各种服务的服务代理!";
}
#endregion

#region 核心
private TimerX _timer;
private TimerX _timer2;
private TimerX _timer3;
/// <summary>开始工作</summary>
/// <param name="reason"></param>
public override void StartWork(String reason)
{
WriteLog("业务开始……");

// 配置星尘。自动读取配置文件 config/star.config 中的服务器地址、应用标识、密钥
_star = new StarFactory();
Tracer = _star.Tracer;

// 从配置中心读取参数设置
var period1 = _star.Config["Period_Work1"].ToInt();
var period2 = _star.Config["Period_Work2"].ToInt();
if (period1 == 0) period1 = 60;
if (period2 == 0) period2 = 24 * 3600;
var cron3 = _star.Config["Cron_Work3"];
if (cron3 == null) cron3 = "*/30 * 9-17 * * 1-5";

// 5秒开始,每60秒执行一次
_timer = new TimerX(DoWork1, null, 5_000, period1 * 000) { Async = true };
// 每天凌晨2点13分执行一次
_timer2 = new TimerX(DoWork2, null, DateTime.Today.AddMinutes(2 * 60 + 13), period2 * 1000) { Async = true };
// 工作日朝九晚五每半分钟
_timer3 = new TimerX(DoWork3, null, cron3) { Async = true };

base.StartWork(reason);
}

private void DoWork1(Object state)
{
// 简易型埋点,测量调用次数和耗时,跟内部HttpClient和数据库操作形成上下级调用链,并送往星尘监控中心
using var span = Tracer?.NewSpan("work1");

var time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

// 日志会输出到当前目录的Log子目录中
XTrace.WriteLine($"Work1代码执行时间:{time}");
}

private void DoWork2(Object state)
{
// 完整型埋点,增加测量错误次数和明细
using var span = Tracer?.NewSpan("work2");

var time = DateTime.Now;
try
{
//// 业务处理,清理已经停用的项目成员关系
//var list = Member.FindAll(Member._.Enable == false);
//foreach (var item in list)
//{
// var tms = TeamMember.FindAllByMemberId(item.ID);
// foreach (var elm in tms)
// {
// elm.Enable = false;
// }
// tms.Save();
//}
}
catch (Exception ex)
{
// 记录错误以及上下文,一起送往星尘监控中心
span?.SetError(ex, time);

throw;
}
finally
{
span?.Dispose();
}
}

private void DoWork3(Object state)
{
// 简易型埋点,测量调用次数和耗时,跟内部HttpClient和数据库操作形成上下级调用链,并送往星尘监控中心
using var span = Tracer?.NewSpan("work3");

var time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

// 日志会输出到当前目录的Log子目录中
XTrace.WriteLine($"Work3代码执行时间:{time}");
}

/// <summary>停止服务</summary>
/// <param name="reason"></param>
public override void StopWork(String reason)
{
WriteLog("业务结束!{0}", reason);

_timer.Dispose();
_timer2.Dispose();
_timer3.Dispose();

base.StopWork(reason);
}
#endregion
}
132 changes: 1 addition & 131 deletions Zero.Agent/Program.cs
Original file line number Diff line number Diff line change
@@ -1,140 +1,10 @@
using System;
using NewLife.Agent;
using NewLife.Log;
using NewLife.Threading;
using Stardust;
using XCode;
using Zero.Data.Projects;

// 比较传统的Windows服务应用模版。优先推荐Console+StarAgent
// 比较传统的Windows/Linux服务应用模版。优先推荐Console+StarAgent

namespace Zero.Agent;

internal class Program
{
private static void Main(String[] args) => new MyServices().Main(args);
}

/// <summary>代理服务例子。自定义服务程序可参照该类实现。</summary>
public class MyServices : ServiceBase
{
#region 属性
/// <summary>性能跟踪器</summary>
public ITracer Tracer { get; set; }

private StarFactory _star;
#endregion

#region 构造函数
/// <summary>实例化一个代理服务</summary>
public MyServices()
{
// 一般在构造函数里面指定服务名
ServiceName = "XAgent";

DisplayName = "新生命服务代理";
Description = "用于承载各种服务的服务代理!";
}
#endregion

#region 核心
private TimerX _timer;
private TimerX _timer2;
private TimerX _timer3;
/// <summary>开始工作</summary>
/// <param name="reason"></param>
public override void StartWork(String reason)
{
WriteLog("业务开始……");

// 配置星尘。自动读取配置文件 config/star.config 中的服务器地址、应用标识、密钥
_star = new StarFactory();
Tracer = _star.Tracer;

// 从配置中心读取参数设置
var period1 = _star.Config["Period_Work1"].ToInt();
var period2 = _star.Config["Period_Work2"].ToInt();
if (period1 == 0) period1 = 60;
if (period2 == 0) period2 = 24 * 3600;
var cron3 = _star.Config["Cron_Work3"];
if (cron3 == null) cron3 = "*/30 * 9-17 * * 1-5";

// 5秒开始,每60秒执行一次
_timer = new TimerX(DoWork1, null, 5_000, period1 * 000) { Async = true };
// 每天凌晨2点13分执行一次
_timer2 = new TimerX(DoWork2, null, DateTime.Today.AddMinutes(2 * 60 + 13), period2 * 1000) { Async = true };
// 工作日朝九晚五每半分钟
_timer3 = new TimerX(DoWork3, null, cron3) { Async = true };

base.StartWork(reason);
}

private void DoWork1(Object state)
{
// 简易型埋点,测量调用次数和耗时,跟内部HttpClient和数据库操作形成上下级调用链,并送往星尘监控中心
using var span = Tracer?.NewSpan("work1");

var time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

// 日志会输出到当前目录的Log子目录中
XTrace.WriteLine($"Work1代码执行时间:{time}");
}

private void DoWork2(Object state)
{
// 完整型埋点,增加测量错误次数和明细
using var span = Tracer?.NewSpan("work2");

var time = DateTime.Now;
try
{
// 业务处理,清理已经停用的项目成员关系
var list = Member.FindAll(Member._.Enable == false);
foreach (var item in list)
{
var tms = TeamMember.FindAllByMemberId(item.ID);
foreach (var elm in tms)
{
elm.Enable = false;
}
tms.Save();
}
}
catch (Exception ex)
{
// 记录错误以及上下文,一起送往星尘监控中心
span?.SetError(ex, time);

throw;
}
finally
{
span?.Dispose();
}
}

private void DoWork3(Object state)
{
// 简易型埋点,测量调用次数和耗时,跟内部HttpClient和数据库操作形成上下级调用链,并送往星尘监控中心
using var span = Tracer?.NewSpan("work3");

var time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");

// 日志会输出到当前目录的Log子目录中
XTrace.WriteLine($"Work3代码执行时间:{time}");
}

/// <summary>停止服务</summary>
/// <param name="reason"></param>
public override void StopWork(String reason)
{
WriteLog("业务结束!{0}", reason);

_timer.Dispose();
_timer2.Dispose();
_timer3.Dispose();

base.StopWork(reason);
}
#endregion
}
5 changes: 0 additions & 5 deletions Zero.Agent/Zero.Agent.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,4 @@
<PackageReference Include="NewLife.XCode" Version="11.16.2024.1005" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Zero.Core\Zero.Core.csproj" />
<ProjectReference Include="..\Zero.Data\Zero.Data.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions Zero.Agent2/Worker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Zero.Agent2;

/// <summary>后台工作者。可根据业务需要定义多个后台工作者</summary>
public class Worker : BackgroundService
{
private readonly ILog _logger;
Expand Down
5 changes: 0 additions & 5 deletions Zero.Agent2/Zero.Agent2.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,4 @@
<PackageReference Include="NewLife.Stardust.Extensions" Version="3.1.2024.1004" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Zero.Core\Zero.Core.csproj" />
<ProjectReference Include="..\Zero.Data\Zero.Data.csproj" />
</ItemGroup>

</Project>
1 change: 1 addition & 0 deletions Zero.AntJob/AntJobWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

namespace Zero.AntJob;

/// <summary>蚂蚁调度后台工作者,负责装配并启动调度作业程序</summary>
public class AntJobWorker : BackgroundService
{
private Scheduler _scheduler;
Expand Down
5 changes: 3 additions & 2 deletions Zero.AntJob/Jobs/TestJob.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ namespace Zero.AntJob.Jobs;
/// <summary>
/// TestJob
/// </summary>
[DisplayName("Zero.AntJob2.TestJob")]
[Description("Zero.AntJob2.TestJob")]
[DisplayName("测试定时作业")]
[Description("Zero.AntJob.TestJob")]
public class TestJob : Handler
{
private readonly ITestService _testService;
Expand All @@ -18,6 +18,7 @@ public TestJob(ITestService testService)
var job = Job;
job.DataTime = DateTime.Now; // 开始时间
job.Step = 24 * 60 * 60; // 一天执行一次

_testService = testService;
}

Expand Down
2 changes: 0 additions & 2 deletions Zero.Desktop/FrmMain.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
using NewLife.Reflection;
using NewLife.Threading;
using XCode.DataAccessLayer;
using XCode.Membership;
using Zero.Data.Nodes;

namespace Zero.Desktop;

Expand Down
10 changes: 4 additions & 6 deletions Zero.Desktop/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
using NewLife.Model;
using Stardust;
using XCode;
using XCode.Membership;
using Zero.Data.Nodes;

namespace Zero.Desktop;

Expand Down Expand Up @@ -35,8 +33,8 @@ static void Main()
// 预热数据层,执行自动建表等操作
Task.Run(() =>
{
var dal = User.Meta.Session.Dal;
dal = Node.Meta.Session.Dal;
//var dal = User.Meta.Session.Dal;
//dal = Node.Meta.Session.Dal;
_ = EntityFactory.InitAllAsync();
});

Expand All @@ -59,7 +57,7 @@ private static void StartClient()

XTrace.WriteLine("初始化服务端地址:{0}", server);

_factory = new StarFactory(server, "ZeroDesktop", null)
_factory = new StarFactory(server, null, null)
{
Log = XTrace.Log,
};
Expand All @@ -68,7 +66,7 @@ private static void StartClient()
{
Code = set.Code,
Secret = set.Secret,
ProductCode = "ZeroDesktop",
ProductCode = _factory.AppId,
Setting = set,

Tracer = _factory.Tracer,
Expand Down
Loading

0 comments on commit efb63b7

Please sign in to comment.