Skip to content

Commit

Permalink
基本完成对Core项目中Manager的异步和依赖注入改造
Browse files Browse the repository at this point in the history
  • Loading branch information
autodotua committed May 5, 2024
1 parent c271f18 commit 38d295c
Show file tree
Hide file tree
Showing 11 changed files with 140 additions and 112 deletions.
20 changes: 20 additions & 0 deletions SimpleFFmpegGUI.Core/DependencyInjectionExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
using Microsoft.Extensions.DependencyInjection;
using SimpleFFmpegGUI.Manager;
using SimpleFFmpegGUI.Model;

namespace SimpleFFmpegGUI
{
public static class DependencyInjectionExtension
{
public static void AddFFmpegServices(this ServiceCollection services)
{
services.AddDbContext<FFmpegDbContext>()
.AddTransient<Logger>()
.AddTransient<LogManager>()
.AddTransient<ConfigManager>()
.AddTransient<PresetManager>()
.AddTransient<TaskManager>()
.AddSingleton<QueueManager>();
}
}
}
30 changes: 17 additions & 13 deletions SimpleFFmpegGUI.Core/Logger.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,6 @@

namespace SimpleFFmpegGUI
{
public class LogEventArgs : EventArgs
{
public LogEventArgs(Log log)
{
Log = log;
}

public Log Log { get; set; }
}

public class ExceptionEventArgs : EventArgs
{
public ExceptionEventArgs(Exception exception)
Expand All @@ -31,17 +21,31 @@ public ExceptionEventArgs(Exception exception, string message)
Exception = exception;
Message = message;
}
public string Message { get; }
public Exception Exception { get; }
public string Message { get; }
}

public class LogEventArgs : EventArgs
{
public LogEventArgs(Log log)
{
Log = log;
}

public Log Log { get; set; }
}
public class Logger : IDisposable
{
private static HashSet<Logger> allLoggers = new HashSet<Logger>();
private object lockObj = new object();
private FFmpegDbContext db = FFmpegDbContext.GetNew();
private readonly FFmpegDbContext db;
private bool disposed = false;
private object lockObj = new object();
private Timer timer;

public Logger(FFmpegDbContext db)
{
this.db = db;
}
public Logger()
{
lock (lockObj)
Expand Down
17 changes: 11 additions & 6 deletions SimpleFFmpegGUI.Core/Manager/ConfigManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,30 @@

namespace SimpleFFmpegGUI.Manager
{
public static class ConfigManager
public class ConfigManager
{
public const string DefaultProcessPriorityKey = "DefaultProcessPriority";
private static readonly Dictionary<string, object> cache = new Dictionary<string, object>();

public static int DefaultProcessPriority
private readonly FFmpegDbContext db;

public ConfigManager(FFmpegDbContext db)
{
this.db = db;
}

public int DefaultProcessPriority
{
get => GetConfig(DefaultProcessPriorityKey, 2);
set => SetConfig(DefaultProcessPriorityKey, value);
}

public static T GetConfig<T>(string key, T defaultValue)
public T GetConfig<T>(string key, T defaultValue)
{
if (cache.ContainsKey(key))
{
return (T)cache[key];
}
using var db = FFmpegDbContext.GetNew();
var item = db.Configs.Where(p => p.Key == key).FirstOrDefault();
if (item == null)
{
Expand All @@ -38,13 +44,12 @@ public static T GetConfig<T>(string key, T defaultValue)
logger.Info($"读取配置:[{key}]={value}");
return value;
}
public static void SetConfig<T>(string key, T value)
public void SetConfig<T>(string key, T value)
{
if (cache.ContainsKey(key))
{
cache[key] = value;
}
using var db = FFmpegDbContext.GetNew();
var item = db.Configs.Where(p => p.Key == key).FirstOrDefault();
if (item == null)
{
Expand Down
5 changes: 3 additions & 2 deletions SimpleFFmpegGUI.Core/Manager/FFmpegManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,10 @@ public async Task CancelAsync()
/// 获取错误信息
/// </summary>
/// <returns></returns>
public string GetErrorMessage()
public async Task<string> GetErrorMessageAsync()
{
var logs = LogManager.GetLogs('O', Task.Id, DateTime.Now.AddSeconds(-5));
LogManager manager = new LogManager(new FFmpegDbContext());
var logs =await manager.GetLogsAsync('O', Task.Id, DateTime.Now.AddSeconds(-5));
var log = logs.List
.Where(p => ErrorMessageRegexs.Any(q => q.IsMatch(p.Message)))
.OrderByDescending(p => p.Time).FirstOrDefault();
Expand Down
5 changes: 3 additions & 2 deletions SimpleFFmpegGUI.Core/Manager/FFmpegProcess.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System;
using SimpleFFmpegGUI.Model;
using System;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;
Expand All @@ -21,7 +22,7 @@ public class FFmpegProcess

private FFmpegProcess()
{
Priority = ConfigManager.DefaultProcessPriority;
Priority = new ConfigManager(new FFmpegDbContext()).DefaultProcessPriority;
}
public FFmpegProcess(string argument) : this()
{
Expand Down
21 changes: 15 additions & 6 deletions SimpleFFmpegGUI.Core/Manager/LogManager.cs
Original file line number Diff line number Diff line change
@@ -1,21 +1,30 @@
using SimpleFFmpegGUI.Dto;
using Microsoft.EntityFrameworkCore;
using SimpleFFmpegGUI.Dto;
using SimpleFFmpegGUI.Model;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace SimpleFFmpegGUI.Manager
{
public static class LogManager
public class LogManager
{
public static PagedListDto<Log> GetLogs(char? type = null,
private readonly FFmpegDbContext db;

public LogManager(FFmpegDbContext db)
{
this.db = db;
}

public async Task<PagedListDto<Log>> GetLogsAsync(char? type = null,
int taskId = 0,
DateTime? from = null,
DateTime? to = null,
int skip = 0,
int take = 0)
{
Logger.SaveAll();
using var db = FFmpegDbContext.GetNew();

IQueryable<Log> logs = db.Logs;
if (type.HasValue)
{
Expand All @@ -34,7 +43,7 @@ public static PagedListDto<Log> GetLogs(char? type = null,
logs = logs.Where(p => p.TaskId == taskId);
}
logs = logs.OrderByDescending(p => p.Time);
int count = logs.Count();
int count = await logs.CountAsync();
if (skip > 0)
{
logs = logs.Skip(skip);
Expand All @@ -43,7 +52,7 @@ public static PagedListDto<Log> GetLogs(char? type = null,
{
logs = logs.Take(take);
}
return new PagedListDto<Log>(logs, count);
return new PagedListDto<Log>(await logs.ToListAsync(), count);
}
}
}
Loading

0 comments on commit 38d295c

Please sign in to comment.