* Created AzureDataModel
* Created Hangfire API
This commit is contained in:
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