Code Monkey home page Code Monkey logo

Comments (6)

tosoikea avatar tosoikea commented on August 22, 2024 1

GetConsumingEnumerable

The count does not drop to zero just by calling 'GetConsumingEnumerable()'.
This can be shown by the following code snippet.

New-Variable -Name LoggingEventQueue    -Scope Script -Value ([System.Collections.Concurrent.BlockingCollection[int]]::new(1000))
New-Variable -Name LoggingRunspace      -Scope Script -Option ReadOnly -Value ([hashtable]::Synchronized(@{ }))

$Script:InitialSessionState = [initialsessionstate]::CreateDefault()

if ($Script:InitialSessionState.psobject.Properties['ApartmentState']) {
	$Script:InitialSessionState.ApartmentState = [System.Threading.ApartmentState]::MTA
}

# Importing variables into runspace
foreach ($sessionVariable in 'LoggingEventQueue') {
	$Value = Get-Variable -Name $sessionVariable -ErrorAction Continue -ValueOnly
	Write-Verbose "Importing variable $sessionVariable`: $Value into runspace"
	$v = New-Object System.Management.Automation.Runspaces.SessionStateVariableEntry -ArgumentList $sessionVariable, $Value, '', ([System.Management.Automation.ScopedItemOptions]::AllScope)
	$Script:InitialSessionState.Variables.Add($v)
}

#Setup runspace
$Script:LoggingRunspace.Runspace = [runspacefactory]::CreateRunspace($Script:InitialSessionState)
$Script:LoggingRunspace.Runspace.Name = 'LoggingQueueConsumer'
$Script:LoggingRunspace.Runspace.Open()

# Spawn Logging Consumer
$Script:LoggingEventQueue.Add(1)
$Script:LoggingEventQueue.Add(2)
$Script:LoggingEventQueue.Add(3)
$Script:LoggingEventQueue.Add(4)
$Script:LoggingEventQueue.Add(5)
$Script:LoggingEventQueue.Add(6)
$Script:LoggingEventQueue.Add(7)
$Script:LoggingEventQueue.Add(8)
$Script:LoggingEventQueue.Add(9)
$Script:LoggingEventQueue.Add(10)


$Consumer = {
	foreach ($Log in $Script:LoggingEventQueue.GetConsumingEnumerable()) {
        Start-Sleep -Milliseconds 200
	}
}

$Script:LoggingRunspace.Powershell = [Powershell]::Create().AddScript($Consumer, $true)
$Script:LoggingRunspace.Powershell.Runspace = $Script:LoggingRunspace.Runspace
$Script:LoggingRunspace.Handle = $Script:LoggingRunspace.Powershell.BeginInvoke()

while ($Script:LoggingEventQueue.Count -gt 0){
	Write-Host $Script:LoggingEventQueue.Count
}

Write-Host "Fin"

Wait-Logging

The last part is about messages being lost, the only portion that should (now) be able to be lost is the last message with Wait-Logging. This could be "fixable" by simply adding another Wait-Sleep after the loop.

(I used my current feature implementation, however this should not alter from the current master.)

function Config-Logging {
	param(
		[Parameter(Mandatory)]
		[String]
		$Name
	)
    Set-LoggingDefaultLevel -Level 'INFO'
    Add-LoggingTarget -Name File -Configuration @{Path = "C:\Users\soennecken\source\repos\private\Logging\ParallelTest\Run_"+$Name+".txt"}
}
$Level = 'DEBUG', 'INFO', 'WARNING', 'ERROR'

$LoggingDemo = {
    Config-Logging -Name ([Guid]::NewGuid())
    foreach ($i in 1..1000) {
        Write-Log -Message 'Message n. {0} , Job{1} ' -Arguments @($i, $_) -Level ERROR
        Start-Sleep -Milliseconds (Get-Random -Min 2 -Max 10)
    }

    Wait-Logging
}

1..5 | Start-RSJob -ScriptBlock $LoggingDemo -ModulesToImport 'Logging', 'PoshRSJob' -VariablesToImport 'Level' -FunctionsToImport 'Config-Logging' | Wait-RSJob | Receive-RSJob

from logging.

EsOsO avatar EsOsO commented on August 22, 2024

I need to take a look into this issue cause my module uses runspace to spawn a thread that actually write the log.

Maybe a runspace inside another runspace is not a good idea for the sanity of the environment in which it runs.

I'll take a look to see if I can work around something

from logging.

SilentBob999 avatar SilentBob999 commented on August 22, 2024

Hi,
I also notice that some message appears after Wait-Logging and can be lost at the end of a script.
I am using those two target: WinEventLog & Console.

I am having a real hard time to understand the mechanic behind the module as I am not familiar with anything related to “runspace”.

I notice that Wait-Logging is waiting for “LoggingEventQueue.Count -gt 0”. However, is the count goes to Zero as soon as “.GetConsumingEnumerable()” is executed?

As a workaround, I move the sleep in Wait-Logging to be after the Count check. And that does help. However, I need at least 200ms to be somewhat reliable.

SilentBob999@bd84551

from logging.

EsOsO avatar EsOsO commented on August 22, 2024

@SilentBob999 Are you using the logging module with PoshRSJob?

from logging.

SilentBob999 avatar SilentBob999 commented on August 22, 2024

Hi @EsOsO ,
I do not use PoshRSJob.
It is all sequential, no "jobs" of any kind. Just a lot of code fragmentation (dot sourcing, import-module, etc....)

from logging.

tosoikea avatar tosoikea commented on August 22, 2024

@EsOsO I think this issue can now be closed.

from logging.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.