This commit is contained in:
2020-05-17 18:30:01 +00:00
parent cc828894a5
commit f5cc3f818b
3 changed files with 56 additions and 133 deletions

View File

@@ -236,44 +236,52 @@ namespace AyaNova.Biz
public long RefCount { get; set; }
public string Name { get; set; }
}
/// <summary>
/// Process bulk tag operation
/// </summary>
/// <returns>true if object needs to be saved or false if no changes were made</returns>
internal static bool ProcessBulkTagOperation(List<string> tagCollection, string tag, string toTag, JobSubType subType)
{
switch (subType)
{
case JobSubType.TagAddAny:
case JobSubType.TagAdd:
if (!tagCollection.Contains(tag))
{
tagCollection.Add(tag);
return true;
}
return false;
case JobSubType.TagRemoveAny:
case JobSubType.TagRemove:
return tagCollection.Remove(tag);
case JobSubType.TagReplaceAny:
case JobSubType.TagReplace:
int index = tagCollection.IndexOf(tag);
if (index != -1)
{
tagCollection[index] = toTag;
return true;
}
return false;
default:
throw new System.ArgumentOutOfRangeException($"ProcessBulkTagOperation -> Invalid job Subtype{subType}");
}
}
#endregion utilities
//Originally this whole object was just a simple utility class but was going to handle bulk tag jobs here so made it a full biz object
//keeping only in case I need to handle some job but in the end this could go back to a simple static utility class if it never pans out
////////////////////////////////////////////////////////////////////////////////////////////////
//JOB / OPERATIONS
//
// public async Task HandleJobAsync(OpsJob job)
// {
// await Task.CompletedTask;
// switch (job.JobType)
// {
// // case JobType.BulkCoreBizObjectOperation:
// // await ProcessBulkJobAsync(job);
// // break;
// default:
// throw new System.ArgumentOutOfRangeException($"TagBiz.HandleJob-> Invalid job type{job.JobType.ToString()}");
// }
// }
// private async Task ProcessBulkJobAsync(OpsJob job)
// {
// //Simulate a long running job here
// await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Running, ct);
// await JobsBiz.LogJobAsync(job.GId, $"Tag::BulkJob started...", ct);
// //get XBiz for type specified
// //use XBiz to call get then update then save
// //ideally don't have a huge switch statement with identical code for each object
// await JobsBiz.LogJobAsync(job.GId, "Tag::BulkJob completed", ct);
// await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Completed, ct);
// }

View File

@@ -584,69 +584,30 @@ namespace AyaNova.Biz
{
await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Running, ct);
await JobsBiz.LogJobAsync(job.GId, $"Bulk job {job.SubType} started...", ct);
List<long> idList = new List<long>();
long ProcessedObjectCount = 0;
long ChangedObjectCount = 0;
JObject jobData = JObject.Parse(job.JobInfo);
//TAGS?
string ToTag = null;
string Tag = null;
if (jobData.ContainsKey("tag"))
Tag = (string)jobData["tag"];
if (jobData.ContainsKey("toTag"))
ToTag = (string)jobData["toTag"];
if (jobData.ContainsKey("idList"))
{
idList = ((JArray)jobData["idList"]).ToObject<List<long>>();
// jobData["idList"].Value<List<long>>();
}
else
{
//we need to fetch a list of them all because ALL items was selected
//If they wanted to filter (say by Active=true) they can do that from a grid list mass op
idList = await ct.User.Select(m => m.Id).ToListAsync();
}
bool SaveIt = false;
//Iterate the list fetching each in turn and sending to the processor to handle
idList = await ct.Widget.Select(m => m.Id).ToListAsync();
bool SaveIt = false;
foreach (long id in idList)
{
try
{
SaveIt = false;
//errors would pile up here if not cleared on each iteration
ClearErrors();
//Fetch
var o = await GetAsync(id, false);
//call out processor for each item in turn
SaveIt = false;
ClearErrors();
var o = await GetAsync(id, false);
switch (job.SubType)
{
case JobSubType.TagAddAny:
case JobSubType.TagAdd:
if (!o.Tags.Contains(Tag))
{
o.Tags.Add(Tag);
SaveIt = true;
}
break;
case JobSubType.TagRemoveAny:
case JobSubType.TagRemove:
SaveIt = o.Tags.Remove(Tag);
break;
case JobSubType.TagReplaceAny:
case JobSubType.TagReplace:
int index = o.Tags.IndexOf(Tag);
if (index != -1)
{
o.Tags[index] = ToTag;
SaveIt = true;
}
SaveIt = TagBiz.ProcessBulkTagOperation(o.Tags, (string)jobData["tag"], jobData.ContainsKey("toTag") ? (string)jobData["toTag"] : null, job.SubType);
break;
default:
throw new System.ArgumentOutOfRangeException($"ProcessBulkJob -> Invalid job Subtype{job.SubType}");
@@ -660,14 +621,10 @@ namespace AyaNova.Biz
if (u == null)
await JobsBiz.LogJobAsync(job.GId, $"Error processing item {id}: {GetErrorsAsString()}", ct);
else
ProcessedObjectCount++;
ChangedObjectCount++;
}
else
{
ProcessedObjectCount++;
}
ChangedObjectCount++;
}
catch (Exception ex)
{
@@ -675,7 +632,7 @@ namespace AyaNova.Biz
await JobsBiz.LogJobAsync(job.GId, ExceptionUtil.ExtractAllExceptionMessages(ex), ct);
}
}
await JobsBiz.LogJobAsync(job.GId, $"Bulk job {job.SubType} processed {ProcessedObjectCount} of {idList.Count}", ct);
await JobsBiz.LogJobAsync(job.GId, $"Bulk job {job.SubType} changed {ChangedObjectCount} of {idList.Count}", ct);
await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Completed, ct);
}

