style: 修改rbac service结构
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Yi.Framework.Rbac.Application.Contracts.Dtos.MonitorCache;
|
||||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||
|
||||
namespace Yi.Framework.Rbac.Application.Services.Monitor
|
||||
{
|
||||
public class MonitorCacheService : ApplicationService, IMonitorCacheService
|
||||
{
|
||||
private static List<MonitorCacheNameGetListOutputDto> monitorCacheNames => new List<MonitorCacheNameGetListOutputDto>()
|
||||
{
|
||||
new MonitorCacheNameGetListOutputDto{ CacheName="Yi:Login",Remark="登录验证码"},
|
||||
new MonitorCacheNameGetListOutputDto{ CacheName="Yi:User",Remark="用户信息"}
|
||||
};
|
||||
private Dictionary<string, string> monitorCacheNamesDic => monitorCacheNames.ToDictionary(x => x.CacheName, x => x.Remark);
|
||||
//private CSRedisClient _cacheClient;
|
||||
public MonitorCacheService()
|
||||
{
|
||||
//_cacheClient = redisCacheClient.Client;
|
||||
}
|
||||
//cacheKey value为空,只要name和备注
|
||||
|
||||
public List<MonitorCacheNameGetListOutputDto> GetName()
|
||||
{
|
||||
//固定的
|
||||
return monitorCacheNames;
|
||||
}
|
||||
[HttpGet("key/{cacaheName}")]
|
||||
public List<string> GetKey(string cacaheName)
|
||||
{
|
||||
//var output = _cacheClient.Keys($"{cacaheName}:*");
|
||||
return new List<string>() { "1233124", "3124", "1231251", "12312412" };
|
||||
}
|
||||
|
||||
//全部不为空
|
||||
[HttpGet("value/{cacaheName}/{cacaheKey}")]
|
||||
public MonitorCacheGetListOutputDto GetValue(string cacaheName, string cacaheKey)
|
||||
{
|
||||
//var value = _cacheClient.Get($"{cacaheName}:{cacaheKey}");
|
||||
return new MonitorCacheGetListOutputDto() { CacheKey = cacaheKey, CacheName = cacaheName, CacheValue = "ttt", Remark = monitorCacheNamesDic[cacaheName] };
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using System.Diagnostics;
|
||||
using System.Runtime.InteropServices;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.AspNetCore.Http;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Yi.Framework.Core.Helper;
|
||||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||
|
||||
namespace Yi.Framework.Rbac.Application.Services.Monitor
|
||||
{
|
||||
public class MonitorServerService : ApplicationService, IMonitorServerService
|
||||
{
|
||||
private IWebHostEnvironment _hostEnvironment;
|
||||
private IHttpContextAccessor _httpContextAccessor;
|
||||
public MonitorServerService(IWebHostEnvironment hostEnvironment, IHttpContextAccessor httpContextAccessor)
|
||||
{
|
||||
_hostEnvironment = hostEnvironment;
|
||||
_httpContextAccessor = httpContextAccessor;
|
||||
}
|
||||
[HttpGet("monitor-server/info")]
|
||||
public object GetInfo()
|
||||
{
|
||||
int cpuNum = Environment.ProcessorCount;
|
||||
string computerName = Environment.MachineName;
|
||||
string osName = RuntimeInformation.OSDescription;
|
||||
string osArch = RuntimeInformation.OSArchitecture.ToString();
|
||||
string version = RuntimeInformation.FrameworkDescription;
|
||||
string appRAM = ((double)Process.GetCurrentProcess().WorkingSet64 / 1048576).ToString("N2") + " MB";
|
||||
string startTime = Process.GetCurrentProcess().StartTime.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
string sysRunTime = ComputerHelper.GetRunTime();
|
||||
string serverIP = _httpContextAccessor.HttpContext.Connection.LocalIpAddress.MapToIPv4().ToString() + ":" + _httpContextAccessor.HttpContext.Connection.LocalPort;//获取服务器IP
|
||||
|
||||
var programStartTime = Process.GetCurrentProcess().StartTime;
|
||||
string programRunTime = DateTimeHelper.FormatTime(long.Parse((DateTime.Now - programStartTime).TotalMilliseconds.ToString().Split('.')[0]));
|
||||
var data = new
|
||||
{
|
||||
cpu = ComputerHelper.GetComputerInfo(),
|
||||
disk = ComputerHelper.GetDiskInfos(),
|
||||
sys = new { cpuNum, computerName, osName, osArch, serverIP, runTime = sysRunTime },
|
||||
app = new
|
||||
{
|
||||
name = _hostEnvironment.EnvironmentName,
|
||||
rootPath = _hostEnvironment.ContentRootPath,
|
||||
webRootPath = _hostEnvironment.WebRootPath,
|
||||
version,
|
||||
appRAM,
|
||||
startTime,
|
||||
runTime = programRunTime,
|
||||
host = serverIP
|
||||
},
|
||||
};
|
||||
|
||||
return data;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,62 @@
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.AspNetCore.SignalR;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||
using Yi.Framework.Rbac.Application.SignalRHubs;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Model;
|
||||
|
||||
namespace Yi.Framework.Rbac.Application.Services.Monitor
|
||||
{
|
||||
public class OnlineService : ApplicationService, IOnlineService
|
||||
{
|
||||
private ILogger<OnlineService> _logger;
|
||||
private IHubContext<OnlineUserHub> _hub;
|
||||
public OnlineService(ILogger<OnlineService> logger, IHubContext<OnlineUserHub> hub)
|
||||
{
|
||||
_logger = logger;
|
||||
_hub = hub;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 动态条件获取当前在线用户
|
||||
/// </summary>
|
||||
/// <param name="online"></param>
|
||||
/// <returns></returns>
|
||||
public Task<PagedResultDto<OnlineUserModel>> GetListAsync([FromQuery] OnlineUserModel online)
|
||||
{
|
||||
var data = OnlineUserHub.clientUsers;
|
||||
IEnumerable<OnlineUserModel> dataWhere = data.AsEnumerable();
|
||||
|
||||
if (!string.IsNullOrEmpty(online.Ipaddr))
|
||||
{
|
||||
dataWhere = dataWhere.Where((u) => u.Ipaddr!.Contains(online.Ipaddr));
|
||||
}
|
||||
if (!string.IsNullOrEmpty(online.UserName))
|
||||
{
|
||||
dataWhere = dataWhere.Where((u) => u.UserName!.Contains(online.UserName));
|
||||
}
|
||||
return Task.FromResult(new PagedResultDto<OnlineUserModel>() { TotalCount = data.Count, Items = dataWhere.ToList() });
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 强制退出用户
|
||||
/// </summary>
|
||||
/// <param name="connnectionId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpDelete]
|
||||
[Route("online/{connnectionId}")]
|
||||
public async Task<bool> ForceOut(string connnectionId)
|
||||
{
|
||||
if (OnlineUserHub.clientUsers.Exists(u => u.ConnnectionId == connnectionId))
|
||||
{
|
||||
//前端接受到这个事件后,触发前端自动退出
|
||||
await _hub.Clients.Client(connnectionId).SendAsync("forceOut", "你已被强制退出!");
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,220 @@
|
||||
using System.Reflection;
|
||||
using Mapster;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Quartz;
|
||||
using Quartz.Impl.Matchers;
|
||||
using Volo.Abp;
|
||||
using Volo.Abp.Application.Dtos;
|
||||
using Volo.Abp.Application.Services;
|
||||
using Volo.Abp.Timing;
|
||||
using Yi.Framework.Rbac.Application.Contracts.Dtos.Task;
|
||||
using Yi.Framework.Rbac.Application.Contracts.IServices;
|
||||
using Yi.Framework.Rbac.Domain.Shared.Enums;
|
||||
|
||||
namespace Yi.Framework.Rbac.Application.Services.Monitor
|
||||
{
|
||||
public class TaskService : ApplicationService, ITaskService
|
||||
{
|
||||
private readonly ISchedulerFactory _schedulerFactory;
|
||||
private readonly IClock _clock;
|
||||
public TaskService(ISchedulerFactory schedulerFactory, IClock clock)
|
||||
{
|
||||
_clock = clock;
|
||||
_schedulerFactory = schedulerFactory;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// 单查job
|
||||
/// </summary>
|
||||
/// <param name="jobId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpGet("task/{jobId}")]
|
||||
public async Task<TaskGetOutput> GetAsync([FromRoute] string jobId)
|
||||
{
|
||||
var scheduler = await _schedulerFactory.GetScheduler();
|
||||
|
||||
var jobDetail = await scheduler.GetJobDetail(new JobKey(jobId));
|
||||
var trigger = (await scheduler.GetTriggersOfJob(new JobKey(jobId))).First();
|
||||
//状态
|
||||
var state = await scheduler.GetTriggerState(trigger.Key);
|
||||
|
||||
|
||||
var output = new TaskGetOutput
|
||||
{
|
||||
JobId = jobDetail.Key.Name,
|
||||
GroupName = jobDetail.Key.Group,
|
||||
JobType = jobDetail.JobType.Name,
|
||||
Properties = Newtonsoft.Json.JsonConvert.SerializeObject(jobDetail.JobDataMap),
|
||||
Concurrent = !jobDetail.ConcurrentExecutionDisallowed,
|
||||
Description = jobDetail.Description,
|
||||
LastRunTime = _clock.Normalize(trigger.GetPreviousFireTimeUtc()?.DateTime ?? DateTime.MinValue),
|
||||
NextRunTime = _clock.Normalize(trigger.GetNextFireTimeUtc()?.DateTime ?? DateTime.MinValue),
|
||||
AssemblyName = jobDetail.JobType.Assembly.GetName().Name,
|
||||
Status = state.ToString()
|
||||
};
|
||||
|
||||
if (trigger is ISimpleTrigger simple)
|
||||
{
|
||||
output.TriggerArgs = Math.Round(simple.RepeatInterval.TotalMinutes, 2).ToString() + "分钟";
|
||||
output.Type = JobTypeEnum.Millisecond;
|
||||
output.Millisecond = simple.RepeatInterval.TotalMilliseconds;
|
||||
}
|
||||
else if (trigger is ICronTrigger cron)
|
||||
{
|
||||
output.TriggerArgs = cron.CronExpressionString!;
|
||||
output.Type = JobTypeEnum.Cron;
|
||||
output.Cron = cron.CronExpressionString;
|
||||
}
|
||||
return output;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 多查job
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public async Task<PagedResultDto<TaskGetListOutput>> GetListAsync([FromQuery] TaskGetListInput input)
|
||||
{
|
||||
var items = new List<TaskGetOutput>();
|
||||
|
||||
var scheduler = await _schedulerFactory.GetScheduler();
|
||||
|
||||
var groups = await scheduler.GetJobGroupNames();
|
||||
|
||||
foreach (var groupName in groups)
|
||||
{
|
||||
foreach (var jobKey in await scheduler.GetJobKeys(GroupMatcher<JobKey>.GroupEquals(groupName)))
|
||||
{
|
||||
string jobName = jobKey.Name;
|
||||
string jobGroup = jobKey.Group;
|
||||
var triggers = (await scheduler.GetTriggersOfJob(jobKey)).First();
|
||||
items.Add(await GetAsync(jobName));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
var output = items.Skip((input.SkipCount - 1) * input.MaxResultCount).Take(input.MaxResultCount)
|
||||
.OrderByDescending(x => x.LastRunTime)
|
||||
.ToList();
|
||||
return new PagedResultDto<TaskGetListOutput>(items.Count(), output.Adapt<List<TaskGetListOutput>>());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 创建job
|
||||
/// </summary>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public async Task CreateAsync(TaskCreateInput input)
|
||||
{
|
||||
var scheduler = await _schedulerFactory.GetScheduler();
|
||||
|
||||
//设置启动时执行一次,然后最大只执行一次
|
||||
|
||||
|
||||
//jobBuilder
|
||||
var jobClassType = Assembly.Load(input.AssemblyName).GetTypes().Where(x => x.Name == input.JobType).FirstOrDefault();
|
||||
|
||||
if (jobClassType is null)
|
||||
{
|
||||
throw new UserFriendlyException($"程序集:{input.AssemblyName},{input.JobType} 不存在");
|
||||
}
|
||||
|
||||
var jobBuilder = JobBuilder.Create(jobClassType).WithIdentity(new JobKey(input.JobId, input.GroupName))
|
||||
.WithDescription(input.Description);
|
||||
if (!input.Concurrent)
|
||||
{
|
||||
jobBuilder.DisallowConcurrentExecution();
|
||||
}
|
||||
|
||||
//triggerBuilder
|
||||
TriggerBuilder triggerBuilder = null;
|
||||
switch (input.Type)
|
||||
{
|
||||
case JobTypeEnum.Cron:
|
||||
triggerBuilder =
|
||||
TriggerBuilder.Create()
|
||||
.WithCronSchedule(input.Cron);
|
||||
|
||||
|
||||
|
||||
|
||||
break;
|
||||
case JobTypeEnum.Millisecond:
|
||||
triggerBuilder =
|
||||
TriggerBuilder.Create().StartNow()
|
||||
.WithSimpleSchedule(x => x
|
||||
.WithInterval(TimeSpan.FromMilliseconds(input.Millisecond ?? 10000))
|
||||
.RepeatForever()
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
//作业计划,单个jobBuilder与多个triggerBuilder组合
|
||||
await scheduler.ScheduleJob(jobBuilder.Build(), triggerBuilder.Build());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 移除job
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task DeleteAsync(IEnumerable<string> id)
|
||||
{
|
||||
var scheduler = await _schedulerFactory.GetScheduler();
|
||||
await scheduler.DeleteJobs(id.Select(x => new JobKey(x)).ToList());
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 暂停job
|
||||
/// </summary>
|
||||
/// <param name="jobId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut]
|
||||
public async Task PauseAsync(string jobId)
|
||||
{
|
||||
var scheduler = await _schedulerFactory.GetScheduler();
|
||||
await scheduler.PauseJob(new JobKey(jobId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 开始job
|
||||
/// </summary>
|
||||
/// <param name="jobId"></param>
|
||||
/// <returns></returns>
|
||||
[HttpPut]
|
||||
public async Task StartAsync(string jobId)
|
||||
{
|
||||
var scheduler = await _schedulerFactory.GetScheduler();
|
||||
await scheduler.ResumeJob(new JobKey(jobId));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// 更新job
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="input"></param>
|
||||
/// <returns></returns>
|
||||
public async Task UpdateAsync(string id, TaskUpdateInput input)
|
||||
{
|
||||
await DeleteAsync(new List<string>() { id });
|
||||
await CreateAsync(input.Adapt<TaskCreateInput>());
|
||||
}
|
||||
|
||||
[HttpPost("task/run-once/{id}")]
|
||||
public async Task RunOnceAsync([FromRoute] string id)
|
||||
{
|
||||
var scheduler = await _schedulerFactory.GetScheduler();
|
||||
var jobDetail = await scheduler.GetJobDetail(new JobKey(id));
|
||||
|
||||
var jobBuilder = JobBuilder.Create(jobDetail.JobType).WithIdentity(new JobKey(Guid.NewGuid().ToString()));
|
||||
//设置启动时执行一次,然后最大只执行一次
|
||||
var trigger = TriggerBuilder.Create().WithIdentity(Guid.NewGuid().ToString()).StartNow()
|
||||
.WithSimpleSchedule(x => x
|
||||
.WithIntervalInHours(1)
|
||||
.WithRepeatCount(1))
|
||||
.Build();
|
||||
|
||||
await scheduler.ScheduleJob(jobBuilder.Build(), trigger);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user