refactor: make exporting consistent
This commit is contained in:
@@ -0,0 +1,76 @@
|
||||
using Disco.Data.Repository;
|
||||
using Disco.Services.Tasks;
|
||||
using Quartz;
|
||||
using System;
|
||||
using System.Web;
|
||||
using System.Web.Caching;
|
||||
|
||||
namespace Disco.Services.Exporting
|
||||
{
|
||||
public class ExportTask : ScheduledTask
|
||||
{
|
||||
private IExportContext context;
|
||||
public override string TaskName { get => context?.Name ?? "Exporting"; }
|
||||
public override bool SingleInstanceTask { get { return false; } }
|
||||
public override bool CancelInitiallySupported { get { return false; } }
|
||||
|
||||
public static ExportTaskContext ScheduleNow(IExportContext exportContext)
|
||||
{
|
||||
// Build Context
|
||||
var taskContext = new ExportTaskContext(exportContext);
|
||||
|
||||
// Build Data Map
|
||||
var task = new ExportTask();
|
||||
JobDataMap taskData = new JobDataMap() { { nameof(ExportTask), taskContext } };
|
||||
|
||||
// Schedule Task
|
||||
taskContext.TaskStatus = task.ScheduleTask(taskData);
|
||||
|
||||
return taskContext;
|
||||
}
|
||||
|
||||
private static string GetCacheKey(Guid exportId) => $"ExportTask_{exportId}";
|
||||
|
||||
public static ExportTaskContext ScheduleNowCacheResult(IExportContext exportContext, Func<Guid, string> returnUrlBuilder)
|
||||
{
|
||||
var taskContext = ScheduleNow(exportContext);
|
||||
|
||||
var key = GetCacheKey(taskContext.Id);
|
||||
HttpRuntime.Cache.Insert(key, taskContext, null, DateTime.Now.AddMinutes(60), Cache.NoSlidingExpiration, CacheItemPriority.NotRemovable, null);
|
||||
|
||||
taskContext.TaskStatus.SetFinishedUrl(returnUrlBuilder(taskContext.Id));
|
||||
|
||||
return taskContext;
|
||||
}
|
||||
|
||||
public static bool TryFromCache(Guid? exportId, out ExportTaskContext exportContext)
|
||||
{
|
||||
if (exportId != null)
|
||||
{
|
||||
var key = GetCacheKey(exportId.Value);
|
||||
|
||||
if (HttpRuntime.Cache.Get(key) is ExportTaskContext context)
|
||||
{
|
||||
exportContext = context;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
exportContext = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
protected override void ExecuteTask()
|
||||
{
|
||||
var context = (ExportTaskContext)ExecutionContext.JobDetail.JobDataMap[nameof(ExportTask)];
|
||||
this.context = context.ExportContext;
|
||||
|
||||
Status.UpdateStatus(0, "Exporting", "Starting...");
|
||||
|
||||
using (var database = new DiscoDataContext())
|
||||
{
|
||||
context.Result = context.ExportContext.Export(database, Status);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user