* Created AzureDataModel
* Created Hangfire API
This commit is contained in:
31
AzureDataModel/AzureDataModel.csproj
Normal file
31
AzureDataModel/AzureDataModel.csproj
Normal file
@@ -0,0 +1,31 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper" Version="13.0.1" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Design" Version="9.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.SqlServer" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.EntityFrameworkCore.Tools" Version="9.0.0">
|
||||
<PrivateAssets>all</PrivateAssets>
|
||||
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
|
||||
</PackageReference>
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.FileExtensions" Version="9.0.0" />
|
||||
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="9.0.0" />
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<None Update="appsettings_Azure.json">
|
||||
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
|
||||
</None>
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
40
AzureDataModel/AzureDbContext.cs
Normal file
40
AzureDataModel/AzureDbContext.cs
Normal file
@@ -0,0 +1,40 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using TaskScheduler = AzureDataModel.Entities.TaskScheduler;
|
||||
|
||||
namespace AzureDataModel;
|
||||
|
||||
public class AzureDbContext : DbContext
|
||||
{
|
||||
public AzureDbContext(DbContextOptions<AzureDbContext> options)
|
||||
: base(options)
|
||||
{
|
||||
}
|
||||
|
||||
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
||||
{
|
||||
var configuration = new ConfigurationBuilder()
|
||||
.SetBasePath(Directory.GetCurrentDirectory())
|
||||
.AddJsonFile("appsettings.json")
|
||||
.Build();
|
||||
|
||||
var connectionString = configuration.GetConnectionString("AzureConnection");
|
||||
optionsBuilder.UseAzureSql(connectionString, options => options.CommandTimeout(300));
|
||||
}
|
||||
|
||||
public DbSet<TaskScheduler> TaskSchedulers { get; set; }
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<TaskScheduler>()
|
||||
.HasKey(e => e.Id);
|
||||
|
||||
modelBuilder.Entity<TaskScheduler>()
|
||||
.Property(e => e.RowPointer)
|
||||
.HasDefaultValueSql("newid()");
|
||||
|
||||
modelBuilder.Entity<TaskScheduler>()
|
||||
.Property(e => e.CreateDate)
|
||||
.HasDefaultValueSql("getdate()");
|
||||
}
|
||||
}
|
||||
10
AzureDataModel/Dtos/TaskScheduler.cs
Normal file
10
AzureDataModel/Dtos/TaskScheduler.cs
Normal file
@@ -0,0 +1,10 @@
|
||||
namespace AzureDataModel.Dtos;
|
||||
|
||||
public class TaskSchedulerDto
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Path { get; set; }
|
||||
public string CronOptions { get; set; }
|
||||
public DateTime CreateDate { get; set; }
|
||||
}
|
||||
11
AzureDataModel/Entities/TaskScheduler.cs
Normal file
11
AzureDataModel/Entities/TaskScheduler.cs
Normal file
@@ -0,0 +1,11 @@
|
||||
namespace AzureDataModel.Entities;
|
||||
|
||||
public class TaskScheduler
|
||||
{
|
||||
public int Id { get; set; }
|
||||
public Guid RowPointer { get; set; }
|
||||
public string Name { get; set; }
|
||||
public string Path { get; set; }
|
||||
public string CronOptions { get; set; }
|
||||
public DateTime CreateDate { get; set; }
|
||||
}
|
||||
12
AzureDataModel/MappingProfile.cs
Normal file
12
AzureDataModel/MappingProfile.cs
Normal file
@@ -0,0 +1,12 @@
|
||||
using AutoMapper;
|
||||
using AzureDataModel.Dtos;
|
||||
|
||||
namespace AzureDataModel;
|
||||
|
||||
public class MappingProfile : Profile
|
||||
{
|
||||
public MappingProfile()
|
||||
{
|
||||
CreateMap<TaskScheduler, TaskSchedulerDto>().ReverseMap();
|
||||
}
|
||||
}
|
||||
8
AzureDataModel/Services/ITaskSchedulerService.cs
Normal file
8
AzureDataModel/Services/ITaskSchedulerService.cs
Normal file
@@ -0,0 +1,8 @@
|
||||
using AzureDataModel.Dtos;
|
||||
|
||||
namespace AzureDataModel.Services;
|
||||
|
||||
public interface ITaskSchedulerService
|
||||
{
|
||||
Task<IEnumerable<TaskSchedulerDto>> GetTaskSchedulers();
|
||||
}
|
||||
53
AzureDataModel/Services/TaskSchedulerService.cs
Normal file
53
AzureDataModel/Services/TaskSchedulerService.cs
Normal file
@@ -0,0 +1,53 @@
|
||||
using AutoMapper;
|
||||
using AzureDataModel.Dtos;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using TaskScheduler = AzureDataModel.Entities.TaskScheduler;
|
||||
|
||||
namespace AzureDataModel.Services;
|
||||
|
||||
public class TaskSchedulerService(AzureDbContext context, IMapper mapper) : ITaskSchedulerService
|
||||
{
|
||||
public async Task<IEnumerable<TaskSchedulerDto>> GetTaskSchedulers()
|
||||
{
|
||||
return await context.TaskSchedulers.Select(x => mapper.Map<TaskSchedulerDto>(x)).ToListAsync();
|
||||
}
|
||||
|
||||
public async Task<TaskSchedulerDto?> GetTaskSchedulerById(Guid id)
|
||||
{
|
||||
TaskSchedulerDto? taskScheduler = await context.TaskSchedulers.Where(x => x.RowPointer == id)
|
||||
.Select(x => mapper.Map<TaskSchedulerDto>(x)).FirstOrDefaultAsync();
|
||||
return taskScheduler;
|
||||
}
|
||||
|
||||
public async Task<TaskSchedulerDto?> GetTaskSchedulerByTaskName(string taskName)
|
||||
{
|
||||
TaskSchedulerDto? taskScheduler = await context.TaskSchedulers.Where(x => x.Name == taskName)
|
||||
.Select(x => mapper.Map<TaskSchedulerDto>(x)).FirstOrDefaultAsync();
|
||||
return taskScheduler;
|
||||
}
|
||||
|
||||
public async Task<int> AddTaskScheduler(TaskSchedulerDto taskSchedulerDto)
|
||||
{
|
||||
TaskScheduler taskScheduler = mapper.Map<TaskScheduler>(taskSchedulerDto);
|
||||
context.TaskSchedulers.Add(taskScheduler);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> UpdateTaskScheduler(TaskSchedulerDto taskSchedulerDto)
|
||||
{
|
||||
TaskScheduler taskScheduler = mapper.Map<TaskScheduler>(taskSchedulerDto);
|
||||
context.TaskSchedulers.Update(taskScheduler);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
|
||||
public async Task<int> DeleteTaskScheduler(Guid id)
|
||||
{
|
||||
TaskScheduler? taskScheduler =
|
||||
await context.TaskSchedulers.Where(x => x.RowPointer == id).FirstOrDefaultAsync() ?? null;
|
||||
|
||||
if (taskScheduler == null) return 0;
|
||||
|
||||
context.TaskSchedulers.Remove(taskScheduler);
|
||||
return await context.SaveChangesAsync();
|
||||
}
|
||||
}
|
||||
5
AzureDataModel/appsettings_Azure.json
Normal file
5
AzureDataModel/appsettings_Azure.json
Normal file
@@ -0,0 +1,5 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"AzureConnection": "Server=tcp:fakrosno-hangfire.database.windows.net,1433;Initial Catalog=FaKrosnoHangfire;Persist Security Info=False;User ID=fakrosno_hangfire@trentblaugranagmail.onmicrosoft.com;Password=juhrot-zigQuz-6jydpu;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication=\"Active Directory Password\";"
|
||||
}
|
||||
}
|
||||
13
FA_WEB.sln
13
FA_WEB.sln
@@ -13,6 +13,10 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "FaKrosnoEfDataModel", "FaKr
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "SytelineSaAppEfDataModel", "SytelineSaAppEfDataModel\SytelineSaAppEfDataModel.csproj", "{02B7F642-62C1-4BA2-87B0-0B9214319095}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "HangfireApi", "HangfireApi\HangfireApi.csproj", "{52106FF2-E714-4FFB-B383-38B1E782B352}"
|
||||
EndProject
|
||||
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "AzureDataModel", "AzureDataModel\AzureDataModel.csproj", "{5971D564-5108-4777-8273-DDE90BF39879}"
|
||||
EndProject
|
||||
Global
|
||||
GlobalSection(SolutionConfigurationPlatforms) = preSolution
|
||||
Debug|Any CPU = Debug|Any CPU
|
||||
@@ -35,6 +39,14 @@ Global
|
||||
{02B7F642-62C1-4BA2-87B0-0B9214319095}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{02B7F642-62C1-4BA2-87B0-0B9214319095}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{02B7F642-62C1-4BA2-87B0-0B9214319095}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{52106FF2-E714-4FFB-B383-38B1E782B352}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{52106FF2-E714-4FFB-B383-38B1E782B352}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{52106FF2-E714-4FFB-B383-38B1E782B352}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{52106FF2-E714-4FFB-B383-38B1E782B352}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
{5971D564-5108-4777-8273-DDE90BF39879}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
|
||||
{5971D564-5108-4777-8273-DDE90BF39879}.Debug|Any CPU.Build.0 = Debug|Any CPU
|
||||
{5971D564-5108-4777-8273-DDE90BF39879}.Release|Any CPU.ActiveCfg = Release|Any CPU
|
||||
{5971D564-5108-4777-8273-DDE90BF39879}.Release|Any CPU.Build.0 = Release|Any CPU
|
||||
EndGlobalSection
|
||||
GlobalSection(SolutionProperties) = preSolution
|
||||
HideSolutionNode = FALSE
|
||||
@@ -42,5 +54,6 @@ Global
|
||||
GlobalSection(NestedProjects) = preSolution
|
||||
{4671E3B6-4D11-4521-A927-DC3B1072FC4A} = {6607ACC9-71B9-4095-9A81-B6FBB0AAAF17}
|
||||
{02B7F642-62C1-4BA2-87B0-0B9214319095} = {6607ACC9-71B9-4095-9A81-B6FBB0AAAF17}
|
||||
{5971D564-5108-4777-8273-DDE90BF39879} = {6607ACC9-71B9-4095-9A81-B6FBB0AAAF17}
|
||||
EndGlobalSection
|
||||
EndGlobal
|
||||
|
||||
90
HangfireApi/Controllers/HangfireJobsController.cs
Normal file
90
HangfireApi/Controllers/HangfireJobsController.cs
Normal file
@@ -0,0 +1,90 @@
|
||||
using System.Diagnostics;
|
||||
using AzureDataModel.Dtos;
|
||||
using AzureDataModel.Services;
|
||||
using Hangfire;
|
||||
using Hangfire.Storage;
|
||||
using HangfireApi.Dtos;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
namespace HangfireApi.Controllers;
|
||||
|
||||
[ApiController]
|
||||
[Route("api/[controller]")]
|
||||
public class HangfireJobsController(JobStorage jobStorage, IRecurringJobManager recurringJobManager, ITaskSchedulerService service) : ControllerBase
|
||||
{
|
||||
[HttpGet("GetJobsToRun")]
|
||||
public async Task<ActionResult<IEnumerable<JobDto>>> GetJobsToRun()
|
||||
{
|
||||
IList<JobDto> jobsToRun = new List<JobDto>();
|
||||
|
||||
using (IStorageConnection? connection = jobStorage.GetConnection())
|
||||
{
|
||||
IList<RecurringJobDto>? recurringJobs = connection.GetRecurringJobs();
|
||||
IList<TaskSchedulerDto>? taskSchedulers = (await service.GetTaskSchedulers()).ToList();
|
||||
|
||||
foreach (var recurringJob in recurringJobs)
|
||||
{
|
||||
TaskSchedulerDto? taskScheduler = taskSchedulers?.FirstOrDefault(ts => ts.Name == recurringJob.Id);
|
||||
|
||||
if (taskScheduler != null)
|
||||
{
|
||||
jobsToRun.Add(new JobDto(recurringJob.Id, recurringJob.Cron, taskScheduler.Path,
|
||||
recurringJob.LastExecution, recurringJob.NextExecution, recurringJob.Job));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return Ok(jobsToRun);
|
||||
}
|
||||
|
||||
[HttpPost("RunJobs")]
|
||||
public async Task<IActionResult> RunJobs()
|
||||
{
|
||||
var jobsToRun = (await GetJobsToRun()).Value?.ToList();
|
||||
|
||||
if (jobsToRun == null || jobsToRun.Count == 0)
|
||||
{
|
||||
return BadRequest("Nie udało się pobrać zadań do uruchomienia.");
|
||||
}
|
||||
|
||||
foreach (var job in jobsToRun)
|
||||
{
|
||||
if (!string.IsNullOrEmpty(job.Path))
|
||||
{
|
||||
recurringJobManager.AddOrUpdate(job.JobId, () => RunConsoleApplication(job.Path), job.Cron,
|
||||
new RecurringJobOptions { TimeZone = TimeZoneInfo.Local });
|
||||
}
|
||||
}
|
||||
|
||||
return Ok("Zadania zostały zaplanowane do uruchamiania zgodnie z ich CRON.");
|
||||
}
|
||||
|
||||
private void RunConsoleApplication(string pathToApp)
|
||||
{
|
||||
try
|
||||
{
|
||||
var process = new Process
|
||||
{
|
||||
StartInfo = new ProcessStartInfo
|
||||
{
|
||||
FileName = pathToApp,
|
||||
UseShellExecute = false,
|
||||
RedirectStandardOutput = true,
|
||||
RedirectStandardError = true,
|
||||
CreateNoWindow = true
|
||||
}
|
||||
};
|
||||
process.Start();
|
||||
string output = process.StandardOutput.ReadToEnd();
|
||||
string error = process.StandardError.ReadToEnd();
|
||||
process.WaitForExit();
|
||||
|
||||
Console.WriteLine($"Output: {output}");
|
||||
Console.WriteLine($"Error: {error}");
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
Console.WriteLine($"Error executing console application: {ex.Message}");
|
||||
}
|
||||
}
|
||||
}
|
||||
19
HangfireApi/Dtos/JobDto.cs
Normal file
19
HangfireApi/Dtos/JobDto.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using Hangfire.Common;
|
||||
|
||||
namespace HangfireApi.Dtos;
|
||||
|
||||
public class JobDto(
|
||||
string jobId,
|
||||
string cron,
|
||||
string path,
|
||||
DateTime? lastExecution,
|
||||
DateTime? nextExecution,
|
||||
Job jobDetails)
|
||||
{
|
||||
public string JobId { get; set; } = jobId;
|
||||
public string Cron { get; set; } = cron;
|
||||
public string Path { get; set; } = path;
|
||||
public DateTime? LastExecution { get; set; } = lastExecution;
|
||||
public DateTime? NextExecution { get; set; } = nextExecution;
|
||||
public Job JobDetails { get; set; } = jobDetails;
|
||||
}
|
||||
20
HangfireApi/HangfireApi.csproj
Normal file
20
HangfireApi/HangfireApi.csproj
Normal file
@@ -0,0 +1,20 @@
|
||||
<Project Sdk="Microsoft.NET.Sdk.Web">
|
||||
|
||||
<PropertyGroup>
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="Hangfire" Version="1.8.17" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="8.0.12"/>
|
||||
<PackageReference Include="Microsoft.Data.SqlClient" Version="6.0.1" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.6.2"/>
|
||||
</ItemGroup>
|
||||
|
||||
<ItemGroup>
|
||||
<ProjectReference Include="..\AzureDataModel\AzureDataModel.csproj" />
|
||||
</ItemGroup>
|
||||
|
||||
</Project>
|
||||
6
HangfireApi/HangfireApi.http
Normal file
6
HangfireApi/HangfireApi.http
Normal file
@@ -0,0 +1,6 @@
|
||||
@HangfireApi_HostAddress = http://localhost:5237
|
||||
|
||||
GET {{HangfireApi_HostAddress}}/weatherforecast/
|
||||
Accept: application/json
|
||||
|
||||
###
|
||||
43
HangfireApi/Program.cs
Normal file
43
HangfireApi/Program.cs
Normal file
@@ -0,0 +1,43 @@
|
||||
using Hangfire;
|
||||
using Hangfire.SqlServer;
|
||||
using HangfireApi.Controllers;
|
||||
|
||||
var builder = WebApplication.CreateBuilder(args);
|
||||
|
||||
builder.Services.AddHangfire(configuration => configuration
|
||||
.SetDataCompatibilityLevel(CompatibilityLevel.Version_170)
|
||||
.UseSimpleAssemblyNameTypeSerializer()
|
||||
.UseRecommendedSerializerSettings()
|
||||
.UseSqlServerStorage(builder.Configuration.GetConnectionString("HangfireConnection"), new SqlServerStorageOptions
|
||||
{
|
||||
CommandBatchMaxTimeout = TimeSpan.FromMinutes(5),
|
||||
SlidingInvisibilityTimeout = TimeSpan.FromMinutes(5),
|
||||
QueuePollInterval = TimeSpan.Zero,
|
||||
UseRecommendedIsolationLevel = true,
|
||||
DisableGlobalLocks = true
|
||||
}));
|
||||
|
||||
builder.Services.AddControllers();
|
||||
builder.Services.AddHangfireServer();
|
||||
builder.Services.AddAuthorization();
|
||||
|
||||
builder.Services.AddSingleton<JobStorage>(provider =>
|
||||
provider.GetRequiredService<IServiceProvider>().GetService(typeof(JobStorage)) as JobStorage ??
|
||||
throw new InvalidOperationException());
|
||||
|
||||
var app = builder.Build();
|
||||
|
||||
if (app.Environment.IsDevelopment())
|
||||
{
|
||||
app.UseDeveloperExceptionPage();
|
||||
}
|
||||
|
||||
app.UseRouting();
|
||||
app.UseAuthorization();
|
||||
|
||||
app.MapControllers();
|
||||
app.MapHangfireDashboard();
|
||||
|
||||
await ((IApplicationBuilder)app).ApplicationServices.GetRequiredService<HangfireJobsController>().RunJobs();
|
||||
|
||||
await app.RunAsync();
|
||||
41
HangfireApi/Properties/launchSettings.json
Normal file
41
HangfireApi/Properties/launchSettings.json
Normal file
@@ -0,0 +1,41 @@
|
||||
{
|
||||
"$schema": "http://json.schemastore.org/launchsettings.json",
|
||||
"iisSettings": {
|
||||
"windowsAuthentication": false,
|
||||
"anonymousAuthentication": true,
|
||||
"iisExpress": {
|
||||
"applicationUrl": "http://localhost:24492",
|
||||
"sslPort": 44367
|
||||
}
|
||||
},
|
||||
"profiles": {
|
||||
"http": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "http://localhost:5237",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"https": {
|
||||
"commandName": "Project",
|
||||
"dotnetRunMessages": true,
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"applicationUrl": "https://localhost:7273;http://localhost:5237",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
},
|
||||
"IIS Express": {
|
||||
"commandName": "IISExpress",
|
||||
"launchBrowser": true,
|
||||
"launchUrl": "swagger",
|
||||
"environmentVariables": {
|
||||
"ASPNETCORE_ENVIRONMENT": "Development"
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
12
HangfireApi/appsettings.json
Normal file
12
HangfireApi/appsettings.json
Normal file
@@ -0,0 +1,12 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"HangfireConnection": "Server=tcp:fakrosno-hangfire.database.windows.net,1433;Initial Catalog=FaKrosnoHangfire;Persist Security Info=False;User ID=fakrosno_hangfire@trentblaugranagmail.onmicrosoft.com;Password=juhrot-zigQuz-6jydpu;MultipleActiveResultSets=False;Encrypt=True;TrustServerCertificate=False;Authentication=\"Active Directory Password\";"
|
||||
},
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Information",
|
||||
"Microsoft.AspNetCore": "Warning"
|
||||
}
|
||||
},
|
||||
"AllowedHosts": "*"
|
||||
}
|
||||
Reference in New Issue
Block a user