View File

@@ -322,89 +322,47 @@ namespace AyaNova.Biz
private async Task ProcessBulkJobAsync(OpsJob job)
{
{
await JobsBiz.UpdateJobStatusAsync(job.GId, JobStatus.Running, ct);
await JobsBiz.LogJobAsync(job.GId, $"Bulk job {job.SubType} started...", ct);
List<long> idList = new List<long>();
long ProcessedObjectCount = 0;
JObject jobData = JObject.Parse(job.JobInfo);
//TAGS?
string ToTag = null;
string Tag = null;
if (jobData.ContainsKey("tag"))
Tag = (string)jobData["tag"];
if (jobData.ContainsKey("toTag"))
ToTag = (string)jobData["toTag"];
if (jobData.ContainsKey("idList"))
{
idList = ((JArray)jobData["idList"]).ToObject<List<long>>();
// jobData["idList"].Value<List<long>>();
}
idList = ((JArray)jobData["idList"]).ToObject<List<long>>();
else
{
//we need to fetch a list of them all because ALL items was selected
//If they wanted to filter (say by Active=true) they can do that from a grid list mass op
idList = await ct.Widget.Select(m => m.Id).ToListAsync();
}
bool SaveIt = false;
//Iterate the list fetching each in turn and sending to the processor to handle
foreach (long id in idList)
{
try
{
SaveIt = false;
//errors would pile up here if not cleared on each iteration
ClearErrors();
//Fetch
var o = (ICoreBizObjectModel)await GetAsync(id, false);
//call out processor for each item in turn
var o = await GetAsync(id, false);
switch (job.SubType)
{
case JobSubType.TagAddAny:
case JobSubType.TagAdd:
if (!o.Tags.Contains(Tag))
{
o.Tags.Add(Tag);
SaveIt = true;
}
break;
case JobSubType.TagRemoveAny:
case JobSubType.TagRemove:
SaveIt = o.Tags.Remove(Tag);
break;
case JobSubType.TagReplaceAny:
case JobSubType.TagReplace:
int index = o.Tags.IndexOf(Tag);
if (index != -1)
{
o.Tags[index] = ToTag;
SaveIt = true;
}
SaveIt = TagBiz.ProcessBulkTagOperation(o.Tags, (string)jobData["tag"], jobData.ContainsKey("toTag") ? (string)jobData["toTag"] : null, job.SubType);
break;
default:
throw new System.ArgumentOutOfRangeException($"ProcessBulkJob -> Invalid job Subtype{job.SubType}");
}
if (SaveIt)
{
o = await PutAsync((Widget)o);
o = await PutAsync(o);
if (o == null)
await JobsBiz.LogJobAsync(job.GId, $"Error processing item {id}: {GetErrorsAsString()}", ct);
else
ProcessedObjectCount++;
}else{
ProcessedObjectCount++;
}
else
ProcessedObjectCount++;
}
catch (Exception ex)
{