123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- using System;
- using System.Collections.Generic;
- using System.ComponentModel.DataAnnotations;
- using System.Data.SqlClient;
- using System.IO;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using DW5S.DTO;
- using DW5S.Entity;
- using DW5S.Repostory;
- using System.Threading;
- using DW5S.WebApi;
- using Microsoft.AspNetCore.Mvc;
- using Serilog;
- using Serilog.Core;
- using DW5S.Service;
- using Newtonsoft.Json;
- using System.Security.Policy;
- namespace DW5S.Controllers
- {
- /// <summary>
- /// 任务相关接口
- /// </summary>
- public class TaskController : BaseController
- {
- ILogger logger { get; set; }
- IUnitOfWork unitOfWork { get; set; }
- [Autowired]
- HttpClient client { get; set; }
- [HttpGet]
- public AjaxResult<string> Test()
- {
- return Success("1");
- }
- /// <summary>
- /// 获取客户端IP地址
- /// </summary>
- /// <returns></returns>
- [HttpGet]
- public AjaxResult<string> GetClientIP()
- {
- try
- {
- while (true)
- {
- var factory = IocContainer.GetService<IHttpClientFactory>();
- var client1 = factory.CreateClient("");
- var response = client1.GetAsync("http://192.168.1.7:8090/api/task/test").Result;
- response.EnsureSuccessStatusCode();
- var result = response.Content.ReadAsStringAsync().Result;
- };
- var ip = RemoteIp;
- return Success(ip);
- }
- catch (Exception ex)
- {
- string error = "获取客户端IP出错";
- logger.Error(ex, error);
- return Error<string>(error);
- }
- }
- /// <summary>
- /// 停止任务
- /// </summary>
- /// <param name="dto">任务停止参数</param>
- /// <returns></returns>
- [HttpPost]
- public async Task<AjaxResult> StopAsync(TaskStopHandleDto dto)
- {
- try
- {
- var repsTask = unitOfWork.Of<TaskInfo>();
- var item = await repsTask.GetByIdAsync(dto.ID);
- if (item == null)
- {
- return Error($"任务停止失败,找不到ID={dto.ID}的任务");
- }
- if (item.TaskState == EnumTaskState.Stopped)
- Thread.Sleep(2000);
- item.TaskState = EnumTaskState.Stopped;
- await repsTask.AddOrUpdateAsync(item);
- await unitOfWork.SaveAsync();
- Messenger.Defalut.Pub("任务状态改变", dto.ID);
- if (dto.StopType == EnumTaskStopType.Properly)
- logger.Information($"任务停止完成,ID={dto.ID},停止原因={dto.StopReason}");
- else
- logger.Error($"任务停止完成,ID={dto.ID},停止原因={dto.StopReason}");
- return Success();
- }
- catch (Exception ex)
- {
- string msg = $"任务停止异常.ID={dto.ID}";
- logger.Error(ex, msg);
- return Error(msg);
- }
- }
- }
- }
|