add quartz for jobs

configure auto-scan folder and create hls files job WIP
This commit is contained in:
Oleksandr Bezdieniezhnykh
2024-07-26 14:11:29 +03:00
parent 5e55210eac
commit 7807f5bc90
18 changed files with 179 additions and 28 deletions
@@ -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';