Public/ps1/Job/Start-LeftConnectRemoteJobThread.ps1


$threadPreparation = 
{
    import-module UblionConnect   

}

# This is the main entry point
function Start-LeftConnectRemoteJobThread {                                                                                                                                               
    param ($name)
    
    $runningProcesses = (Get-Job   -ErrorAction SilentlyContinue) | Where-object {$_.Name.Contains(("UblionConnect"+$name))}
    
    $runningProcesses | Where-Object {$_.State -ne "Running"} | %{
        Stop-Job $_ #-ErrorAction SilentlyContinue
        Remove-Job $_# -ErrorAction SilentlyContinue    
    }

    $runningProcesses = (Get-Job   -ErrorAction SilentlyContinue) | Where-object {$_.Name.Contains(("UblionConnect"+$name))}


    if (($runningProcesses | Where-Object {$_.State -eq "Running"}).Count -ge 1)
    {
        return
    } else {
        $extension = [int]([int](Get-Date -UFormat %s -Millisecond 0) / 60)

        $directProcessing = Get-ConfigurationValue -name ($name+"DirectProcessing") -defaultValue $false

        Start-Job -InitializationScript $threadPreparation  -Name ("UblionConnect"+$name+$extension) -ScriptBlock {
            param ($sleepTime, $pipeline, $directProcessing)
        
         import-module UblionConnect   -ErrorAction SilentlyContinue -WarningAction SilentlyContinue
            while ($true) {
                Log "$pipeline : Getting job"
                $result = Get-LeftConnectRemoteJob -channel $pipeline
                [system.gc]::Collect()


                if ($directProcessing -and $result -eq $false) {
                    Start-Sleep -seconds $sleepTime
                } elseif ($directProcessing -and $result) {
                } else {
                    Start-Sleep -seconds $sleepTime
                }
                
            }
        } -ArgumentList (Get-ConfigurationValue -name ($name+"Sleep") ), $name,  $directProcessing 

        Log "Starting job instance $(("UblionConnect"+$name+$extension))"

    } 
       
}