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

Refactoring Module to C# #22

Merged
merged 27 commits into from
Jun 17, 2024
Merged

Refactoring Module to C# #22

merged 27 commits into from
Jun 17, 2024

Conversation

santisq
Copy link
Owner

@santisq santisq commented Jun 17, 2024

This PR re-implements Invoke-Parallel in C#. This newer version shares the same capabilities of the previous one and offers the following improvements:

Truly pipeline streaming capabilities

Invoke-Parallel v1.1.5

Measure-Command {
    1 | Invoke-Parallel { 0..10 | ForEach-Object { Start-Sleep 1; $_ } } | Select-Object -First 1
} | Select-Object TotalSeconds

# TotalSeconds
# ------------
#       11.10

Invoke-Parallel v1.1.6

Measure-Command {
    1 | Invoke-Parallel { 0..10 | ForEach-Object { Start-Sleep 1; $_ } } | Select-Object -First 1
} | Select-Object TotalSeconds

# TotalSeconds
# ------------
#        1.06

Support for CommonParameters

ForEach-Object v7.5.0.3

This is something missing on ForEach-Object -Parallel as of v7.5.0.3.

PS \> 0..5 | ForEach-Object -Parallel { Write-Error $_ } -ErrorAction Stop
# ForEach-Object: The following common parameters are not currently supported in the Parallel parameter set:
# ErrorAction, WarningAction, InformationAction, PipelineVariable

Invoke-Parallel v1.1.6

A few examples, they should all work properly, please submit an issue if not 😅.

PS \> 0..5 | Invoke-Parallel { Write-Error $_ } -ErrorAction Stop
# Invoke-Parallel: 0

PS \>  0..5 | Invoke-Parallel { Write-Warning $_ } -WarningAction Stop
# WARNING: 1
# Invoke-Parallel: The running command stopped because the preference variable "WarningPreference" or common parameter is set to Stop: 1

PS \> 0..5 | Invoke-Parallel { $_ } -PipelineVariable pipe | ForEach-Object { "[$pipe]" }
# [0]
# [1]
# [5]
# [2]
# [3]
# [4]

Input Script Block check

This check was missing in previous version.

PS \> { 1 + 1 } | Invoke-Parallel { & $_ }
# Invoke-Parallel: Piped input object cannot be a script block. Passed-in script block variables are not supported, and can result in undefined behavior.

Improved Script Block checks for variables passed in via $using: keyword

Invoke-Parallel v1.1.5

PS \> $var = @{ foo = @{ bar = { 1 + 1 } }}
PS \> 1 | Invoke-Parallel { & $using:var['foo']['bar'] }
# 2

Invoke-Parallel v1.1.6

PS \> $var = @{ foo = @{ bar = { 1 + 1 } }}
PS \> 1 | Invoke-Parallel { & $using:var['foo']['bar'] }
# Invoke-Parallel: A $using: variable cannot be a script block. Passed-in script block variables are not supported, and can result in undefined behavior.

Improved -TimeOutSeconds error message

Invoke-Parallel v1.1.5

PS \> 0..10 | Invoke-Parallel { $_; Start-Sleep 5 } -TimeoutSeconds 3
# Invoke-Parallel: The pipeline has been stopped.
# Invoke-Parallel: The pipeline has been stopped.
# Invoke-Parallel: The pipeline has been stopped.
# Invoke-Parallel: The pipeline has been stopped.
# Invoke-Parallel: The pipeline has been stopped.

Invoke-Parallel v1.1.6

PS \> 0..10 | Invoke-Parallel { $_; Start-Sleep 5 } -TimeoutSeconds 3
# 0
# 1
# 3
# 2
# 4
# Invoke-Parallel: Timeout has been reached.

Accept $null as input

Same as ForEach-Object -Parallel, now you can pipe $null to this cmdlet.

PS \> $null | Invoke-Parallel { 'hi' }
# hi

New parameter aliases

Parameter Alias
ThrottleLimit tl
TimeOutSeconds to
Variables vars
Functions funcs
UseNewRunspace unr

@santisq santisq added the enhancement New feature or request label Jun 17, 2024
@santisq santisq self-assigned this Jun 17, 2024
@santisq santisq linked an issue Jun 17, 2024 that may be closed by this pull request
@santisq santisq closed this Jun 17, 2024
@santisq santisq reopened this Jun 17, 2024
@santisq santisq merged commit e1a579c into main Jun 17, 2024
5 of 6 checks passed
@santisq santisq deleted the 14-refactoring-to-c branch June 17, 2024 19:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactoring to C#
1 participant