proxb/PoshRSJob

Support for `Receive-RSJob -Wait -AutoRemoveJob`

fresh2dev opened this issue · 2 comments

Do you want to request a feature or report a bug?

Feature Request: Support for -Wait and -AutoRemoveJob in Receive-RSJob (as done in the built-in Receive-Job).

What is the current behavior?

Without this implemented, one must separate processing and cleanup, like so

1..10 | Start-RSJob {
    "I am $($_)"
} | Wait-RSJob | Receive-RSJob

Get-RSJob | Remove-RSJob -Force

If it were implemented, it would allow for single-pipe processing and (I presume) lessen the memory load when a large number of objects are piped in.

1..10 | Start-RSJob {
    "I am $($_)"
} | Wait-RSJob | Receive-RSJob -AutoRemoveJob

For the project I am currently working on, I created a function to emulate this behavior (and -Wait, as in the builtin Receive-Job), but I'm sure it would be faster if implemented in PoshRSJob.

function Receive-CompletedJobs {
  param([switch]$AutoRemoveJob, [switch]$Wait, [int]$WhileGT = 0, [uint32]$SleepMS = 500)

  do
  {
    $jobs = @(Get-RSJob)
    $done_jobs = @($jobs | where {$_.Completed})

    foreach ($job in $done_jobs)
    {
      $job | Receive-RSJob -ea Continue -WarningAction Continue -InformationAction Continue
      if ($AutoRemoveJob) { $job | Remove-RSJob -Force }
    }

    if (-not $Wait) {
      break
    }
    elseif ($jobs.Count - $done_jobs.Count -gt $WhileGT) {
      sleep -Milliseconds $SleepMS
    }
    else {
      break
    }
  } while ($Wait)
}

Would also appreciate this. :)

I will PR this if @proxb is still accepting.