MP.TaskMan:
- Completato aggiunta progetto - porting classi servizi/controlli/db a progetto - compilazione OK - manca gestione connString ottimizzata x il DB di origine CORRETTO (LAND/PROG/STATS)
This commit is contained in:
@@ -0,0 +1,438 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using Microsoft.Extensions.Configuration;
|
||||
using MP.TaskMan.Models;
|
||||
using NLog;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using static MP.TaskMan.Objects.Enums;
|
||||
|
||||
namespace MP.TaskMan.Controllers
|
||||
{
|
||||
public class MpTaskController: IDisposable
|
||||
{
|
||||
public MpTaskController(IConfiguration configuration)
|
||||
{
|
||||
_configuration = configuration;
|
||||
Log.Info("Avviato MpTaskController");
|
||||
}
|
||||
private static IConfiguration _configuration;
|
||||
|
||||
private static Logger Log = LogManager.GetCurrentClassLogger();
|
||||
public DateTime CalcNextExe(TaskListModel taskRec)
|
||||
{
|
||||
DateTime dtNext = DateTime.Today;
|
||||
try
|
||||
{
|
||||
// calcolo next exec da tipo...
|
||||
switch (taskRec.Freq)
|
||||
{
|
||||
case TaskFreqType.ND:
|
||||
dtNext = taskRec.DtLastExec.AddDays(taskRec.Cad);
|
||||
break;
|
||||
|
||||
case TaskFreqType.Sec:
|
||||
dtNext = taskRec.DtLastExec.AddSeconds(taskRec.Cad);
|
||||
break;
|
||||
|
||||
case TaskFreqType.Min:
|
||||
dtNext = taskRec.DtLastExec.AddMinutes(taskRec.Cad);
|
||||
break;
|
||||
|
||||
case TaskFreqType.Hour:
|
||||
dtNext = taskRec.DtLastExec.AddHours(taskRec.Cad);
|
||||
break;
|
||||
|
||||
case TaskFreqType.Day:
|
||||
dtNext = taskRec.DtLastExec.AddDays(taskRec.Cad);
|
||||
break;
|
||||
|
||||
case TaskFreqType.Week:
|
||||
dtNext = taskRec.DtLastExec.AddDays(7 * taskRec.Cad);
|
||||
break;
|
||||
|
||||
case TaskFreqType.Month:
|
||||
dtNext = taskRec.DtLastExec.AddMonths(taskRec.Cad);
|
||||
break;
|
||||
|
||||
case TaskFreqType.Year:
|
||||
dtNext = taskRec.DtLastExec.AddYears(taskRec.Cad);
|
||||
break;
|
||||
|
||||
default:
|
||||
dtNext = taskRec.DtLastExec.AddDays(taskRec.Cad);
|
||||
break;
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Error($"Eccezione in CalcNextExe{Environment.NewLine}{exc}");
|
||||
}
|
||||
return dtNext;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Elenco da tabella Config
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
public List<ConfigModel> ConfigGetAll()
|
||||
{
|
||||
List<ConfigModel> dbResult = new List<ConfigModel>();
|
||||
using (var dbCtx = new TaskContext(_configuration))
|
||||
{
|
||||
dbResult = dbCtx
|
||||
.DbSetConfig
|
||||
.AsNoTracking()
|
||||
.OrderBy(x => x.Chiave)
|
||||
.ToList();
|
||||
}
|
||||
return dbResult;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
_configuration = null;
|
||||
}
|
||||
/// <summary>
|
||||
/// Chiamata esecuzione di un singolo task programmato
|
||||
/// </summary>
|
||||
/// <param name="TaskId"></param>
|
||||
/// <param name="SchedNext">Se true rischedula successiva chiamata</param>
|
||||
/// <returns></returns>
|
||||
public TaskResultModel ExecuteSqlTask(int TaskId, bool SchedNext)
|
||||
{
|
||||
TaskResultModel callRes = new TaskResultModel();
|
||||
using (var dbCtx = new TaskContext(_configuration))
|
||||
{
|
||||
// imposto timeout a 5 min
|
||||
//var currTimeout = dbCtx.Database.GetCommandTimeout();
|
||||
dbCtx.Database.SetCommandTimeout(TimeSpan.FromMinutes(5));
|
||||
try
|
||||
{
|
||||
DateTime dtStart = DateTime.Now;
|
||||
// recupero i dati da richiamare...
|
||||
var currRec = dbCtx
|
||||
.DbSetTaskList
|
||||
.Where(x => x.TaskId == TaskId)
|
||||
.FirstOrDefault();
|
||||
if (currRec != null)
|
||||
{
|
||||
// recupero comando
|
||||
string sqlCommand = currRec.Command;
|
||||
string rawParams = currRec.Args;
|
||||
callRes = dbCtx
|
||||
.DbSetTaskResult
|
||||
.FromSqlRaw($"EXEC {sqlCommand} {rawParams}")
|
||||
.AsNoTracking()
|
||||
.AsEnumerable()
|
||||
.FirstOrDefault();
|
||||
DateTime dtEnd = DateTime.Now;
|
||||
|
||||
// preparo record esecuzione...
|
||||
TaskExecModel resRec = new TaskExecModel()
|
||||
{
|
||||
TaskId = TaskId,
|
||||
DtStart = dtStart,
|
||||
DtEnd = dtEnd,
|
||||
IsError = callRes.ExecResult < 0,
|
||||
Result = callRes.TextResult
|
||||
};
|
||||
dbCtx
|
||||
.DbSetTaskExe
|
||||
.Add(resRec);
|
||||
|
||||
// aggiorno record chiamata...
|
||||
currRec.DtLastExec = dtStart;
|
||||
currRec.LastResult = resRec.Result;
|
||||
currRec.LastIsError = resRec.IsError;
|
||||
currRec.LastDuration = dtEnd.Subtract(dtStart).TotalSeconds;
|
||||
// solo se richiesto rischedulazione ricalcola chiamata
|
||||
if (SchedNext)
|
||||
{
|
||||
// calcolo prossima esecuzione...
|
||||
currRec.DtNextExec = CalcNextExe(currRec);
|
||||
}
|
||||
// segno modificato
|
||||
dbCtx.Entry(currRec).State = EntityState.Modified;
|
||||
|
||||
// salvo modifiche!
|
||||
dbCtx.SaveChanges();
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Error($"Eccezione in ExecuteSqlCommand{Environment.NewLine}{exc}");
|
||||
}
|
||||
}
|
||||
return callRes;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Annulla modifiche su una specifica entity (cancel update)
|
||||
/// </summary>
|
||||
/// <param name="item"></param>
|
||||
/// <returns></returns>
|
||||
public bool RollBackEntity(object item)
|
||||
{
|
||||
bool answ = false;
|
||||
using (var dbCtx = new TaskContext(_configuration))
|
||||
{
|
||||
try
|
||||
{
|
||||
if (dbCtx.Entry(item).State == Microsoft.EntityFrameworkCore.EntityState.Deleted || dbCtx.Entry(item).State == Microsoft.EntityFrameworkCore.EntityState.Modified)
|
||||
{
|
||||
dbCtx.Entry(item).Reload();
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Error($"Eccezione in rollBackEntity{Environment.NewLine}{exc}");
|
||||
}
|
||||
}
|
||||
return answ;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ricerca task dato tipo + num max (desc)
|
||||
/// </summary>
|
||||
/// <param name="TaskId">TaskId da cui deriva</param>
|
||||
/// <returns></returns>
|
||||
public List<TaskExecModel> TaskExecGetFilt(int TaskId, int maxRec)
|
||||
{
|
||||
List<TaskExecModel> dbResult = new List<TaskExecModel>();
|
||||
using (var dbCtx = new TaskContext(_configuration))
|
||||
{
|
||||
dbResult = dbCtx
|
||||
.DbSetTaskExe
|
||||
.Include(x => x.TaskListNav)
|
||||
.Where(x => (x.TaskId == TaskId))
|
||||
.OrderByDescending(x => x.DtStart)
|
||||
.Take(maxRec)
|
||||
.ToList();
|
||||
}
|
||||
return dbResult;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Upsert record TaskExec
|
||||
/// </summary>
|
||||
/// <param name="rec2upd">Record da aggiornare/inserire</param>
|
||||
/// <returns></returns>
|
||||
public bool TaskExecUpsert(TaskExecModel rec2upd)
|
||||
{
|
||||
bool done = false;
|
||||
using (var dbCtx = new TaskContext(_configuration))
|
||||
{
|
||||
try
|
||||
{
|
||||
var currData = dbCtx
|
||||
.DbSetTaskExe
|
||||
.Where(x => x.TaskExecId == rec2upd.TaskExecId)
|
||||
.FirstOrDefault();
|
||||
if (currData != null)
|
||||
{
|
||||
currData.TaskId = rec2upd.TaskId;
|
||||
currData.DtStart = rec2upd.DtStart;
|
||||
currData.DtEnd = rec2upd.DtEnd;
|
||||
currData.IsError = rec2upd.IsError;
|
||||
currData.Result = rec2upd.Result;
|
||||
dbCtx.Entry(currData).State = EntityState.Modified;
|
||||
}
|
||||
else
|
||||
{
|
||||
dbCtx
|
||||
.DbSetTaskExe
|
||||
.Add(rec2upd);
|
||||
}
|
||||
dbCtx.SaveChanges();
|
||||
done = true;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Error($"Eccezione in TaskExecUpsert{Environment.NewLine}{exc}");
|
||||
}
|
||||
}
|
||||
return done;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Ricerca task dato tipo e
|
||||
/// </summary>
|
||||
/// <param name="TType"></param>
|
||||
/// <returns></returns>
|
||||
public List<TaskListModel> TaskListGetAll(Task2ExeType TType)
|
||||
{
|
||||
List<TaskListModel> dbResult = new List<TaskListModel>();
|
||||
using (var dbCtx = new TaskContext(_configuration))
|
||||
{
|
||||
dbResult = dbCtx
|
||||
.DbSetTaskList
|
||||
.Where(x => (TType == Task2ExeType.ND || x.TType == TType))
|
||||
.OrderBy(x => x.Ordinal)
|
||||
.ToList();
|
||||
}
|
||||
return dbResult;
|
||||
}
|
||||
|
||||
|
||||
/// <summary>
|
||||
/// Esegue registrazione di un Task generico (NON SQL)
|
||||
/// </summary>
|
||||
/// <param name="TaskId"></param>
|
||||
/// <param name="SchedNext">Se true rischedula successiva chiamata</param>
|
||||
/// <param name="ResRec">Record esecuzione task esterno</param>
|
||||
/// <returns></returns>
|
||||
public TaskResultModel TaskExecSaveExecuted(int TaskId, bool SchedNext, TaskExecModel ResRec)
|
||||
{
|
||||
TaskResultModel callRes = new TaskResultModel();
|
||||
using (var dbCtx = new TaskContext(_configuration))
|
||||
{
|
||||
try
|
||||
{
|
||||
// recupero i dati da richiamare...
|
||||
var currRec = dbCtx
|
||||
.DbSetTaskList
|
||||
.Where(x => x.TaskId == TaskId)
|
||||
.FirstOrDefault();
|
||||
if (currRec != null)
|
||||
{
|
||||
// registro task ricevuto
|
||||
dbCtx
|
||||
.DbSetTaskExe
|
||||
.Add(ResRec);
|
||||
|
||||
// aggiorno record chiamata...
|
||||
currRec.DtLastExec = ResRec.DtStart;
|
||||
currRec.LastResult = ResRec.Result;
|
||||
currRec.LastIsError = ResRec.IsError;
|
||||
currRec.LastDuration = ResRec.DtEnd.Subtract(ResRec.DtStart).TotalSeconds;
|
||||
// solo se richiesto rischedulazione ricalcola chiamata
|
||||
if (SchedNext)
|
||||
{
|
||||
// calcolo prossima esecuzione...
|
||||
currRec.DtNextExec = CalcNextExe(currRec);
|
||||
}
|
||||
// segno modificato
|
||||
dbCtx.Entry(currRec).State = EntityState.Modified;
|
||||
|
||||
// salvo modifiche!
|
||||
dbCtx.SaveChanges();
|
||||
}
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Error($"Eccezione in TaskExecSaveExecuted{Environment.NewLine}{exc}");
|
||||
}
|
||||
}
|
||||
return callRes;
|
||||
}
|
||||
/// <summary>
|
||||
/// Update ordinamento task
|
||||
/// </summary>
|
||||
/// <param name="rec2upd">Record da spostare x priorità</param>
|
||||
/// <returns></returns>
|
||||
public bool TaskListMove(TaskListModel rec2upd, bool moveUp)
|
||||
{
|
||||
bool done = false;
|
||||
using (var dbCtx = new TaskContext(_configuration))
|
||||
{
|
||||
try
|
||||
{
|
||||
var currData = dbCtx
|
||||
.DbSetTaskList
|
||||
.Where(x => x.TaskId == rec2upd.TaskId)
|
||||
.FirstOrDefault();
|
||||
if (currData != null)
|
||||
{
|
||||
int actOrdinal = currData.Ordinal;
|
||||
TaskListModel? otherRec = null;
|
||||
// cerco, secondo richiesta, precedente o successivo
|
||||
if (moveUp)
|
||||
{
|
||||
otherRec = dbCtx
|
||||
.DbSetTaskList
|
||||
.Where(x => x.Ordinal < currData.Ordinal)
|
||||
.OrderByDescending(x => x.Ordinal)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
else
|
||||
{
|
||||
otherRec = dbCtx
|
||||
.DbSetTaskList
|
||||
.Where(x => x.Ordinal > currData.Ordinal)
|
||||
.OrderBy(x => x.Ordinal)
|
||||
.FirstOrDefault();
|
||||
}
|
||||
// inverto ordinale SE ho record
|
||||
if (otherRec != null)
|
||||
{
|
||||
currData.Ordinal = otherRec.Ordinal;
|
||||
otherRec.Ordinal = actOrdinal;
|
||||
dbCtx.Entry(currData).State = EntityState.Modified;
|
||||
dbCtx.Entry(otherRec).State = EntityState.Modified;
|
||||
}
|
||||
}
|
||||
//salvo
|
||||
dbCtx.SaveChanges();
|
||||
done = true;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Error($"Eccezione in TaskListUpsert{Environment.NewLine}{exc}");
|
||||
}
|
||||
}
|
||||
return done;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Upsert record TaskList
|
||||
/// </summary>
|
||||
/// <param name="rec2upd">Record da aggiornare/inserire</param>
|
||||
/// <returns></returns>
|
||||
public bool TaskListUpsert(TaskListModel rec2upd)
|
||||
{
|
||||
bool done = false;
|
||||
using (var dbCtx = new TaskContext(_configuration))
|
||||
{
|
||||
try
|
||||
{
|
||||
var currData = dbCtx
|
||||
.DbSetTaskList
|
||||
.Where(x => x.TaskId == rec2upd.TaskId && rec2upd.TaskId > 0)
|
||||
.FirstOrDefault();
|
||||
if (currData != null)
|
||||
{
|
||||
currData.Ordinal = rec2upd.Ordinal;
|
||||
currData.Name = rec2upd.Name;
|
||||
currData.Descript = rec2upd.Descript;
|
||||
currData.Command = rec2upd.Command;
|
||||
currData.Args = rec2upd.Args;
|
||||
currData.Freq = rec2upd.Freq;
|
||||
currData.Cad = rec2upd.Cad;
|
||||
currData.DtLastExec = rec2upd.DtLastExec;
|
||||
currData.DtNextExec = rec2upd.DtNextExec;
|
||||
currData.LastDuration = rec2upd.LastDuration;
|
||||
currData.LastResult = rec2upd.LastResult;
|
||||
dbCtx.Entry(currData).State = EntityState.Modified;
|
||||
}
|
||||
else
|
||||
{
|
||||
dbCtx
|
||||
.DbSetTaskList
|
||||
.Add(rec2upd);
|
||||
}
|
||||
dbCtx.SaveChanges();
|
||||
done = true;
|
||||
}
|
||||
catch (Exception exc)
|
||||
{
|
||||
Log.Error($"Eccezione in TaskListUpsert{Environment.NewLine}{exc}");
|
||||
}
|
||||
}
|
||||
return done;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user