Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[*] fix: unable to fetch results by group #57

Merged
merged 2 commits into from
Sep 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions PowerThreadPool/Core/PowerThreadPool.Control.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ public void StopIfRequested()
}
else if (work != null)
{
_aliveWorkDic.TryRemove(work.ID, out _);
if (work.Group != null)
// If the result needs to be stored, there is a possibility of fetching the result through Group.
// Therefore, Work should not be removed from _aliveWorkDic and _workGroupDic for the time being
if (work.Group == null || !work.ShouldStoreResult)
{
_aliveWorkDic.TryRemove(work.ID, out _);
}
if (work.Group != null && !work.ShouldStoreResult)
{
if (_workGroupDic.TryGetValue(work.Group, out ConcurrentSet<string> idSet))
{
Expand Down Expand Up @@ -310,6 +315,11 @@ public List<ExecuteResult<TResult>> Fetch<TResult>(IEnumerable<string> idList, b
if (executeResultBase != null)
{
resultList.Add(executeResultBase.ToTypedResult<TResult>());

if (removeAfterFetch && _aliveWorkDic.TryRemove(id, out WorkBase work))
{
RemoveWorkFromGroup(work.Group, work);
}
}
else
{
Expand Down
9 changes: 7 additions & 2 deletions PowerThreadPool/Core/PowerThreadPool.Events.cs
Original file line number Diff line number Diff line change
Expand Up @@ -143,8 +143,13 @@ internal void WorkCallbackEnd(WorkBase work, Status status)
CallbackEnd.Invoke(work.ID);
}

_aliveWorkDic.TryRemove(work.ID, out _);
if (work.Group != null)
// If the result needs to be stored, there is a possibility of fetching the result through Group.
// Therefore, Work should not be removed from _aliveWorkDic and _workGroupDic for the time being
if (work.Group == null || !work.ShouldStoreResult)
{
_aliveWorkDic.TryRemove(work.ID, out _);
}
if (work.Group != null && !work.ShouldStoreResult)
{
if (_workGroupDic.TryGetValue(work.Group, out ConcurrentSet<string> idSet))
{
Expand Down
23 changes: 18 additions & 5 deletions PowerThreadPool/Core/PowerThreadPool.Group.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,24 @@ public bool RemoveWorkFromGroup(string groupName, string workID)
{
if (_aliveWorkDic.TryGetValue(workID, out WorkBase work))
{
if (_workGroupDic.TryGetValue(groupName, out ConcurrentSet<string> workIDSet))
{
work.Group = null;
return workIDSet.Remove(workID);
}
return RemoveWorkFromGroup(groupName, work);
}

return false;
}

/// <summary>
/// Remove work from group.
/// </summary>
/// <param name="groupName"></param>
/// <param name="work"></param>
/// <returns>Returns false if either the work or the group does not exist, or if the work does not belong to the group.</returns>
private bool RemoveWorkFromGroup(string groupName, WorkBase work)
{
if (_workGroupDic.TryGetValue(groupName, out ConcurrentSet<string> workIDSet))
{
work.Group = null;
return workIDSet.Remove(work.ID);
}

return false;
Expand Down
1 change: 1 addition & 0 deletions PowerThreadPool/Works/Work.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ internal override string Group
internal override TimeoutOption WorkTimeoutOption => _workOption.TimeoutOption;
internal override RetryOption RetryOption => _workOption.RetryOption;
internal override bool LongRunning => _workOption.LongRunning;
internal override bool ShouldStoreResult => _workOption.ShouldStoreResult;
internal override ConcurrentSet<string> Dependents => _workOption.Dependents;

internal Work(PowerPool powerPool, string id, Func<object[], TResult> function, object[] param, WorkOption<TResult> option)
Expand Down
1 change: 1 addition & 0 deletions PowerThreadPool/Works/WorkBase.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ internal bool IsPausing
internal abstract TimeoutOption WorkTimeoutOption { get; }
internal abstract RetryOption RetryOption { get; }
internal abstract bool LongRunning { get; }
internal abstract bool ShouldStoreResult { get; }
internal abstract ConcurrentSet<string> Dependents { get; }
}
}
Loading