using Microsoft.EntityFrameworkCore;
using RSecurityBackend.DbContext;
using RSecurityBackend.Models.Auth.Db;
using RSecurityBackend.Models.Generic;
using RSecurityBackend.Models.Generic.Db;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace RSecurityBackend.Services.Implementation
{
///
/// Long Running Job Progress Service
///
public class LongRunningJobProgressServiceEF : ILongRunningJobProgressService
{
///
/// new job
///
///
///
///
public async Task> 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(job);
}
catch(Exception exp)
{
return new RServiceResult(null, exp.ToString());
}
}
///
/// Get Jobs
///
///
///
///
public async Task> GetJobs(bool succeeded = true, bool failed = true)
{
try
{
var jobs = await _context.LongRunningJobs.Where(j => (succeeded && j.Succeeded == true) || (failed && j.Exception != "")).OrderByDescending(j => j.StartTime).ToArrayAsync();
return new RServiceResult(jobs);
}
catch (Exception exp)
{
return new RServiceResult (null, exp.ToString());
}
}
///
/// Get Job By Id
///
///
///
public async Task> GetJob(Guid id)
{
try
{
return new RServiceResult(await _context.LongRunningJobs.Where(j => j.Id == id).SingleOrDefaultAsync());
}
catch (Exception exp)
{
return new RServiceResult(null, exp.ToString());
}
}
///
/// update job
///
///
///
///
///
///
///
public async Task> 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(job);
}
catch (Exception exp)
{
return new RServiceResult(null, exp.ToString());
}
}
///
/// delete job
///
///
///
public async Task> 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(true);
}
catch(Exception exp)
{
return new RServiceResult(false, exp.ToString());
}
}
///
/// clean up finished jobs
///
///
///
///
public async Task> 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(true);
}
catch (Exception exp)
{
return new RServiceResult(false, exp.ToString());
}
}
///
/// Database Contetxt
///
protected readonly RSecurityDbContext _context;
///
/// constructor
///
///
public LongRunningJobProgressServiceEF(RSecurityDbContext context)
{
_context = context;
}
}
}