mirror of
https://github.com/Kareadita/Kavita.git
synced 2025-05-24 00:52:23 -04:00
Added Hangfire with LiteDB for a task running system. At the most basic, this allows us to monitor tasks running on the system (during dev only) and run tasks on a reoccuring or ad-hoc basis.
This commit is contained in:
parent
e1c1719b6a
commit
4fd9943b91
@ -8,6 +8,9 @@
|
||||
|
||||
<ItemGroup>
|
||||
<PackageReference Include="AutoMapper.Extensions.Microsoft.DependencyInjection" Version="8.1.0" />
|
||||
<PackageReference Include="Hangfire" Version="1.7.18" />
|
||||
<PackageReference Include="Hangfire.AspNetCore" Version="1.7.18" />
|
||||
<PackageReference Include="Hangfire.LiteDB" Version="0.4.0" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="5.0.1" NoWarn="NU1605" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Authentication.OpenIdConnect" Version="5.0.1" NoWarn="NU1605" />
|
||||
<PackageReference Include="Microsoft.AspNetCore.Identity.EntityFrameworkCore" Version="5.0.1" />
|
||||
|
@ -49,8 +49,6 @@ namespace API.Controllers
|
||||
|
||||
if (!result.Succeeded) return BadRequest(result.Errors);
|
||||
|
||||
|
||||
// TODO: Need a way to store Roles in enum and configure from there
|
||||
var role = registerDto.IsAdmin ? PolicyConstants.AdminRole : PolicyConstants.PlebRole;
|
||||
var roleResult = await _userManager.AddToRoleAsync(user, role);
|
||||
|
||||
|
@ -1,9 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using API.DTOs;
|
||||
using System.Threading.Tasks;
|
||||
using API.Entities;
|
||||
using API.Interfaces;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
|
||||
|
@ -1,14 +1,13 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Immutable;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
using API.Data;
|
||||
using API.DTOs;
|
||||
using API.Entities;
|
||||
using API.Extensions;
|
||||
using API.Interfaces;
|
||||
using AutoMapper;
|
||||
using Hangfire;
|
||||
using Microsoft.AspNetCore.Authorization;
|
||||
using Microsoft.AspNetCore.Mvc;
|
||||
using Microsoft.Extensions.Logging;
|
||||
@ -18,23 +17,23 @@ namespace API.Controllers
|
||||
[Authorize]
|
||||
public class LibraryController : BaseApiController
|
||||
{
|
||||
private readonly DataContext _context;
|
||||
private readonly IDirectoryService _directoryService;
|
||||
private readonly ILibraryRepository _libraryRepository;
|
||||
private readonly ILogger<LibraryController> _logger;
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly IMapper _mapper;
|
||||
private readonly ITaskScheduler _taskScheduler;
|
||||
|
||||
public LibraryController(DataContext context, IDirectoryService directoryService,
|
||||
public LibraryController(IDirectoryService directoryService,
|
||||
ILibraryRepository libraryRepository, ILogger<LibraryController> logger, IUserRepository userRepository,
|
||||
IMapper mapper)
|
||||
IMapper mapper, ITaskScheduler taskScheduler)
|
||||
{
|
||||
_context = context;
|
||||
_directoryService = directoryService;
|
||||
_libraryRepository = libraryRepository;
|
||||
_logger = logger;
|
||||
_userRepository = userRepository;
|
||||
_mapper = mapper;
|
||||
_taskScheduler = taskScheduler;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -90,8 +89,8 @@ namespace API.Controllers
|
||||
public async Task<ActionResult> ScanLibrary(int libraryId)
|
||||
{
|
||||
var library = await _libraryRepository.GetLibraryForIdAsync(libraryId);
|
||||
|
||||
_directoryService.ScanLibrary(library);
|
||||
|
||||
BackgroundJob.Enqueue(() => _directoryService.ScanLibrary(library));
|
||||
|
||||
return Ok();
|
||||
}
|
||||
|
@ -14,13 +14,11 @@ namespace API.Controllers
|
||||
[Authorize]
|
||||
public class UsersController : BaseApiController
|
||||
{
|
||||
private readonly DataContext _context;
|
||||
private readonly IUserRepository _userRepository;
|
||||
private readonly ILibraryRepository _libraryRepository;
|
||||
|
||||
public UsersController(DataContext context, IUserRepository userRepository, ILibraryRepository libraryRepository)
|
||||
public UsersController(IUserRepository userRepository, ILibraryRepository libraryRepository)
|
||||
{
|
||||
_context = context;
|
||||
_userRepository = userRepository;
|
||||
_libraryRepository = libraryRepository;
|
||||
}
|
||||
@ -43,7 +41,7 @@ namespace API.Controllers
|
||||
// TODO: We probably need to clean the folders before we insert
|
||||
var library = new Library
|
||||
{
|
||||
Name = createLibraryDto.Name, // TODO: Ensure code handles Library name always being lowercase
|
||||
Name = createLibraryDto.Name.ToLower(),
|
||||
Type = createLibraryDto.Type,
|
||||
AppUsers = new List<AppUser>() { user }
|
||||
};
|
||||
|
@ -1,8 +1,6 @@
|
||||
using System.Collections.Generic;
|
||||
using System.ComponentModel.DataAnnotations;
|
||||
using System.Text.Json.Serialization;
|
||||
using API.Entities;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion.Internal;
|
||||
|
||||
namespace API.DTOs
|
||||
{
|
||||
|
@ -1,7 +1,5 @@
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using API.Entities;
|
||||
|
||||
namespace API.DTOs
|
||||
{
|
||||
|
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using API.Entities;
|
||||
using API.Entities;
|
||||
using Microsoft.AspNetCore.Identity;
|
||||
using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
|
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
using Microsoft.EntityFrameworkCore.Migrations;
|
||||
|
||||
namespace API.Data.Migrations
|
||||
{
|
||||
|
@ -1,9 +1,7 @@
|
||||
// <auto-generated />
|
||||
using System;
|
||||
using API.Data;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore.Infrastructure;
|
||||
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
|
||||
|
||||
namespace API.Data.Migrations
|
||||
{
|
||||
|
@ -12,8 +12,8 @@ namespace API.Data
|
||||
{
|
||||
var roles = new List<AppRole>
|
||||
{
|
||||
new AppRole {Name = PolicyConstants.AdminRole},
|
||||
new AppRole {Name = PolicyConstants.PlebRole}
|
||||
new() {Name = PolicyConstants.AdminRole},
|
||||
new() {Name = PolicyConstants.PlebRole}
|
||||
};
|
||||
|
||||
foreach (var role in roles)
|
||||
|
@ -3,6 +3,8 @@ using API.Helpers;
|
||||
using API.Interfaces;
|
||||
using API.Services;
|
||||
using AutoMapper;
|
||||
using Hangfire;
|
||||
using Hangfire.LiteDB;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
@ -14,6 +16,7 @@ namespace API.Extensions
|
||||
public static IServiceCollection AddApplicationServices(this IServiceCollection services, IConfiguration config)
|
||||
{
|
||||
services.AddAutoMapper(typeof(AutoMapperProfiles).Assembly);
|
||||
services.AddScoped<ITaskScheduler, TaskScheduler>();
|
||||
services.AddScoped<IUserRepository, UserRepository>();
|
||||
services.AddScoped<ITokenService, TokenService>();
|
||||
services.AddScoped<IDirectoryService, DirectoryService>();
|
||||
@ -23,6 +26,14 @@ namespace API.Extensions
|
||||
options.UseSqlite(config.GetConnectionString("DefaultConnection"));
|
||||
});
|
||||
|
||||
services.AddHangfire(configuration => configuration
|
||||
.UseSimpleAssemblyNameTypeSerializer()
|
||||
.UseRecommendedSerializerSettings()
|
||||
.UseLiteDbStorage());
|
||||
|
||||
// Add the processing server as IHostedService
|
||||
services.AddHangfireServer();
|
||||
|
||||
return services;
|
||||
}
|
||||
}
|
||||
|
BIN
API/Hangfire-log.db
Normal file
BIN
API/Hangfire-log.db
Normal file
Binary file not shown.
BIN
API/Hangfire.db
Normal file
BIN
API/Hangfire.db
Normal file
Binary file not shown.
@ -1,5 +1,4 @@
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Linq;
|
||||
using API.DTOs;
|
||||
using API.Entities;
|
||||
using AutoMapper;
|
||||
|
@ -1,7 +1,5 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using API.DTOs;
|
||||
using API.Entities;
|
||||
|
||||
namespace API.Interfaces
|
||||
{
|
||||
|
7
API/Interfaces/ITaskScheduler.cs
Normal file
7
API/Interfaces/ITaskScheduler.cs
Normal file
@ -0,0 +1,7 @@
|
||||
namespace API.Interfaces
|
||||
{
|
||||
public interface ITaskScheduler
|
||||
{
|
||||
|
||||
}
|
||||
}
|
@ -1,5 +1,4 @@
|
||||
using System.Collections;
|
||||
using System.Collections.Generic;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using API.DTOs;
|
||||
using API.Entities;
|
||||
|
@ -29,7 +29,7 @@ namespace API.Services
|
||||
/// <returns>List of folder names</returns>
|
||||
public IEnumerable<string> ListDirectory(string rootPath)
|
||||
{
|
||||
// TODO: Put some checks in here along with API to ensure that we aren't passed a file, folder exists, etc.
|
||||
if (!Directory.Exists(rootPath)) return ImmutableList<string>.Empty;
|
||||
|
||||
var di = new DirectoryInfo(rootPath);
|
||||
var dirs = di.GetDirectories()
|
||||
@ -137,7 +137,7 @@ namespace API.Services
|
||||
else {
|
||||
Parallel.ForEach(files, () => 0, (file, loopState, localCount) =>
|
||||
{ action(file);
|
||||
return (int) ++localCount;
|
||||
return ++localCount;
|
||||
},
|
||||
(c) => {
|
||||
Interlocked.Add(ref fileCount, c);
|
||||
|
17
API/Services/TaskScheduler.cs
Normal file
17
API/Services/TaskScheduler.cs
Normal file
@ -0,0 +1,17 @@
|
||||
using API.Interfaces;
|
||||
using Hangfire;
|
||||
|
||||
namespace API.Services
|
||||
{
|
||||
public class TaskScheduler : ITaskScheduler
|
||||
{
|
||||
private readonly BackgroundJobServer _client;
|
||||
|
||||
public TaskScheduler()
|
||||
{
|
||||
_client = new BackgroundJobServer();
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
}
|
@ -1,5 +1,7 @@
|
||||
using System;
|
||||
using API.Extensions;
|
||||
using API.Middleware;
|
||||
using Hangfire;
|
||||
using Microsoft.AspNetCore.Builder;
|
||||
using Microsoft.AspNetCore.Hosting;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
@ -33,7 +35,7 @@ namespace API
|
||||
}
|
||||
|
||||
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
||||
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
|
||||
public void Configure(IApplicationBuilder app, IBackgroundJobClient backgroundJobs, IWebHostEnvironment env)
|
||||
{
|
||||
app.UseMiddleware<ExceptionMiddleware>();
|
||||
|
||||
@ -42,6 +44,9 @@ namespace API
|
||||
app.UseSwagger();
|
||||
app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "API v1"));
|
||||
}
|
||||
|
||||
app.UseHangfireDashboard();
|
||||
backgroundJobs.Enqueue(() => Console.WriteLine("Hello world from Hangfire!"));
|
||||
|
||||
app.UseHttpsRedirection();
|
||||
|
||||
@ -57,6 +62,7 @@ namespace API
|
||||
app.UseEndpoints(endpoints =>
|
||||
{
|
||||
endpoints.MapControllers();
|
||||
endpoints.MapHangfireDashboard();
|
||||
});
|
||||
}
|
||||
}
|
||||
|
@ -1,13 +1,15 @@
|
||||
{
|
||||
"ConnectionStrings": {
|
||||
"DefaultConnection": "Data source=kavita.db"
|
||||
"DefaultConnection": "Data source=kavita.db",
|
||||
"HangfireConnection": "Data source=hangfire.db"
|
||||
},
|
||||
"TokenKey": "super secret unguessable key",
|
||||
"Logging": {
|
||||
"LogLevel": {
|
||||
"Default": "Debug",
|
||||
"Microsoft": "Information",
|
||||
"Microsoft.Hosting.Lifetime": "Information"
|
||||
"Microsoft.Hosting.Lifetime": "Information",
|
||||
"Hangfire": "Information"
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user