Skip to content

Commit

Permalink
完成TaskManager的改造,开始QueueManager的改造
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed May 5, 2024
1 parent d3de94d commit c271f18
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 112 deletions.
6 changes: 3 additions & 3 deletions SimpleFFmpegGUI.Core/Dto/PagedListDto.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ public PagedListDto()
List = new List<T>();
}

public PagedListDto(IEnumerable<T> collection, int totalCount)
public PagedListDto(IList<T> list, int totalCount)
{
TotalCount = totalCount;
List = new List<T>(collection);
List = list;
}

public List<T> List { get; set; }
public IList<T> List { get; set; }

[JsonProperty]
public int TotalCount { get; set; }
Expand Down
11 changes: 7 additions & 4 deletions SimpleFFmpegGUI.Core/Manager/QueueManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,8 @@ namespace SimpleFFmpegGUI.Manager
{
public class QueueManager
{
private Logger logger = new Logger();
private readonly FFmpegDbContext db;
private readonly Logger logger;
private bool cancelQueue = false;

/// <summary>
Expand All @@ -23,8 +24,10 @@ public class QueueManager
private DateTime? scheduleTime = null;
private List<FFmpegManager> taskProcessManagers = new List<FFmpegManager>();

public QueueManager()
public QueueManager(FFmpegDbContext db, Logger logger)
{
this.db = db;
this.logger = logger;
}

/// <summary>
Expand Down Expand Up @@ -218,7 +221,7 @@ private IQueryable<TaskInfo> GetQueueTasks(FFmpegDbContext db)
.Where(p => !p.IsDeleted);
}

private async Task ProcessTaskAsync(FFmpegDbContext db, TaskInfo task, bool main)
private async Task ProcessTaskAsync(TaskInfo task, bool main)
{
FFmpegManager ffmpegManager = new FFmpegManager(task);

Expand All @@ -240,7 +243,7 @@ private async Task ProcessTaskAsync(FFmpegDbContext db, TaskInfo task, bool main
{
logger.Error(task, "运行错误:" + ex.ToString());
task.Status = TaskStatus.Error;
task.Message = ex is FFmpegArgumentException ?
task.Message = ex is FFmpegArgumentException ?
ex.Message : ffmpegManager.GetErrorMessage() ?? "运行错误,请查看日志";
}
else
Expand Down
160 changes: 60 additions & 100 deletions SimpleFFmpegGUI.Core/Manager/TaskManager.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using SimpleFFmpegGUI.Dto;
using Microsoft.EntityFrameworkCore;
using SimpleFFmpegGUI.Dto;
using SimpleFFmpegGUI.Model;
using System;
using System.Collections.Generic;
Expand All @@ -8,22 +9,36 @@

namespace SimpleFFmpegGUI.Manager
{
public static class TaskManager
public class TaskManager
{
static TaskManager()
private readonly bool taskChecked = false;
private static readonly object lockObj = new object();
private readonly FFmpegDbContext db;
private readonly Logger logger;
private readonly QueueManager queue;

public TaskManager(FFmpegDbContext db, Logger logger, QueueManager queueManager)
{
using var db = FFmpegDbContext.GetNew();
foreach (var item in db.Tasks.Where(p => p.Status == TaskStatus.Processing))
this.db = db;
this.logger = logger;
queue = queueManager;
if (!taskChecked)
{
item.Status = TaskStatus.Error;
item.Message = "状态异常:启动时处于正在运行状态";
lock (lockObj)
{
foreach (var item in db.Tasks.Where(p => p.Status == TaskStatus.Processing))
{
item.Status = TaskStatus.Error;
item.Message = "状态异常:启动时处于正在运行状态";
}
db.SaveChanges();
}
}
db.SaveChanges();

}

public static TaskInfo AddTask(TaskType type, List<InputArguments> path, string outputPath, OutputArguments arg)
public async Task<TaskInfo> AddTaskAsync(TaskType type, List<InputArguments> path, string outputPath, OutputArguments arg)
{
using FFmpegDbContext db = FFmpegDbContext.GetNew();
var task = new TaskInfo()
{
Type = type,
Expand All @@ -32,52 +47,35 @@ public static TaskInfo AddTask(TaskType type, List<InputArguments> path, string
Arguments = arg
};
db.Tasks.Add(task);
db.SaveChanges();
using Logger logger = new Logger();
await db.SaveChangesAsync();
logger.Info(task, "新建任务");
return task;
}

public static List<TaskInfo> GetCurrentTasks(DateTime startTime)
public async Task<List<TaskInfo>> GetCurrentTasks(DateTime startTime)
{
using var db = FFmpegDbContext.GetNew();
var tasks = db.Tasks.Where(p => p.IsDeleted == false);
var runningTasks = tasks.Where(p => p.Status == TaskStatus.Processing);
var queueTasks = tasks.Where(p => p.Status == TaskStatus.Queue);
var doneTasks = tasks.Where(p => p.Status == TaskStatus.Done).Where(p => p.StartTime > startTime);
var errorTasks = tasks.Where(p => p.Status == TaskStatus.Done).Where(p => p.StartTime > startTime);
var cancelTasks = tasks.Where(p => p.Status == TaskStatus.Done).Where(p => p.StartTime > startTime);
return runningTasks
.Concat(queueTasks)
.Concat(doneTasks)
.Concat(errorTasks)
.Concat(cancelTasks)
.ToList();
var runningTasks = await tasks.Where(p => p.Status == TaskStatus.Processing).ToListAsync();
var queueTasks = await tasks.Where(p => p.Status == TaskStatus.Queue).ToListAsync();
var doneTasks = await tasks.Where(p => p.Status == TaskStatus.Done).Where(p => p.StartTime > startTime).ToListAsync();
var errorTasks = await tasks.Where(p => p.Status == TaskStatus.Done).Where(p => p.StartTime > startTime).ToListAsync();
var cancelTasks = await tasks.Where(p => p.Status == TaskStatus.Done).Where(p => p.StartTime > startTime).ToListAsync();
return [.. runningTasks, .. queueTasks, .. doneTasks, .. errorTasks, .. cancelTasks];
}

public static PagedListDto<TaskInfo> GetTasks(TaskStatus? status = null, int skip = 0, int take = 0)
public async Task<PagedListDto<TaskInfo>> GetTasksAsync(TaskStatus? status = null, int skip = 0, int take = 0)
{
using var db = FFmpegDbContext.GetNew();
IQueryable<TaskInfo> tasks = db.Tasks
.Where(p => p.IsDeleted == false);
if (status.HasValue)
{
tasks = tasks.Where(p => p.Status == status);
switch (status.Value)
tasks = status.Value switch
{
case TaskStatus.Queue:
tasks = tasks.OrderBy(p => p.CreateTime);
break;

case TaskStatus.Processing:
case TaskStatus.Done:
tasks = tasks.OrderByDescending(p => p.StartTime);
break;

default:
tasks = tasks.OrderByDescending(p => p.CreateTime);
break;
}
TaskStatus.Queue => tasks.OrderBy(p => p.CreateTime),
TaskStatus.Processing or TaskStatus.Done => tasks.OrderByDescending(p => p.StartTime),
_ => tasks.OrderByDescending(p => p.CreateTime),
};
}
else
{
Expand All @@ -92,46 +90,38 @@ public static PagedListDto<TaskInfo> GetTasks(TaskStatus? status = null, int ski
{
tasks = tasks.Take(take);
}
return new PagedListDto<TaskInfo>(tasks, count);
return new PagedListDto<TaskInfo>(await tasks.ToListAsync(), count);
}

public static TaskInfo GetTask(int id)
public async Task<TaskInfo> GetTaskAsync(int id)
{
using var db = FFmpegDbContext.GetNew();
var task = db.Tasks.Find(id);
var task = await db.Tasks.FindAsync(id);
return task;
}

public static bool HasQueueTasks()
public Task<bool> HasQueueTasksAsync()
{
using var db = FFmpegDbContext.GetNew();
return db.Tasks.Any(p => p.IsDeleted == false && p.Status == TaskStatus.Queue);
return db.Tasks.AnyAsync(p => p.IsDeleted == false && p.Status == TaskStatus.Queue);
}

public static void ResetTask(int id, QueueManager queue)
public async Task ResetTaskAsync(int id, QueueManager queue)
{
using var db = FFmpegDbContext.GetNew();
TaskInfo task = db.Tasks.Find(id);
if (task == null)
{
throw new ArgumentException($"找不到ID为{id}的任务");
}
TaskInfo task = await db.Tasks.FindAsync(id) ?? throw new ArgumentException($"找不到ID为{id}的任务");
if (queue.Tasks.Any(p => p.Id == id))
{
throw new Exception("ID为{id}的任务正在进行中");
}
task.Status = TaskStatus.Queue;
db.Update(task);
db.SaveChanges();
await db.SaveChangesAsync();
}

public static int TryResetTasks(IEnumerable<int> ids, QueueManager queue)
public async Task<int> TryResetTasksAsync(IEnumerable<int> ids, QueueManager queue)
{
using var db = FFmpegDbContext.GetNew();
int count = 0;
foreach (var id in ids)
{
TaskInfo task = db.Tasks.Find(id);
TaskInfo task = await db.Tasks.FindAsync(id);
if (task == null)
{
continue;
Expand All @@ -145,47 +135,24 @@ public static int TryResetTasks(IEnumerable<int> ids, QueueManager queue)
count++;
}

db.SaveChanges();
await db.SaveChangesAsync();
return count;
}

public static void CancelTask(int id, QueueManager queue)
public async Task CancelTaskAsync(int id)
{
using var db = FFmpegDbContext.GetNew();
TaskInfo task = db.Tasks.Find(id);
if (task == null)
{
throw new ArgumentException($"找不到ID为{id}的任务");
}
TaskInfo task = await db.Tasks.FindAsync(id) ?? throw new ArgumentException($"找不到ID为{id}的任务");
CheckCancelingTask(task);
if (queue.Tasks.Any(p => p.Id == task.Id))
{
queue.Managers.First(p => p.Task.Id == task.Id).Cancel();
}
task.Status = TaskStatus.Cancel;
db.Update(task);
db.SaveChanges();
}

public static async Task CancelTaskAsync(int id, QueueManager queue)
{
using var db = FFmpegDbContext.GetNew();
TaskInfo task = await db.Tasks.FindAsync(id);
if (task == null)
{
throw new ArgumentException($"找不到ID为{id}的任务");
}
CheckCancelingTask(task);
if (queue.Tasks.Any(p => p.Id == task.Id))
{
await queue.Managers.First(p => p.Task.Id == task.Id).CancelAsync();
}
task.Status = TaskStatus.Cancel;
db.Update(task);
await db.SaveChangesAsync();
}

private static void CheckCancelingTask(TaskInfo task)
private void CheckCancelingTask(TaskInfo task)
{
if (task.Status == TaskStatus.Cancel)
{
Expand All @@ -201,13 +168,12 @@ private static void CheckCancelingTask(TaskInfo task)
}
}

public static int TryCancelTasks(IEnumerable<int> ids, QueueManager queue)
public async Task<int> TryCancelTasksAsync(IEnumerable<int> ids)
{
using var db = FFmpegDbContext.GetNew();
int count = 0;
foreach (var id in ids)
{
TaskInfo task = db.Tasks.Find(id);
TaskInfo task = await db.Tasks.FindAsync(id);
if (task == null)
{
continue;
Expand Down Expand Up @@ -236,30 +202,24 @@ public static int TryCancelTasks(IEnumerable<int> ids, QueueManager queue)
return count;
}

public static void DeleteTask(int id, QueueManager queue)
public async Task DeleteTask(int id)
{
using var db = FFmpegDbContext.GetNew();
TaskInfo task = db.Tasks.Find(id);
if (task == null)
{
throw new ArgumentException($"找不到ID为{id}的任务");
}
TaskInfo task = await db.Tasks.FindAsync(id) ?? throw new ArgumentException($"找不到ID为{id}的任务");
if (queue.Tasks.Any(p => p.Id == task.Id))
{
queue.Managers.First(p => p.Task.Id == task.Id).Cancel();
}
task.IsDeleted = true;
db.Update(task);
db.SaveChanges();
await db.SaveChangesAsync();
}

public static int TryDeleteTasks(IEnumerable<int> ids, QueueManager queue)
public async Task<int> TryDeleteTasks(IEnumerable<int> ids)
{
using var db = FFmpegDbContext.GetNew();
int count = 0;
foreach (var id in ids)
{
TaskInfo task = db.Tasks.Find(id);
TaskInfo task =await db.Tasks.FindAsync(id);
if (task == null)
{
continue;
Expand Down
5 changes: 0 additions & 5 deletions SimpleFFmpegGUI.Core/Model/FFmpegDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -45,11 +45,6 @@ public void Check()
}
}

internal static FFmpegDbContext GetNew()
{
return new FFmpegDbContext();
}

protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
{
optionsBuilder.UseSqlite(connectionString);
Expand Down

0 comments on commit c271f18

Please sign in to comment.