This commit is contained in:
owenchen committed 2026-09-21 16:54:56 +08:00
1 parent 9e79a6a67c
commit 7c8af2fb08
14 files changed
+328 -45

No files matched your search

+63 -9
View File
@@ -1,6 +1,12 @@
using CloudBuilder.Core.DatabaseAccessor.Entity;
using CliWrap;
using CloudBuilder.Core.DatabaseAccessor.Entity;
using CloudBuilder.Core.DependencyInjection.Task;
using CloudBuilder.Core.Service;
using CloudBuilder.Master.Entity;
using CloudBuilder.Security.Entity;
using CloudBuilder.Security.Service;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
namespace CloudBuilder.Topshelf.Task
{
@@ -8,27 +14,75 @@ namespace CloudBuilder.Topshelf.Task
public class RunTask : IScheduleTask
{
private readonly IApplicationService service;
private readonly IConfiguration config;
private FileLog log;
public RunTask(IApplicationService service, IConfiguration config)
public RunTask(IApplicationService service)
{
this.service = service;
this.config = config;
log = new FileLog(service.Configuration);
}
public void Run(Dictionary<string, string> bodyDict)
{
File.AppendAllText("log.txt", "RunTask:" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + "\r\n");
log.WriteToFile("RunTask");
try
{
new FileLog(config).WriteToFile("RunTask");
IRepository<SecurityScheduleTaskQueueEntity> taskRepository = service.GetRepository<IRepository<SecurityScheduleTaskQueueEntity>>();
SecurityScheduleTaskQueueEntity task = taskRepository.DetachedEntities.OrderBy(x => x.QueueId).FirstOrDefault();
if (task == null) return;
IRepository<DmsFileEntity> repositoryDmsFile = service.GetRepository<IRepository<DmsFileEntity>>();
DmsFileEntity dmsFile = repositoryDmsFile.DetachedEntities.Where(x => x.OrderKey == task.DocumentId).FirstOrDefault();
if (dmsFile == null) return;
RunMyProcess(task.TaskId, task.TaskParam);
dmsFile.TranscodeIndc = YesNoPolicy.YES;
repositoryDmsFile.UpdateNow(dmsFile);
}
catch (Exception ex)
{
File.AppendAllText("log.txt", "RunTask:" + ex.Message);
throw;
log.WriteToFile(ex.Message);
}
}
/// <summary>
/// 执行cmd指令(异步不等待,await要求等待返回,转码太耗时)
/// </summary>
/// <param name="parameters"></param>
public void RunMyProcess(string exePath, string parameters)
{
try
{
parameters = parameters.Replace("/app/data", "/vol1/1000/Docker/NET8/DEV/data");
log.WriteToFile(exePath + parameters);
if (string.IsNullOrEmpty(exePath)) return;
// 获取异步任务
var commandTask = Cli.Wrap(exePath)
.WithArguments(parameters)
.WithValidation(CommandResultValidation.None) // 关键:允许非0退出码而不抛异常,以便我们能拿到错误输出
.ExecuteAsync();
// 阻塞当前线程,等待任务完成(因为你的外层是同步的 void 方法)
var result = commandTask.Task.GetAwaiter().GetResult();
// 记录退出码和标准错误输出
log.WriteToFile($"FFmpeg ExitCode: {result.ExitCode}");
}
catch (Exception ex)
{
log.WriteToFile(ex.Message);
throw new ValidatedException(ex.Message);
}
}
}
}