应用上线。RPC用ClientId,Web用AppName@IP
This commit is contained in:
parent
8d53f8f383
commit
3f579c0150
|
@ -28,6 +28,7 @@ class AntService : IApi, IActionFilter
|
|||
public IApiSession Session { get; set; }
|
||||
|
||||
private App _App;
|
||||
private AppOnline _Online;
|
||||
private INetSession _Net;
|
||||
private readonly AppService _appService;
|
||||
private readonly JobService _jobService;
|
||||
|
@ -53,19 +54,21 @@ class AntService : IApi, IActionFilter
|
|||
var act = filterContext.ActionName;
|
||||
if (act == nameof(Login)) return;
|
||||
|
||||
if (Session["App"] is App app)
|
||||
{
|
||||
_App = app;
|
||||
|
||||
var remote = _Net.Remote;
|
||||
var online = _appService.GetOnline(app, remote + "", remote.Host);
|
||||
online.UpdateTime = TimerX.Now;
|
||||
online.SaveAsync();
|
||||
}
|
||||
else
|
||||
{
|
||||
if (Session["App"] is not App app)
|
||||
throw new ApiException(ApiCode.Unauthorized, $"{_Net.Remote}未登录!不能执行{act}");
|
||||
|
||||
_App = app;
|
||||
|
||||
if (Session["AppOnline"] is not AppOnline online)
|
||||
{
|
||||
var remote = _Net.Remote;
|
||||
online = _appService.GetOnline(app, remote + "", remote.Host);
|
||||
}
|
||||
|
||||
_Online = online;
|
||||
|
||||
online.UpdateTime = TimerX.Now;
|
||||
online.SaveAsync();
|
||||
}
|
||||
|
||||
void IActionFilter.OnActionExecuted(ControllerContext filterContext)
|
||||
|
@ -103,7 +106,7 @@ class AntService : IApi, IActionFilter
|
|||
if (model.Code.IsNullOrEmpty()) throw new ArgumentNullException(nameof(model.Code));
|
||||
|
||||
var remote = _Net.Remote;
|
||||
var (app, online, rs) = _appService.Login(model, remote + "", remote.Host);
|
||||
var (app, online, rs) = _appService.Login(model, null, remote.Host);
|
||||
|
||||
// 记录当前用户
|
||||
Session["App"] = app;
|
||||
|
@ -113,28 +116,10 @@ class AntService : IApi, IActionFilter
|
|||
}
|
||||
|
||||
[Api(nameof(Logout))]
|
||||
public ILogoutResponse Logout(String reason)
|
||||
{
|
||||
var app = Session["App"] as App;
|
||||
var online = Session["AppOnline"] as AppOnline;
|
||||
|
||||
var remote = _Net.Remote;
|
||||
online ??= _appService.GetOnline(app, remote + "", remote.Host);
|
||||
|
||||
return _appService.Logout(app, online, reason, _Net.Remote.Host);
|
||||
}
|
||||
public ILogoutResponse Logout(String reason) => _appService.Logout(_App, _Online, reason, _Net.Remote.Host);
|
||||
|
||||
[Api(nameof(Ping))]
|
||||
public IPingResponse Ping(PingRequest request)
|
||||
{
|
||||
var app = Session["App"] as App;
|
||||
var online = Session["AppOnline"] as AppOnline;
|
||||
|
||||
var remote = _Net.Remote;
|
||||
online ??= _appService.GetOnline(app, remote + "", remote.Host);
|
||||
|
||||
return _appService.Ping(app, online, request, _Net.Remote.Host);
|
||||
}
|
||||
public IPingResponse Ping(PingRequest request) => _appService.Ping(_App, _Online, request, _Net.Remote.Host);
|
||||
|
||||
/// <summary>获取当前应用的所有在线实例</summary>
|
||||
/// <returns></returns>
|
||||
|
@ -176,8 +161,9 @@ class AntService : IApi, IActionFilter
|
|||
var job = model.Job?.Trim();
|
||||
if (job.IsNullOrEmpty()) return [];
|
||||
|
||||
var ip = _Net.Remote.Host;
|
||||
var tasks = _jobService.Acquire(_App, model, ip);
|
||||
var tasks = _jobService.Acquire(_App, model, _Online);
|
||||
|
||||
// 记录申请到的任务数
|
||||
if (span != null) span.Value = tasks?.Length ?? 0;
|
||||
|
||||
return tasks;
|
||||
|
@ -203,8 +189,7 @@ class AntService : IApi, IActionFilter
|
|||
{
|
||||
if (task == null || task.ID == 0) throw new InvalidOperationException("无效操作 TaskID=" + task?.ID);
|
||||
|
||||
var remote = _Net.Remote;
|
||||
return _jobService.Report(_App, task, remote + "", remote.Host);
|
||||
return _jobService.Report(_App, task, _Online);
|
||||
}
|
||||
#endregion
|
||||
}
|
|
@ -65,8 +65,9 @@ public class AppService
|
|||
|
||||
app.Save();
|
||||
|
||||
// 应用上线
|
||||
if (!model.ClientId.IsNullOrEmpty()) sessionId = model.ClientId;
|
||||
// 应用上线。RPC用ClientId,Web用AppName@IP
|
||||
//if (!model.ClientId.IsNullOrEmpty()) sessionId = model.ClientId;
|
||||
if (sessionId.IsNullOrEmpty()) sessionId = model.ClientId;
|
||||
var online = GetOnline(app, sessionId, ip);
|
||||
online.Name = model.Machine;
|
||||
online.Client = model.ClientId;
|
||||
|
@ -189,9 +190,9 @@ public class AppService
|
|||
return online;
|
||||
}
|
||||
|
||||
public void UpdateOnline(App app, JobTask ji, String sessionId, String ip)
|
||||
public void UpdateOnline(App app, JobTask ji, AppOnline online)
|
||||
{
|
||||
var online = GetOnline(app, sessionId, ip);
|
||||
//var online = GetOnline(app, sessionId, ip);
|
||||
online.Total += ji.Total;
|
||||
online.Success += ji.Success;
|
||||
online.Error += ji.Error;
|
||||
|
|
|
@ -134,7 +134,7 @@ public class JobService(AppService appService, ICacheProvider cacheProvider, ITr
|
|||
/// <summary>申请作业任务</summary>
|
||||
/// <param name="model">模型</param>
|
||||
/// <returns></returns>
|
||||
public ITask[] Acquire(App app, AcquireModel model, NetUri remote)
|
||||
public ITask[] Acquire(App app, AcquireModel model, AppOnline online)
|
||||
{
|
||||
var jobName = model.Job?.Trim();
|
||||
if (jobName.IsNullOrEmpty()) return [];
|
||||
|
@ -160,8 +160,9 @@ public class JobService(AppService appService, ICacheProvider cacheProvider, ITr
|
|||
if (job.DataTime.Year <= 2000) throw new XException("作业[{0}/{1}]未设置数据时间", job.ID, job.Name);
|
||||
|
||||
// 应用在线,但可能禁止向其分配任务
|
||||
var ip = remote?.Host;
|
||||
var online = _appService.GetOnline(app, remote + "", ip);
|
||||
var ip = online.UpdateIP;
|
||||
//var ip = remote?.Host;
|
||||
//var online = _appService.GetOnline(app, remote + "", ip);
|
||||
if (!online.Enable) return [];
|
||||
|
||||
var list = new List<JobTask>();
|
||||
|
@ -411,7 +412,7 @@ public class JobService(AppService appService, ICacheProvider cacheProvider, ITr
|
|||
/// <summary>报告状态(进度、成功、错误)</summary>
|
||||
/// <param name="result"></param>
|
||||
/// <returns></returns>
|
||||
public Boolean Report(App app, TaskResult result, String sessionId, String ip)
|
||||
public Boolean Report(App app, TaskResult result, AppOnline online)
|
||||
{
|
||||
if (result == null || result.ID == 0) throw new InvalidOperationException("无效操作 TaskID=" + result?.ID);
|
||||
|
||||
|
@ -444,7 +445,7 @@ public class JobService(AppService appService, ICacheProvider cacheProvider, ITr
|
|||
SetJobFinish(job, task);
|
||||
|
||||
// 记录状态
|
||||
_appService.UpdateOnline(app, task, sessionId, ip);
|
||||
_appService.UpdateOnline(app, task, online);
|
||||
}
|
||||
else if (result.Status == JobStatus.错误)
|
||||
{
|
||||
|
@ -452,13 +453,13 @@ public class JobService(AppService appService, ICacheProvider cacheProvider, ITr
|
|||
task.Error++;
|
||||
//ji.Message = err.Message;
|
||||
|
||||
SetJobError(job, task, ip);
|
||||
SetJobError(job, task, online.UpdateIP);
|
||||
|
||||
// 出错时判断如果超过最大错误数,则停止作业
|
||||
CheckMaxError(app, job);
|
||||
|
||||
// 记录状态
|
||||
_appService.UpdateOnline(app, task, sessionId, ip);
|
||||
_appService.UpdateOnline(app, task, online);
|
||||
}
|
||||
else if (result.Status == JobStatus.延迟)
|
||||
{
|
||||
|
|
|
@ -184,7 +184,7 @@ public class AntJobController : ControllerBase, IActionFilter
|
|||
[HttpPost(nameof(AddJobs))]
|
||||
public String[] AddJobs(JobModel[] jobs)
|
||||
{
|
||||
if (jobs == null || jobs.Length == 0) return new String[0];
|
||||
if (jobs == null || jobs.Length == 0) return [];
|
||||
|
||||
return _jobService.AddJobs(_App, jobs);
|
||||
}
|
||||
|
@ -196,9 +196,12 @@ public class AntJobController : ControllerBase, IActionFilter
|
|||
public ITask[] Acquire(AcquireModel model)
|
||||
{
|
||||
var job = model.Job?.Trim();
|
||||
if (job.IsNullOrEmpty()) return new TaskModel[0];
|
||||
if (job.IsNullOrEmpty()) return [];
|
||||
|
||||
return _jobService.Acquire(_App, model, UserHost);
|
||||
var sessionId = $"{_App.Name}@{UserHost}";
|
||||
var online = _appService.GetOnline(_App, sessionId, UserHost);
|
||||
|
||||
return _jobService.Acquire(_App, model, online);
|
||||
}
|
||||
|
||||
/// <summary>生产消息</summary>
|
||||
|
@ -222,7 +225,8 @@ public class AntJobController : ControllerBase, IActionFilter
|
|||
if (task == null || task.ID == 0) throw new InvalidOperationException("无效操作 TaskID=" + task?.ID);
|
||||
|
||||
var sessionId = $"{_App.Name}@{UserHost}";
|
||||
return _jobService.Report(_App, task, sessionId, UserHost);
|
||||
var online = _appService.GetOnline(_App, sessionId, UserHost);
|
||||
return _jobService.Report(_App, task, online);
|
||||
}
|
||||
#endregion
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue