mirror of
https://github.com/azaion/annotations.git
synced 2026-04-22 09:56:31 +00:00
add quartz for jobs
configure auto-scan folder and create hls files job WIP
This commit is contained in:
@@ -8,7 +8,9 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="linq2db" Version="5.4.1" />
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
|
||||
<PackageReference Include="MySqlConnector" Version="2.3.7" />
|
||||
<PackageReference Include="Quartz.AspNetCore" Version="3.11.0" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -2,9 +2,6 @@ namespace Azaion.Repository;
|
||||
|
||||
public class Constants
|
||||
{
|
||||
public const string MEDIA_HLS_FOLDER = "/azaion-media/hls";
|
||||
public const string M3_U8_EXT = "m3u8";
|
||||
public const string TS_EXT = "ts";
|
||||
public const string FFMPEG_FILE = "/opt/homebrew/bin/ffmpeg";
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
namespace Azaion.Repository.DTO.Configs;
|
||||
|
||||
public class ConnectionStrings
|
||||
{
|
||||
public string? AzaionDb { get; set; }
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
namespace Azaion.Repository.DTO.Configs;
|
||||
|
||||
public class FoldersConfig
|
||||
{
|
||||
public string VideosFolder { get; set; } = null!;
|
||||
public string HlsFolder { get; set; } = null!;
|
||||
public string FfmpegExecutable { get; set; } = null!;
|
||||
}
|
||||
@@ -1,7 +0,0 @@
|
||||
namespace Azaion.Repository.DTO;
|
||||
|
||||
public class ConnectionStrings
|
||||
{
|
||||
public string? FraudDb { get; set; }
|
||||
public string? FraudDbMsSql { get; set; }
|
||||
}
|
||||
@@ -1,4 +1,6 @@
|
||||
using Azaion.Repository.DTO.Configs;
|
||||
using Azaion.Video.DTO;
|
||||
using Microsoft.Extensions.Options;
|
||||
using IOPath = System.IO.Path;
|
||||
|
||||
namespace Azaion.Repository.Entities;
|
||||
@@ -15,8 +17,12 @@ public class Media
|
||||
|
||||
|
||||
private string MediaName => IOPath.GetFileNameWithoutExtension(Path);
|
||||
private string OutDir => Directory.CreateDirectory(IOPath.Combine(Constants.MEDIA_HLS_FOLDER, MediaName)).FullName;
|
||||
private string OutDir(IOptions<FoldersConfig> config) =>
|
||||
Directory.CreateDirectory(IOPath.Combine(config.Value.HlsFolder, MediaName)).FullName;
|
||||
|
||||
public string M3U8File => IOPath.Combine(OutDir, $"{MediaName}.{Constants.M3_U8_EXT}");
|
||||
public string SegmentFile => IOPath.Combine(OutDir, $"{MediaName}%03d.{Constants.TS_EXT}");
|
||||
public string M3U8File(IOptions<FoldersConfig> config) =>
|
||||
IOPath.Combine(OutDir(config), $"{MediaName}.{Constants.M3_U8_EXT}");
|
||||
|
||||
public string SegmentFile(IOptions<FoldersConfig> config) =>
|
||||
IOPath.Combine(OutDir(config), $"{MediaName}%03d.{Constants.TS_EXT}");
|
||||
}
|
||||
|
||||
@@ -0,0 +1,27 @@
|
||||
using Azaion.Repository.Jobs;
|
||||
using Quartz;
|
||||
|
||||
namespace Azaion.Repository.Extensions
|
||||
{
|
||||
public static class QuartzJobConfigHelper
|
||||
{
|
||||
public static IServiceCollectionQuartzConfigurator RegisterJob<T>(this IServiceCollectionQuartzConfigurator q,
|
||||
string cron, TimeSpan? startForDebug = null) where T: BaseJob
|
||||
{
|
||||
var jobName = typeof(T).Name;
|
||||
var jobKey = new JobKey(jobName);
|
||||
q.AddJob<T>(jobKey);
|
||||
q.AddTrigger(t => t.WithIdentity($"{jobName}_TRIGGER")
|
||||
.ForJob(jobKey)
|
||||
#if DEBUG
|
||||
.StartAt(DateTimeOffset.Now.Add(startForDebug ?? TimeSpan.FromHours(10)))
|
||||
.WithSimpleSchedule(b => b.WithIntervalInHours(5))
|
||||
#else
|
||||
.StartAt(DateTimeOffset.Now.AddMinutes(1))
|
||||
.WithCronSchedule(cron, builder => builder.InTimeZone(TimeZoneInfo.Utc))
|
||||
#endif
|
||||
);
|
||||
return q;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Quartz;
|
||||
|
||||
namespace Azaion.Repository.Jobs
|
||||
{
|
||||
[DisallowConcurrentExecution]
|
||||
public abstract class BaseJob(ILogger<BaseJob> logger) : IJob
|
||||
{
|
||||
protected abstract Task ExecuteInner(IJobExecutionContext context);
|
||||
|
||||
public async Task Execute(IJobExecutionContext context)
|
||||
{
|
||||
logger.LogDebug($"Start {GetType().Name}");
|
||||
|
||||
try
|
||||
{
|
||||
await ExecuteInner(context);
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
logger.LogError(e, e.Message);
|
||||
throw;
|
||||
}
|
||||
|
||||
logger.LogDebug($"{GetType().Name} Finished");
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,11 @@
|
||||
CREATE USER 'azaion-user' IDENTIFIED BY 'Aza1on@db123'
|
||||
CREATE DATABASE azaion;
|
||||
GRANT SELECT, INSERT, REFERENCES, UPDATE, DELETE, CREATE, INDEX, DROP, ALTER, CREATE TEMPORARY TABLES, LOCK TABLES
|
||||
ON azaion.*
|
||||
TO 'azaion-user';
|
||||
GRANT FILE ON *.* TO 'azaion-user';
|
||||
|
||||
CREATE USER 'azaion-admin' IDENTIFIED BY 'Aza1on@db-admin123';
|
||||
GRANT ALL ON azaion.* TO 'azaion-admin';
|
||||
GRANT FILE ON *.* TO 'azaion-admin';
|
||||
|
||||
CREATE USER 'azaion-user' IDENTIFIED BY 'Aza1on@db123';
|
||||
GRANT SELECT, INSERT, UPDATE, DELETE ON azaion.* TO 'azaion-user';
|
||||
|
||||
CREATE USER 'azaion-user-read' IDENTIFIED BY 'Az@10n-ro';
|
||||
GRANT SELECT ON azaion.* TO 'azaion-user';
|
||||
@@ -10,4 +10,8 @@
|
||||
<ProjectReference Include="..\Azaion.Repository\Azaion.Repository.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.Extensions.Options" Version="8.0.2" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
|
||||
@@ -0,0 +1,38 @@
|
||||
using System.Diagnostics;
|
||||
using Azaion.Repository;
|
||||
using Azaion.Repository.DTO.Configs;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Azaion.Video;
|
||||
|
||||
public interface IFfmpegManager
|
||||
{
|
||||
void ConvertToHls(string video, string segmentFile, string outFile);
|
||||
}
|
||||
|
||||
public class FfmpegManager(IOptions<FoldersConfig> config) : IFfmpegManager
|
||||
{
|
||||
public void ConvertToHls(string video, string segmentFile, string outFile)
|
||||
{
|
||||
string arguments = string.Concat($"-i \"{video}\" ",
|
||||
"-f hls ",
|
||||
"-hls_time 2 ",
|
||||
"-hls_playlist_type vod ",
|
||||
"-hls_flags independent_segments ",
|
||||
"-hls_segment_type mpegts ",
|
||||
$"-hls_segment_filename \"{segmentFile}\"",
|
||||
$"\"{outFile}\"");
|
||||
|
||||
var process = new Process
|
||||
{
|
||||
StartInfo = new()
|
||||
{
|
||||
FileName = config.Value.FfmpegExecutable,
|
||||
Arguments = arguments,
|
||||
UseShellExecute = true,
|
||||
},
|
||||
EnableRaisingEvents = true,
|
||||
};
|
||||
process.Start();
|
||||
}
|
||||
}
|
||||
+1
-1
@@ -3,7 +3,7 @@ using Azaion.Repository.Entities;
|
||||
|
||||
namespace Azaion.Video;
|
||||
|
||||
public interface IVideoManager
|
||||
public interface IVideoRepository
|
||||
{
|
||||
List<VideoDto> GetVideos();
|
||||
Media Get(Guid mediaId);
|
||||
+4
-2
@@ -1,11 +1,13 @@
|
||||
using System.Diagnostics;
|
||||
using Azaion.Repository;
|
||||
using Azaion.Repository.DTO;
|
||||
using Azaion.Repository.DTO.Configs;
|
||||
using Azaion.Repository.Entities;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Azaion.Video;
|
||||
|
||||
public class VideoManager(IDbFactory dbFactory) : IVideoManager
|
||||
public class VideoRepository(IDbFactory dbFactory, IOptions<FoldersConfig> foldersConfig) : IVideoRepository
|
||||
{
|
||||
public List<VideoDto> GetVideos()
|
||||
{
|
||||
@@ -38,7 +40,7 @@ public class VideoManager(IDbFactory dbFactory) : IVideoManager
|
||||
{
|
||||
StartInfo = new()
|
||||
{
|
||||
FileName = Constants.FFMPEG_FILE,
|
||||
FileName = foldersConfig.Value.FfmpegExecutable,
|
||||
Arguments = arguments,
|
||||
UseShellExecute = true,
|
||||
},
|
||||
@@ -8,6 +8,8 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.6"/>
|
||||
<PackageReference Include="Quartz.AspNetCore" Version="3.11.0" />
|
||||
<PackageReference Include="Quartz.Serialization.Json" Version="3.11.0" />
|
||||
<PackageReference Include="Serilog.Extensions.Hosting" Version="8.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.Console" Version="6.0.0" />
|
||||
<PackageReference Include="Serilog.Sinks.File" Version="6.0.0" />
|
||||
|
||||
@@ -1,19 +1,22 @@
|
||||
using Azaion.Repository.DTO.Configs;
|
||||
using Azaion.Video;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Options;
|
||||
|
||||
namespace Azaion.WebService.Controllers;
|
||||
|
||||
[Route("/controller")]
|
||||
[Route("/{controller}")]
|
||||
[ApiController]
|
||||
public class VideoController(IVideoManager videoManager) : Controller
|
||||
public class VideoController(IVideoRepository videoRepository, IOptions<FoldersConfig> config) : Controller
|
||||
{
|
||||
[HttpGet("{guid}")]
|
||||
public IActionResult GetVideo(Guid guid)
|
||||
{
|
||||
var media = videoManager.Get(guid);
|
||||
var fileStream = new FileStream(media.M3U8File, FileMode.Open);
|
||||
var media = videoRepository.Get(guid);
|
||||
var fileStream = new FileStream(media.M3U8File(config), FileMode.Open);
|
||||
var fileSize = new FileInfo(media.Path).Length;
|
||||
Response.ContentLength = fileSize;
|
||||
return File(fileStream, "application/x-mpegURL", true);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
using System.Collections.Specialized;
|
||||
using Azaion.Repository;
|
||||
using Azaion.Repository.DTO;
|
||||
using Azaion.Repository.DTO.Configs;
|
||||
using Azaion.Video;
|
||||
using Microsoft.Extensions.Options;
|
||||
using Quartz;
|
||||
using Serilog;
|
||||
using Serilog.Events;
|
||||
|
||||
@@ -22,9 +25,23 @@ builder.Services.AddControllers();
|
||||
builder.Services.AddEndpointsApiExplorer();
|
||||
builder.Services.AddSwaggerGen();
|
||||
|
||||
builder.Services.Configure<FoldersConfig>(builder.Configuration.GetSection(nameof(FoldersConfig)));
|
||||
builder.Services.Configure<ConnectionStrings>(builder.Configuration.GetSection(nameof(ConnectionStrings)));
|
||||
builder.Services.AddSingleton<IDbFactory, DbFactory>(sp => new DbFactory(sp.GetService<IOptions<ConnectionStrings>>()!.Value.FraudDb!));
|
||||
builder.Services.AddScoped<IVideoManager, VideoManager>();
|
||||
builder.Services.AddScoped<IVideoRepository, VideoRepository>();
|
||||
|
||||
var connStr = builder.Configuration.Get<ConnectionStrings>();
|
||||
|
||||
builder.Services.AddQuartz(q =>
|
||||
{
|
||||
q.SchedulerId = "AzaionScheduler";
|
||||
q.SchedulerName = "Azaion Scheduler";
|
||||
q.UsePersistentStore(c =>
|
||||
{
|
||||
c.UseNewtonsoftJsonSerializer();
|
||||
c.UseMySql(connStr.FraudDbMsSql);
|
||||
});
|
||||
});
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
@@ -37,4 +54,4 @@ if (app.Environment.IsDevelopment())
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
app.MapControllers();
|
||||
app.Run();
|
||||
app.Run();
|
||||
@@ -4,5 +4,10 @@
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"FoldersConfig": {
|
||||
"VideosFolder": "E:\\Azaion3\\Videos",
|
||||
"HlsFolder": "/azaion-media/hls",
|
||||
"FFMPEG_EXECUTABLE": "/opt/homebrew/bin/ffmpeg"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -5,5 +5,13 @@
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
"AllowedHosts": "*",
|
||||
"ConnectionStrings": {
|
||||
"AzaionDb": ""
|
||||
},
|
||||
"FoldersConfig": {
|
||||
"VideosFolder": "/azaion-media",
|
||||
"HlsFolder": "/azaion-media/hls",
|
||||
"FFMPEG_EXECUTABLE": "/opt/homebrew/bin/ffmpeg"
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user