adding ILongRunningJobProgressService and its implementation
This commit is contained in:
parent
e95052666e
commit
a8e2b06e70
@ -3,6 +3,7 @@ using Microsoft.AspNetCore.Identity.EntityFrameworkCore;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RSecurityBackend.Models.Audit.Db;
|
||||
using RSecurityBackend.Models.Auth.Db;
|
||||
using RSecurityBackend.Models.Generic.Db;
|
||||
using RSecurityBackend.Models.Image;
|
||||
using System;
|
||||
|
||||
@ -82,5 +83,12 @@ namespace RSecurityBackend.DbContext
|
||||
/// </summary>
|
||||
public DbSet<REvent> AuditLogs { get; set; }
|
||||
#endregion
|
||||
|
||||
#region
|
||||
/// <summary>
|
||||
/// long running jobs
|
||||
/// </summary>
|
||||
public DbSet<RLongRunningJobStatus> LongRunningJobs { get; set; }
|
||||
#endregion
|
||||
}
|
||||
}
|
||||
|
||||
@ -44,6 +44,6 @@ namespace RSecurityBackend.Models.Generic.Db
|
||||
/// <summary>
|
||||
/// finished
|
||||
/// </summary>
|
||||
public bool Finished { get; set; }
|
||||
public bool Succeeded { get; set; }
|
||||
}
|
||||
}
|
||||
|
||||
@ -546,6 +546,11 @@
|
||||
Audit Logs Events
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:RSecurityBackend.DbContext.RSecurityDbContext`3.LongRunningJobs">
|
||||
<summary>
|
||||
long running jobs
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:RSecurityBackend.Models.Audit.Db.REvent">
|
||||
<summary>
|
||||
Audit Event
|
||||
@ -1669,7 +1674,7 @@
|
||||
Exception
|
||||
</summary>
|
||||
</member>
|
||||
<member name="P:RSecurityBackend.Models.Generic.Db.RLongRunningJobStatus.Finished">
|
||||
<member name="P:RSecurityBackend.Models.Generic.Db.RLongRunningJobStatus.Succeeded">
|
||||
<summary>
|
||||
finished
|
||||
</summary>
|
||||
@ -2191,6 +2196,52 @@
|
||||
Image Storage Path
|
||||
</summary>
|
||||
</member>
|
||||
<member name="T:RSecurityBackend.Services.ILongRunningJobProgressService">
|
||||
<summary>
|
||||
Long Running Job Progress Service
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:RSecurityBackend.Services.ILongRunningJobProgressService.NewJob(System.String,System.String)">
|
||||
<summary>
|
||||
new job
|
||||
</summary>
|
||||
<param name="name"></param>
|
||||
<param name="step"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RSecurityBackend.Services.ILongRunningJobProgressService.GetJob(System.Guid)">
|
||||
<summary>
|
||||
Get Job By Id
|
||||
</summary>
|
||||
<param name="id"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RSecurityBackend.Services.ILongRunningJobProgressService.UpdateJob(System.Guid,System.Double,System.String,System.Boolean,System.String)">
|
||||
<summary>
|
||||
update job
|
||||
</summary>
|
||||
<param name="id"></param>
|
||||
<param name="progress"></param>
|
||||
<param name="step"></param>
|
||||
<param name="succeeded"></param>
|
||||
<param name="exception"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RSecurityBackend.Services.ILongRunningJobProgressService.DeleteJob(System.Guid)">
|
||||
<summary>
|
||||
delete job
|
||||
</summary>
|
||||
<param name="id"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RSecurityBackend.Services.ILongRunningJobProgressService.CleanUp(System.Boolean,System.Boolean)">
|
||||
<summary>
|
||||
clean up finished jobs
|
||||
</summary>
|
||||
<param name="succeededJobs"></param>
|
||||
<param name="failedJobs"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="T:RSecurityBackend.Services.Implementation.AppUserService">
|
||||
<summary>
|
||||
Authentication Service
|
||||
@ -2662,6 +2713,63 @@
|
||||
<param name="context"></param>
|
||||
<param name="configuration"></param>
|
||||
</member>
|
||||
<member name="T:RSecurityBackend.Services.Implementation.LongRunningJobProgressServiceEF">
|
||||
<summary>
|
||||
Long Running Job Progress Service
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:RSecurityBackend.Services.Implementation.LongRunningJobProgressServiceEF.NewJob(System.String,System.String)">
|
||||
<summary>
|
||||
new job
|
||||
</summary>
|
||||
<param name="name"></param>
|
||||
<param name="step"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RSecurityBackend.Services.Implementation.LongRunningJobProgressServiceEF.GetJob(System.Guid)">
|
||||
<summary>
|
||||
Get Job By Id
|
||||
</summary>
|
||||
<param name="id"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RSecurityBackend.Services.Implementation.LongRunningJobProgressServiceEF.UpdateJob(System.Guid,System.Double,System.String,System.Boolean,System.String)">
|
||||
<summary>
|
||||
update job
|
||||
</summary>
|
||||
<param name="id"></param>
|
||||
<param name="progress"></param>
|
||||
<param name="step"></param>
|
||||
<param name="succeeded"></param>
|
||||
<param name="exception"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RSecurityBackend.Services.Implementation.LongRunningJobProgressServiceEF.DeleteJob(System.Guid)">
|
||||
<summary>
|
||||
delete job
|
||||
</summary>
|
||||
<param name="id"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="M:RSecurityBackend.Services.Implementation.LongRunningJobProgressServiceEF.CleanUp(System.Boolean,System.Boolean)">
|
||||
<summary>
|
||||
clean up finished jobs
|
||||
</summary>
|
||||
<param name="succeededJobs"></param>
|
||||
<param name="failedJobs"></param>
|
||||
<returns></returns>
|
||||
</member>
|
||||
<member name="F:RSecurityBackend.Services.Implementation.LongRunningJobProgressServiceEF._context">
|
||||
<summary>
|
||||
Database Contetxt
|
||||
</summary>
|
||||
</member>
|
||||
<member name="M:RSecurityBackend.Services.Implementation.LongRunningJobProgressServiceEF.#ctor(RSecurityBackend.DbContext.RSecurityDbContext{RSecurityBackend.Models.Auth.Db.RAppUser,RSecurityBackend.Models.Auth.Db.RAppRole,System.Guid})">
|
||||
<summary>
|
||||
constructor
|
||||
</summary>
|
||||
<param name="context"></param>
|
||||
</member>
|
||||
<member name="T:RSecurityBackend.Services.Implementation.MailKitEmailSender">
|
||||
<summary>
|
||||
mail sender using MailKit
|
||||
|
||||
54
RSecurityBackend/Services/ILongRunningJobProgressService.cs
Normal file
54
RSecurityBackend/Services/ILongRunningJobProgressService.cs
Normal file
@ -0,0 +1,54 @@
|
||||
using RSecurityBackend.Models.Generic;
|
||||
using RSecurityBackend.Models.Generic.Db;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RSecurityBackend.Services
|
||||
{
|
||||
/// <summary>
|
||||
/// Long Running Job Progress Service
|
||||
/// </summary>
|
||||
public interface ILongRunningJobProgressService
|
||||
{
|
||||
/// <summary>
|
||||
/// new job
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="step"></param>
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<RLongRunningJobStatus>> NewJob(string name, string step);
|
||||
|
||||
/// <summary>
|
||||
/// Get Job By Id
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<RLongRunningJobStatus>> GetJob(Guid id);
|
||||
|
||||
/// <summary>
|
||||
/// update job
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="progress"></param>
|
||||
/// <param name="step"></param>
|
||||
/// <param name="succeeded"></param>
|
||||
/// <param name="exception"></param>
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<RLongRunningJobStatus>> UpdateJob(Guid id, double progress, string step = "", bool succeeded = false, string exception = "");
|
||||
|
||||
/// <summary>
|
||||
/// delete job
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<bool>> DeleteJob(Guid id);
|
||||
|
||||
/// <summary>
|
||||
/// clean up finished jobs
|
||||
/// </summary>
|
||||
/// <param name="succeededJobs"></param>
|
||||
/// <param name="failedJobs"></param>
|
||||
/// <returns></returns>
|
||||
Task<RServiceResult<bool>> CleanUp(bool succeededJobs, bool failedJobs);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,163 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using RSecurityBackend.DbContext;
|
||||
using RSecurityBackend.Models.Auth.Db;
|
||||
using RSecurityBackend.Models.Generic;
|
||||
using RSecurityBackend.Models.Generic.Db;
|
||||
using System;
|
||||
using System.Linq;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace RSecurityBackend.Services.Implementation
|
||||
{
|
||||
/// <summary>
|
||||
/// Long Running Job Progress Service
|
||||
/// </summary>
|
||||
public class LongRunningJobProgressServiceEF
|
||||
{
|
||||
/// <summary>
|
||||
/// new job
|
||||
/// </summary>
|
||||
/// <param name="name"></param>
|
||||
/// <param name="step"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<RServiceResult<RLongRunningJobStatus>> NewJob(string name, string step)
|
||||
{
|
||||
try
|
||||
{
|
||||
RLongRunningJobStatus job = new RLongRunningJobStatus()
|
||||
{
|
||||
Name = name,
|
||||
Step = step,
|
||||
Progress = 0,
|
||||
StartTime = DateTime.Now,
|
||||
Succeeded = false,
|
||||
Exception = ""
|
||||
};
|
||||
_context.LongRunningJobs.Add(job);
|
||||
await _context.SaveChangesAsync();
|
||||
return new RServiceResult<RLongRunningJobStatus>(job);
|
||||
}
|
||||
catch(Exception exp)
|
||||
{
|
||||
return new RServiceResult<RLongRunningJobStatus>(null, exp.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Get Job By Id
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<RServiceResult<RLongRunningJobStatus>> GetJob(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
return new RServiceResult<RLongRunningJobStatus>(await _context.LongRunningJobs.Where(j => j.Id == id).SingleOrDefaultAsync());
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
return new RServiceResult<RLongRunningJobStatus>(null, exp.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// update job
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <param name="progress"></param>
|
||||
/// <param name="step"></param>
|
||||
/// <param name="succeeded"></param>
|
||||
/// <param name="exception"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<RServiceResult<RLongRunningJobStatus>> UpdateJob(Guid id, double progress, string step = "", bool succeeded = false, string exception = "")
|
||||
{
|
||||
try
|
||||
{
|
||||
RLongRunningJobStatus job = await _context.LongRunningJobs.Where(j => j.Id == id).SingleOrDefaultAsync();
|
||||
|
||||
if(!string.IsNullOrEmpty(step))
|
||||
{
|
||||
job.Step = step;
|
||||
}
|
||||
|
||||
if(succeeded || !string.IsNullOrEmpty(exception))
|
||||
{
|
||||
job.EndTime = DateTime.Now;
|
||||
}
|
||||
|
||||
job.Progress = progress;
|
||||
job.Succeeded = succeeded;
|
||||
job.Exception = exception;
|
||||
|
||||
_context.Update(job);
|
||||
|
||||
await _context.SaveChangesAsync();
|
||||
|
||||
return new RServiceResult<RLongRunningJobStatus>(job);
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
return new RServiceResult<RLongRunningJobStatus>(null, exp.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// delete job
|
||||
/// </summary>
|
||||
/// <param name="id"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<RServiceResult<bool>> DeleteJob(Guid id)
|
||||
{
|
||||
try
|
||||
{
|
||||
RLongRunningJobStatus job = await _context.LongRunningJobs.Where(j => j.Id == id).SingleOrDefaultAsync();
|
||||
_context.LongRunningJobs.Remove(job);
|
||||
await _context.SaveChangesAsync();
|
||||
return new RServiceResult<bool>(true);
|
||||
}
|
||||
catch(Exception exp)
|
||||
{
|
||||
return new RServiceResult<bool>(false, exp.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// clean up finished jobs
|
||||
/// </summary>
|
||||
/// <param name="succeededJobs"></param>
|
||||
/// <param name="failedJobs"></param>
|
||||
/// <returns></returns>
|
||||
public async Task<RServiceResult<bool>> CleanUp(bool succeededJobs, bool failedJobs)
|
||||
{
|
||||
try
|
||||
{
|
||||
var jobs = await _context.LongRunningJobs.Where(j => (succeededJobs && j.Succeeded == true) || (failedJobs && j.Exception != "")).ToListAsync();
|
||||
if(jobs.Count > 0)
|
||||
{
|
||||
_context.LongRunningJobs.RemoveRange(jobs);
|
||||
await _context.SaveChangesAsync();
|
||||
}
|
||||
return new RServiceResult<bool>(true);
|
||||
}
|
||||
catch (Exception exp)
|
||||
{
|
||||
return new RServiceResult<bool>(false, exp.ToString());
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Database Contetxt
|
||||
/// </summary>
|
||||
protected readonly RSecurityDbContext<RAppUser, RAppRole, Guid> _context;
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// constructor
|
||||
/// </summary>
|
||||
/// <param name="context"></param>
|
||||
public LongRunningJobProgressServiceEF(RSecurityDbContext<RAppUser, RAppRole, Guid> context)
|
||||
{
|
||||
_context = context;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user