add 后台服务 和 部署sql 等
This commit is contained in:
50
Ewide.Nbzs.BackWorkerService/DbManage.cs
Normal file
50
Ewide.Nbzs.BackWorkerService/DbManage.cs
Normal file
@@ -0,0 +1,50 @@
|
||||
using Furion;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
|
||||
namespace Ewide.Nbzs.BackWorkerService
|
||||
{
|
||||
public class MutiDBConnectionString
|
||||
{
|
||||
public string Id { get; set; }
|
||||
public string ConnectionString { get; set; }
|
||||
public DbType DbType { get; set; }
|
||||
public bool Enabled { get; set; }
|
||||
public string ProviderName { get; set; }
|
||||
}
|
||||
public class DbManage
|
||||
{
|
||||
private static List<MutiDBConnectionString> GetConnectionStrings()
|
||||
{
|
||||
var list = App.GetConfig<List<MutiDBConnectionString>>("DataBaseConnectionStrings");
|
||||
return list.Where(p => p.Enabled).ToList();
|
||||
}
|
||||
|
||||
private static SqlSugarClient GetDbClient()
|
||||
{
|
||||
List<ConnectionConfig> connectConfigList = new();
|
||||
GetConnectionStrings().ForEach(p =>
|
||||
{
|
||||
connectConfigList.Add(new ConnectionConfig
|
||||
{
|
||||
ConfigId = p.Id,
|
||||
ConnectionString = p.ConnectionString,
|
||||
DbType = p.DbType,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute
|
||||
});
|
||||
});
|
||||
var db = new SqlSugarClient(connectConfigList);
|
||||
return db;
|
||||
}
|
||||
public static SqlSugarProvider GetConnection(string id)
|
||||
{
|
||||
return GetDbClient().GetConnection(id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,11 +7,13 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting" Version="5.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Hosting.WindowsServices" Version="5.0.1" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\Ewide.Nbzs.Entity\Ewide.Nbzs.Entity.csproj" />
|
||||
<ProjectReference Include="..\framework\Api\Furion\framework\Furion.Extras.DatabaseAccessor.SqlSugar\Furion.Extras.DatabaseAccessor.SqlSugar.csproj" />
|
||||
<ProjectReference Include="..\framework\Api\Furion\framework\Furion.Extras.Logging.Serilog\Furion.Extras.Logging.Serilog.csproj" />
|
||||
<ProjectReference Include="..\framework\Api\Furion\framework\Furion\Furion.csproj" />
|
||||
</ItemGroup>
|
||||
</Project>
|
||||
|
||||
@@ -19,10 +19,11 @@ namespace Ewide.Nbzs.BackWorkerService
|
||||
|
||||
public static IHostBuilder CreateHostBuilder(string[] args) =>
|
||||
Host.CreateDefaultBuilder(args)
|
||||
.UseWindowsService()
|
||||
.Inject()
|
||||
.ConfigureServices((hostContext, services) =>
|
||||
{
|
||||
services.AddHostedService<Worker>();
|
||||
});
|
||||
}).UseSerilogDefault();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,12 @@
|
||||
<?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>
|
||||
<Configuration>Release</Configuration>
|
||||
<Platform>Any CPU</Platform>
|
||||
<PublishDir>D:\1_发布程序\宁波拆迁后台推送浙里办服务发布</PublishDir>
|
||||
<PublishProtocol>FileSystem</PublishProtocol>
|
||||
</PropertyGroup>
|
||||
</Project>
|
||||
@@ -1,10 +1,13 @@
|
||||
using Ewide.Nbzs.Entity;
|
||||
using Furion;
|
||||
using Furion.TaskScheduler;
|
||||
using Microsoft.Extensions.Hosting;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using SqlSugar;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
@@ -12,49 +15,191 @@ namespace Ewide.Nbzs.BackWorkerService
|
||||
{
|
||||
public class Worker : BackgroundService
|
||||
{
|
||||
public SqlSugarClient db;
|
||||
private readonly ILogger<Worker> _logger;
|
||||
|
||||
readonly SqlSugarProvider db_Product_Conn;
|
||||
readonly SqlSugarProvider db_Zlb_Conn;
|
||||
public Worker(ILogger<Worker> logger)
|
||||
{
|
||||
_logger = logger;
|
||||
List<ConnectionConfig> connectConfigList = new()
|
||||
{
|
||||
new ConnectionConfig
|
||||
{
|
||||
ConnectionString = App.Configuration["SqlServerConnectionString:DefaultConnection"],
|
||||
DbType = DbType.SqlServer,
|
||||
IsAutoCloseConnection = true,
|
||||
InitKeyType = InitKeyType.Attribute,
|
||||
ConfigId = "0"
|
||||
}
|
||||
};
|
||||
db = new SqlSugarClient(connectConfigList);
|
||||
db_Product_Conn = DbManage.GetConnection("Product_Conn");
|
||||
db_Zlb_Conn = DbManage.GetConnection("Zlb_Conn");
|
||||
}
|
||||
|
||||
protected override async Task ExecuteAsync(CancellationToken stoppingToken)
|
||||
{
|
||||
while (!stoppingToken.IsCancellationRequested)
|
||||
{
|
||||
ExecProcess();
|
||||
//var asd = db.Queryable<Ewide.NbzsZheliban.Entity.zjzwfwTickets>().ToList();
|
||||
var asd = db.Queryable<Ewide.NbzsZheliban.Entity.InvestigateTable>().ToList();
|
||||
_logger.LogInformation("Worker running at: {time}", DateTimeOffset.Now);
|
||||
await Task.Delay(1000, stoppingToken);
|
||||
ExecPushProcess();
|
||||
_logger.LogInformation("已完成一次任务,Finish a Worker running at: {time}", DateTimeOffset.Now);
|
||||
await Task.Delay(1 * 60 * 1000, stoppingToken);
|
||||
}
|
||||
}
|
||||
/// <summary>
|
||||
/// Ö´ÐÐÈÎÎñ ÿÃë
|
||||
/// </summary>
|
||||
private void ExecProcess()
|
||||
{
|
||||
Thread.Sleep(3000);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 执行推送任务
|
||||
/// </summary>
|
||||
private void ExecPushProcess()
|
||||
{
|
||||
//取出推送任务表数据
|
||||
var listPush = db_Product_Conn.Queryable<PushZlb>().Where(p => p.IsDeleted == 0).ToList();
|
||||
listPush.ForEach(p =>
|
||||
{
|
||||
if (p.IsAutoDo == 1)
|
||||
{
|
||||
//_logger.LogInformation("自动创建任务:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
////自动推
|
||||
//#region 自动
|
||||
//SpareTime.Do(() =>
|
||||
//{
|
||||
// if (p.IsError == 1)
|
||||
// {
|
||||
// _logger.LogInformation("自动任务停止:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
// return null;
|
||||
// }
|
||||
// else
|
||||
// return SpareTime.GetCronNextOccurrence(p.AutoDoTime);
|
||||
//}, (time, count) => { Push(p, count); }
|
||||
//);
|
||||
//#endregion
|
||||
|
||||
if (p.IsError == 1)
|
||||
{
|
||||
//_logger.LogInformation("自动任务停止:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
}
|
||||
else
|
||||
{
|
||||
Push(p);
|
||||
}
|
||||
}
|
||||
else if (p.IsAutoDo == 0 && p.IsProcessed == 0)
|
||||
{
|
||||
//_logger.LogInformation("手动创建任务:" + DateTimeOffset.Now.ToString("yyyy-MM-dd HH:mm:ss"));
|
||||
//手动
|
||||
//SpareTime.DoOnce(1, (timer, count) => { Push(p); });
|
||||
Push(p);
|
||||
}
|
||||
});
|
||||
|
||||
}
|
||||
private void Push2(PushZlb pushZlb)
|
||||
{
|
||||
_logger.LogWarning("测试Push2");
|
||||
}
|
||||
|
||||
private List<PushResult> Push(PushZlb pushZlbObj)
|
||||
{
|
||||
_logger.LogInformation("进入Push方法():Id:{Id} ,项目ID:{PrjId} at: {time}", pushZlbObj.ID, pushZlbObj.PrjId, DateTimeOffset.Now);
|
||||
List<PushResult> list_result = new();
|
||||
try
|
||||
{
|
||||
var projectId = Guid.Parse(pushZlbObj.PrjId);
|
||||
var project = db_Product_Conn.Queryable<Projects>().Where(p => p.ID == projectId).First();
|
||||
if (project != null)
|
||||
{
|
||||
//删政策
|
||||
var delete_result = db_Zlb_Conn.Deleteable<PoliciesRegulations>().ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "PoliciesRegulations", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
var insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<PoliciesRegulations>().ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "PoliciesRegulations", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删除非住宅协议
|
||||
delete_result = db_Zlb_Conn.Deleteable<NonResidentialAgreement>().Where(p => p.ProjectID == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "NonResidentialAgreement", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<NonResidentialAgreement>().Where(p => p.ProjectID == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "NonResidentialAgreement", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删除住宅协议
|
||||
delete_result = db_Zlb_Conn.Deleteable<ResidentialAgreement>().Where(p => p.ProjectID == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "ResidentialAgreement", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<ResidentialAgreement>().Where(p => p.ProjectID == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "ResidentialAgreement", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删除非住宅评估
|
||||
delete_result = db_Zlb_Conn.Deleteable<NonInvestigateTable_Assessment>().Where(p => p.ProjectId == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "NonInvestigateTable_Assessment", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<NonInvestigateTable_Assessment>().Where(p => p.ProjectId == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "NonInvestigateTable_Assessment", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删除住宅评估
|
||||
delete_result = db_Zlb_Conn.Deleteable<InvestigateTable_Assessment>().Where(p => p.ProjectId == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "InvestigateTable_Assessment", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<InvestigateTable_Assessment>().Where(p => p.ProjectId == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "InvestigateTable_Assessment", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删除非住宅调查表
|
||||
delete_result = db_Zlb_Conn.Deleteable<NonResidentialInvestigateTable>().Where(p => p.ProjectId == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "NonResidentialInvestigateTable", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<NonResidentialInvestigateTable>().Where(p => p.ProjectId == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "NonResidentialInvestigateTable", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删除住宅调查表
|
||||
delete_result = db_Zlb_Conn.Deleteable<InvestigateTable>().Where(p => p.ProjectId == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "InvestigateTable", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<InvestigateTable>().Where(p => p.ProjectId == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "InvestigateTable", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
//删项目
|
||||
delete_result = db_Zlb_Conn.Deleteable<Projects>().Where(p => p.ID == projectId).ExecuteCommandHasChange();
|
||||
list_result.Add(new PushResult { Action = "Delete", TableName = "Projects", IsSuccess = delete_result });
|
||||
//if (delete_result)
|
||||
//{
|
||||
insert_result = db_Zlb_Conn.Insertable(db_Product_Conn.Queryable<Projects>().Where(p => p.ID == projectId).ToList()).ExecuteCommand();
|
||||
list_result.Add(new PushResult { Action = "Insert", TableName = "Projects", IsSuccess = insert_result > 0 });
|
||||
//}
|
||||
}
|
||||
StringBuilder push_error_reason = new();
|
||||
//if (list_result.Any(r => !r.IsSuccess))
|
||||
//{
|
||||
// list_result.ForEach(r =>
|
||||
// {
|
||||
// push_error_reason.AppendFormat("{0} 表:[{1}] 结果:[{2}] [{3}]", r.Action, r.TableName, r.IsSuccess, r.FailReason);
|
||||
// });
|
||||
// pushZlbObj.IsProcessed = 1;
|
||||
// pushZlbObj.LastProcessTime = DateTime.Now;
|
||||
// pushZlbObj.ProcessCount = pushZlbObj.ProcessCount + 1;
|
||||
// pushZlbObj.IsError = 1;
|
||||
// pushZlbObj.ErrorReason = push_error_reason.ToString();
|
||||
// db_Product_Conn.Updateable(pushZlbObj).UpdateColumns(u => new { u.IsProcessed, u.LastProcessTime, u.ProcessCount, u.IsError, u.ErrorReason }).ExecuteCommand();
|
||||
//}
|
||||
//else
|
||||
//{
|
||||
pushZlbObj.IsProcessed = 1;
|
||||
pushZlbObj.LastProcessTime = DateTime.Now;
|
||||
if (pushZlbObj.ProcessCount == null)
|
||||
pushZlbObj.ProcessCount = 0;
|
||||
pushZlbObj.ProcessCount++;
|
||||
pushZlbObj.IsError = 0;
|
||||
pushZlbObj.ErrorReason = push_error_reason.ToString();
|
||||
db_Product_Conn.Updateable(pushZlbObj).UpdateColumns(u => new { u.IsProcessed, u.LastProcessTime, u.ProcessCount, u.IsError, u.ErrorReason }).WhereColumns(u => new { u.ID }).ExecuteCommand();
|
||||
//}
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogError(ex.Message + ex.StackTrace);
|
||||
}
|
||||
_logger.LogInformation("离开Push方法():Id:{Id} ,项目ID:{PrjId} at: {time}", pushZlbObj.ID, pushZlbObj.PrjId, DateTimeOffset.Now);
|
||||
return list_result;
|
||||
}
|
||||
}
|
||||
public class PushResult
|
||||
{
|
||||
public string Action { get; set; }
|
||||
public bool IsSuccess { get; set; }
|
||||
public string TableName { get; set; }
|
||||
public string FailReason { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,10 +6,33 @@
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
}
|
||||
},
|
||||
"Product_Conn": {
|
||||
"DefaultConnection": "data source=118.178.224.202;initial catalog=Nbcqb2;persist security info=True;user id=KYSQLSERVERADMIN;password=KYDBLogin20161103...;MultipleActiveResultSets=True;App=EntityFramework"
|
||||
},
|
||||
"Zlb_Conn": {
|
||||
"DefaultConnection": "data source=118.178.224.202;initial catalog=NbzsZlb;persist security info=True;user id=KYSQLSERVERADMIN;password=KYDBLogin20161103...;MultipleActiveResultSets=True;App=EntityFramework"
|
||||
}
|
||||
"DataBaseConnectionStrings": [
|
||||
/*
|
||||
MySql = 0,
|
||||
SqlServer = 1,
|
||||
Sqlite = 2,
|
||||
Oracle = 3,
|
||||
PostgreSQL = 4,
|
||||
Dm = 5,
|
||||
Kdbndp = 6
|
||||
*/
|
||||
{
|
||||
"Id": 1, // 连接id,可以配置到数据库
|
||||
"DBType": 2, // db类型,枚举,具体的看上边
|
||||
"Enabled": false, // 是否开启当前数据库db
|
||||
"Connection": "WMBlog.db" // 连接字符串
|
||||
},
|
||||
{
|
||||
"Id": "Product_Conn",
|
||||
"DBType": 1,
|
||||
"Enabled": true,
|
||||
"ConnectionString": "data source=118.178.224.202;initial catalog=Nbcqb2;persist security info=True;user id=KYSQLSERVERADMIN;password=KYDBLogin20161103...;MultipleActiveResultSets=True;App=EntityFramework"
|
||||
},
|
||||
{
|
||||
"Id": "Zlb_Conn",
|
||||
"DBType": 1,
|
||||
"Enabled": true,
|
||||
"ConnectionString": "data source=118.178.224.202;initial catalog=NbzsZlb;persist security info=True;user id=KYSQLSERVERADMIN;password=KYDBLogin20161103...;MultipleActiveResultSets=True;App=EntityFramework"
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user