PoshRSJob icon indicating copy to clipboard operation
PoshRSJob copied to clipboard

Preserve jobs order

Open MVKozlov opened this issue 6 years ago • 11 comments

Fixes #179.

Changes proposed in this pull request: Job order for Get-RSJob preserved from input to output

How to test this code:

PS C:\> 1..10 | Start-RSJob {
>>     if (1 -BAND $_) {
>>         "First ($_)"
>>     } Else {
>>         Start-sleep -seconds 2
>>         "Last ($_)"
>>     }
>> } | Wait-RSJob | Receive-RSJob
First (1)
First (3)
First (5)
First (7)
First (9)
Last (2)
Last (4)
Last (6)
Last (8)
Last (10)

Has been tested on (remove any that don't apply):

  • Powershell 2 and above
  • Windows 7 and above

MVKozlov avatar Mar 16 '18 11:03 MVKozlov

I think I will accept this one after https://github.com/proxb/PoshRSJob/pull/178 has been completed assuming that the OP gets back to my question in a reasonable amount of time.

proxb avatar Mar 21 '18 23:03 proxb

I know this is a little old but I would like to be able to use the streaming aspect of Wait-RSJob so that as jobs complete I can immediately start another long running single threaded processes so that they are going through that downstream process as soon as they are individually done, not once all of them are done.

ChrisMagnuson avatar Nov 21 '18 18:11 ChrisMagnuson

so, you want single threading ? may be just not use RSJob ? :) or may be join multiple jobs inside one ?

or, if you want to start the same jobs with different data, then look to -Throttle 1

because Wait-RSJob return Job object, not data

MVKozlov avatar Nov 21 '18 19:11 MVKozlov

@MVKozlov I am sorry I must not have been clear, I want to process a set of asynchronous jobs as soon as each one is complete.

To do this I would need the streaming functionality that passes whichever jobs have reached completion down the pipeline as soon as the job is complete.

ChrisMagnuson avatar Nov 21 '18 22:11 ChrisMagnuson

it sounds still not clear to me. let's divide question...

  1. Do you want to run the different jobs each time or the same with different data ? $data1 | { scriptblock-1 } $data2 | { scriptblock-2 } or $data1 | { scriptblock-1 } $data2 | { scriptblock-1 }

  2. Do you want your output from scriptblocks go to into next scriptblock or it is fully independent and just need to be run sequidentally ? $data1 | { scriptblock-1 } | { scriptblock-2 } or $data1 | { scriptblock-1 }, $data2 | { scriptblock-2 }

MVKozlov avatar Nov 23 '18 07:11 MVKozlov

@MVKozlov I have created some sample code below that I think will help explain.

I believe this is closest to the $data1 | { scriptblock-1 } | { scriptblock-2 } scenario you listed above but it would be more like $data1,$data2 | { scriptblock-1 } | { scriptblock-2 }:

$ArrayOfObjects = [PSCustomObject]@{
    URL1 = "https://domain.com/file83423"
    URL2 = "https://domain.com/file88234"
},
[PSCustomObject]@{
    URL1 = "https://domain.com/file12342"
    URL2 = "https://domain.com/file84342"
}

$ArrayOfObjects | 
Start-RSJob -ScriptBlock { ScriptBlockToDownloadFilesFromURLs } |
Wait-RSJob |
Receive-RSJob |
ForEach-Object -Process {
    #This should be executed the moment any job has downloaded both of its two files    
    ScriptBlockToDoSomethingWithTheTwoFilesPerIndividualJob
    #This should keep being executed as each individual set of two downloaded files is passed to it by jobs that have completed 
    #This happens in whatever order the jobs complete, not in the order of the $ArrayOfObjects
}

ChrisMagnuson avatar Nov 29 '18 16:11 ChrisMagnuson

ok, now I see. but seems there is no method to accomplish this task, sorry. I have a plans to redesign commands to property support pipeline but have no time just now. and @proxb still not approve previous PRs...

MVKozlov avatar Nov 30 '18 21:11 MVKozlov

@ChrisMagnuson, fortunately for you, it was a calm morning :)

https://github.com/MVKozlov/PoshRSJob/tree/PerJobTimeout

MVKozlov avatar Dec 03 '18 08:12 MVKozlov

@MVKozlov Nice!

I cloned this and ran the example from PoshRSJob README.md and now the example works again:

PS > 1..10|Start-RSJob {
>>     if (1 -BAND $_){
>>         "First ($_)"
>>     }Else{
>>         Start-sleep -seconds 2
>>         "Last ($_)"
>>     }
>> }|Wait-RSJob|Receive-RSJob|ForEach{"I am $($_)"}
I am First (1)
I am First (3)
I am First (5)
I am First (7)
I am First (9)
I am Last (2)
I am Last (4)
I am Last (6)
I am Last (8)
I am Last (10)

Will there be another separate PR for this?

ChrisMagnuson avatar Dec 04 '18 19:12 ChrisMagnuson

I can make as many PR as need but @proxb seems have no time to visit here

MVKozlov avatar Dec 04 '18 21:12 MVKozlov

@MVKozlov I see what you mean, don't see much activity from @proxb since February.

I will focus on trying to use that branch of your fork in our code for now.

ChrisMagnuson avatar Dec 05 '18 15:12 ChrisMagnuson