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

1.10.318 #603

Merged
merged 20 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
updates
  • Loading branch information
FriedrichWeinmann committed Nov 2, 2023
commit b9b15356feba77953029b24149a948e13c353196
Binary file modified PSFramework/bin/PSFramework.dll
Binary file not shown.
Binary file modified PSFramework/bin/PSFramework.pdb
Binary file not shown.
32 changes: 32 additions & 0 deletions PSFramework/bin/PSFramework.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 7 additions & 0 deletions PSFramework/internal/scripts/runspaceWorkerCode.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
trap {
Write-PSFMessage -Level Error -Message "Runspace Worker Code failed" -ErrorRecord $_
$__PSF_Worker.State = 'Failed'
$__PSF_Worker.SignalEnd()
throw $_
}

Expand All @@ -32,6 +33,10 @@

$validStates = 'Starting', 'Running'
while ($validStates -contains $__PSF_Worker.State) {
# Inqueue is closed and all items processed?
if ($__PSF_Worker.Done) { break }
if ($__PSF_Worker.MaxItems -and $__PSF_Worker.MaxItems -ge $__PSF_Worker.CountInputCompleted)

if ($__PSF_Worker.Throttle) {
$__PSF_Worker.Throttle.GetSlot()
}
Expand Down Expand Up @@ -66,4 +71,6 @@
$__PSF_Worker.LastError = $_
}
}

$__PSF_Worker.SignalEnd()
}
Original file line number Diff line number Diff line change
Expand Up @@ -76,10 +76,10 @@ $dis2 | Remove-PSFRunspaceDispatcher
TODO: Open Dev Items

- Worker:
- Should have a "MaxItems" property, to prevent endless circling when done
- Should have a way to match a closed inqueue against its total processed items to know when to self-terminate
- Should have a "CloseOutQueue" property, to have the last worker runspace close out the output queue when done.
- Should have a public Dispatcher property
- Should have a "MaxItems" property, to prevent endless circling when done [Done]
- Should have a way to match a closed inqueue against its total processed items to know when to self-terminate [Done]
- Should have a "CloseOutQueue" property, to have the last worker runspace close out the output queue when done. [Done]
- Should have a public Dispatcher property [Done]
- Dispatcher
- Should ToString() to its name
- Should ToString() to its name [Done]
#>
9 changes: 9 additions & 0 deletions library/PSFramework/Runspace/RSDispatcher.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,5 +123,14 @@ public void Stop()
foreach (RSWorker worker in Workers.Values)
worker.Stop();
}

/// <summary>
/// String form of the dispatcher
/// </summary>
/// <returns></returns>
public override string ToString()
{
return Name;
}
}
}
42 changes: 42 additions & 0 deletions library/PSFramework/Runspace/RSWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,11 @@ public ErrorRecord LastError
/// </summary>
public bool Completed => CountInputCompleted >= InQueueTotalItemCount;

/// <summary>
/// Whether the inqueue has been closed and fully processed. If so, processing can stop.
/// </summary>
public bool IsDone => dispatcher.Queues[InQueue].Closed && CountInputCompleted >= InQueueTotalItemCount;

/// <summary>
/// If this flag is set, worker runspaces will be hard-killed on stop, rather than waiting for them to gracefully shut down
/// </summary>
Expand Down Expand Up @@ -177,6 +182,21 @@ public ErrorRecord LastError
/// </summary>
public ConcurrentDictionary<string, RSQueue> PerRSValues = new ConcurrentDictionary<string, RSQueue>(StringComparer.InvariantCultureIgnoreCase);

/// <summary>
/// The maximum number of items this worker will process.
/// </summary>
public int MaxItems;

/// <summary>
/// Whether the last worker runspace finishing should close the out-queue of this worker
/// </summary>
public bool CloseOutQueue;

/// <summary>
/// The Dispatcher owning the worker
/// </summary>
public RSDispatcher Dispatcher => dispatcher;

private RunspacePool pool;
private RSDispatcher dispatcher;
private List<RSPowerShellWrapper> runtimes = new List<RSPowerShellWrapper>();
Expand Down Expand Up @@ -335,6 +355,28 @@ public ScriptBlock GetEnd()
return _End.ToGlobal();
}

/// <summary>
/// The tool for each runspace of the worker to signal it is done.
/// Any concluding post-processing is done here, once the last runspace calls it.
/// </summary>
public void SignalEnd()
{
bool terminate = false;
lock (this)
{
_CountDone++;
if (_CountDone < Count)
terminate = true;
}
if (terminate)
return;

// Seal the out queue if desired, ensuring subsequent workers know not to expect further input
if (CloseOutQueue)
dispatcher.CloseQueue(OutQueue);
}
private int _CountDone = 0;

/// <summary>
/// The text form of the current worker
/// </summary>
Expand Down