Public/WorkflowJobs.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)


        Start-Job -InitializationScript $threadPreparation  -Name ("UblionConnect"+$name+$extension) -ScriptBlock {
            param ($sleepTime, $pipeline, $directProcessing)
        
            while ($true) {
                $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,  (Get-ConfigurationValue -name ($name+"DirectProcessing") -defaultValue $false )

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

    }

 
       
}

function Stop-LeftConnectJob{
    param($jobParam)

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

    $runningProcesses | %{
        Stop-Job $_.Name
        Remove-Job $_.Name
    }
}

function Stop-AllJobs{

    $runningProcesses = get-job

    $runningProcesses | %{
    
        if ($_.Name.Contains("UblionConnect")) {
            Stop-Job $_.Name
            Remove-Job $_.Name
        }
    }
}

function Create-PipelineJob {
    param ($jobParam)

    

     Create-Pipeline -name ($jobParam.Name+"Sleep") -sleep ($jobParam.WaitTime) -directProcessing ($jobParam.DirectProcessing)
}

function Create-Pipeline {
    param ($name, 
            [int]$sleep,
            $directProcessing)

            if ($sleep -eq 0) {
                $sleep = 10
            }
    Set-ConfigurationValue -name ($name+"Sleep") -value $sleep
    if ($directProcessing) {
        Set-ConfigurationValue -name ($name+"DirectProcessing") -value $true
    
    }


    Start-LeftConnectRemoteJobThread -name $name
}

function Remove-PipelineJob {
    param ($jobParam)
    
    Remove-ConfigurationValue -name ($jobParam.Name+"Sleep") -value $sleep
}

function Get-AllPipelines{

    $configurations = Get-Configuration

    $allProperties = $configurations.PSObject.Properties | Select-Object -Expand Name

    $props = $allProperties | Where-Object {$_.contains("Sleep") }

    if ($props) {
        $props.Replace("Sleep", "")
    }
}

function Start-AllPipelines {
    $pipelines = Get-AllPipelines

    $pipelines | %{
        write-host Starting pipeline $_
        Start-LeftConnectRemoteJobThread -name $_
    }
    
    write-host Starting main receiver Default
    Start-LeftConnectRemoteJobThread -name Default
}



Export-ModuleMember -function  Start-LeftConnectRemoteJobThread
Export-ModuleMember -function  Stop-LeftConnectJob
Export-ModuleMember -function  Stop-AllJobs
Export-ModuleMember -function  Create-PipelineJob
Export-ModuleMember -function  Create-Pipeline
Export-ModuleMember -function  Remove-PipelineJob
Export-ModuleMember -function  Get-AllPipelines
Export-ModuleMember -function  Start-AllPipelines