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
541 stars 87 forks source link

Support for `Receive-RSJob -Wait -AutoRemoveJob` #187

Closed fresh2dev closed 1 year ago

fresh2dev commented 6 years ago

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)
}
erikgraa commented 6 years ago

Would also appreciate this. :)

JustinGrote commented 5 years ago

I will PR this if @proxb is still accepting.