proxb / PoshRSJob

Provides an alternative to PSjobs with greater performance and less overhead to run commands in the background, freeing up the console and allowing throttling on the jobs.
MIT License
542 stars 87 forks source link

RSJob on PSv2 #136

Closed MVKozlov closed 7 years ago

MVKozlov commented 7 years ago

Jobs can't set it's state on powershell v2

PS C:\> import-module poshrsjob
PS C:\> (get-module poshrsjob).version
Major  Minor  Build  Revision
-----  -----  -----  --------
1      7      3      5
PS C:\> 1..3|start-rsjob { $_ }

Id       Name                 State           HasMoreData  HasErrors    Command
--       ----                 -----           -----------  ---------    -------
1        Job1                                              {}            $_
2        Job2                                              {}            $_
3        Job3                                              {}            $_

What is the expected behavior?

PS C:\> import-module poshrsjob
PS C:\> (get-module poshrsjob).version
Major  Minor  Build  Revision
-----  -----  -----  --------
1      7      3      4
PS C:\> 1..3|start-rsjob { $_ }
Id       Name                 State           HasMoreData  HasErrors    Command
--       ----                 -----           -----------  ---------    -------
1        Job1                 Running         False        {}            $_
2        Job2                 Running         False        {}            $_
3        Job3                 Running         False        {}            $_

actually Jobs a running but it's State not set, so Wait-RSJob does not exit

MVKozlov commented 7 years ago

hmm... Everything is much worse than I thought even in v1.7.2.4

import-module poshrsjob
1..3|start-rsjob { start-sleep -sec 15; $_ }

take 45 sec to exit to prompt while measure-command { 1..3|start-rsjob { start-sleep -sec 15; $_ } } or $jobs = 1..3 | start-rsjob { $_ } takes milliseconds to run, get-rsjob or $jobs right after that take 15 sec to display job list

sorry, have no time to check earlier versions today :(

proxb commented 7 years ago

I really need to figure out and fix the issue with AppVeyor that broke my V2 tests. I think I saw a PR over at @RamblingCookieMonster Invoke-Parallel (https://github.com/RamblingCookieMonster/Invoke-Parallel/pull/41) that I might steal from to get it working again.

In the meantime, it looks like some background stuff that uses reflection for the Types may not be V2 compatible (https://github.com/proxb/PoshRSJob/blob/master/PoshRSJob/TypeData/PoshRSJob.Types.ps1xml). I'll have to test more in V2 and figure out the best way to handle that. I don't really want to lose this functionality due to V2 as it provides better state tracking of RSJobs.

MVKozlov commented 7 years ago

Just did another test v2 with 1..3|start-rsjob { start-sleep -sec 15; $_ } seems that delay exists when job object outputs it's values to host If I do

$j = 1..3 | Start-RSJob { start-sleep -sec 15; $_ }
start-sleep -sec 14
$j

delay while outputting jobs from $j does not appear, so it's really .Types related. but disappearing the State's for v2 is the real problem because of suspended Wait-RSJob

proxb commented 7 years ago

I found the issue with the disappearing states which was due to one of the techniques that I use for reflection was missing some of the other parameters that exists in V3+. Luckily it was a simple fix by adding $Null as the second parameter and it supports V2+.

Before PowerShell $CRP = $_CRP.GetValue($Worker)

After PowerShell $CRP = $_CRP.GetValue($Worker,$Null)

I couldn't reproduce the delay either after making that change so this may solve both items. I'll add this to the list of things when I upload the next version tonight or tomorrow.

MVKozlov commented 7 years ago

Another v2 error (I continue to report here) ConvertScriptBlockV2 does not support $v -is [type] syntax

PS D:\> $a = 1; $a -is [array]
False
PS D:\> $a | Start-RSJob { $_ -is [array] }
Create : Exception calling "Create" with "1" argument(s): "You must provide a value expression on the right-hand side o
f the '-is' operator."
At PoshRSJob\Private\ConvertScriptBlockV2.ps1:88 char:26

debug show that scriptblock converted to Param($_)$_ -is array workaround: $_ -is "array"

MVKozlov commented 7 years ago

start-sleep issue not fixed, but I fix all other in my fork latest commit

42 too

MVKozlov commented 7 years ago

About Start-Sleep inside scriptblock on PSv2 Seems I found a fix

--- a/PoshRSJob/TypeData/PoshRSJob.Types.ps1xml
+++ b/PoshRSJob/TypeData/PoshRSJob.Types.ps1xml
@@ -83,7 +83,7 @@
             $this.innerjob.HadErrors
          }
          Else {
-            ($this.innerjob.Streams.Error -ne $null)
+            ($this.innerjob.Streams.Error.Count -ne 0)
          }
         </GetScriptBlock>
       </ScriptProperty>

let it be here until widely tested