2

I'm writing a Powershell script that monitors a directory for changes, and when detected, spawns a job to deal with the file:

get-childitem | foreach-object {
   $name = $_.Name
   $newname = "work-"+$name

   Rename-Item -Path $name -NewName $newname
   Start-Job -ScriptBlock {
      $newvar = $args[0]
      ffmpeg -i $newvar -acodec flac -f ogg $newvar".flac"
      } -ArgumentList $newname
   }
}

When running the script, jobs are spawned correctly, but processing doesn't commence after the third job. For example:

Job 1 --> Complete
Job 2 --> Complete
Job 3 --> Complete
Job 4 --> Running
Job 5 --> Running
Job 6 --> Running

And the script does not progress. Jobs 4-6 are left perpetually in the running state. There is no output file created from ffmpeg, so I know the script block hasn't run.

I have tried setting Set-PSDebug -Trace 2 to monitor the script - variables are correctly being set, and jobs are spawning correctly. However, as the script is running, I can't do a Receive-Job or Get-Job to see what's going on inside of them.

Any advice on how to proceed?

mklement0
  • 382,024
  • 64
  • 607
  • 775
IAmTheSquidward
  • 562
  • 6
  • 22
  • 1
    You can `receive-job -keep` for a job that is running to see what could be going on – Santiago Squarzon Apr 11 '22 at 21:39
  • 1
    As an aside: if feasible, consider using much more lightweight _thread_-based jobs instead, via `Start-ThreadJob` - see [this answer](https://stackoverflow.com/a/56612574/45375). – mklement0 Apr 11 '22 at 22:30
  • 1
    If `Receive-Job` shows no output and the jobs are still running, the likeliest explanation is that the `ffmpeg` calls _haven't finished yet_. – mklement0 Apr 11 '22 at 22:35
  • 1
    As an aside: `$newvar".flac"` is better expressed as `"$newvar.flac"`. Otherwise, you'll be in for a surprise if you try `"foo/"$newvar`, for instance (compound string arguments mustn't start with a _quoted_ token - see [this answer](https://stackoverflow.com/a/65380907/45375). – mklement0 Apr 11 '22 at 22:38

0 Answers0