Skip to content

Commit

Permalink
Switch $LogToProccess to [System.Collections.Concurrent.ConcurrentQue…
Browse files Browse the repository at this point in the history
…ue[hashtable]] type

changed $LogToProccess [System.Collections.ArrayList] to  [System.Collections.Concurrent.ConcurrentQueue[hashtable]]
  • Loading branch information
mdaneri committed Jun 26, 2024
1 parent c015eb0 commit 313b01f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/Private/Context.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,7 @@ function New-PodeContext {
}

# requests that should be logged
$ctx.LogsToProcess = [System.Collections.ArrayList]::new()
$ctx.LogsToProcess = [System.Collections.Concurrent.ConcurrentQueue[hashtable]]::new()

# middleware that needs to run
$ctx.Server.Middleware = @()
Expand Down
108 changes: 50 additions & 58 deletions src/Private/Logging.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -778,9 +778,8 @@ function Write-PodeRequestLog {
$item.User = $user -ireplace '\s+', '.'
}
}

# add the item to be processed
$null = $PodeContext.LogsToProcess.Add(@{
$null = $PodeContext.LogsToProcess.Enqueue(@{
Name = $name
Item = $item
})
Expand Down Expand Up @@ -822,73 +821,67 @@ function Start-PodeLoggingRunspace {
}

$script = {
$log = @{}
while (!$PodeContext.Tokens.Cancellation.IsCancellationRequested) {
# if there are no logs to process, just sleep for a few seconds - but after checking the batch
if ($PodeContext.LogsToProcess.Count -eq 0) {
Test-PodeLoggerBatch
Start-Sleep -Seconds 5
continue
}

if ($PodeContext.LogsToProcess.Count -ge $PodeContext.Server.Logging.QueueLimit) {
Invoke-PodeHandleFailure -Message "Reached the log Queue Limit of $($PodeContext.Server.Logging.QueueLimit)" -FailureAction $logger.Method.Arguments.FailureAction
}
if ($PodeContext.LogsToProcess.TryDequeue([ref]$log)) {

# safely pop off the first log from the array
$log = (Lock-PodeObject -Return -Object $PodeContext.LogsToProcess -ScriptBlock {
$log = $PodeContext.LogsToProcess[0]
$null = $PodeContext.LogsToProcess.RemoveAt(0)
return $log
})

# run the log item through the appropriate method
$logger = Get-PodeLogger -Name $log.Name
$now = [datetime]::Now
# if the log is null, check batch then sleep and skip
if ($null -eq $log) {
Start-Sleep -Milliseconds 100
continue
}

# if the log is null, check batch then sleep and skip
if ($null -eq $log) {
Start-Sleep -Milliseconds 100
continue
}
# run the log item through the appropriate method
$logger = $PodeContext.Server.Logging.Types[$log.Name]
$now = [datetime]::Now

# convert to log item into a writable format
$rawItems = $log.Item
$_args = @($log.Item) + @($logger.Arguments)

$result = @(Invoke-PodeScriptBlock -ScriptBlock $logger.ScriptBlock -Arguments $_args -UsingVariables $logger.UsingVariables -Return -Splat)

# check batching
$batch = $logger.Method.Batch
if ($batch.Size -gt 1) {
# add current item to batch
$batch.Items += $result
$batch.RawItems += $log.Item
$batch.LastUpdate = $now

# if the current amount of items matches the batch, write
$result = $null
if ($batch.Items.Length -ge $batch.Size) {
$result = $batch.Items
$rawItems = $batch.RawItems
}

# convert to log item into a writable format
$rawItems = $log.Item
$_args = @($log.Item) + @($logger.Arguments)

$result = @(Invoke-PodeScriptBlock -ScriptBlock $logger.ScriptBlock -Arguments $_args -UsingVariables $logger.UsingVariables -Return -Splat)

# check batching
$batch = $logger.Method.Batch
if ($batch.Size -gt 1) {
# add current item to batch
$batch.Items += $result
$batch.RawItems += $log.Item
$batch.LastUpdate = $now

# if the current amount of items matches the batch, write
$result = $null
if ($batch.Items.Length -ge $batch.Size) {
$result = $batch.Items
$rawItems = $batch.RawItems
# if we're writing, reset the items
if ($null -ne $result) {
$batch.Items = @()
$batch.RawItems = @()
}
}

# if we're writing, reset the items
# send the writable log item off to the log writer
if ($null -ne $result) {
$batch.Items = @()
$batch.RawItems = @()
foreach ($method in $logger.Method) {
$_args = @(, $result) + @($method.Arguments) + @(, $rawItems)
$null = Invoke-PodeScriptBlock -ScriptBlock $method.ScriptBlock -Arguments $_args -UsingVariables $logger.Method.UsingVariables -Splat
}
}
}

# send the writable log item off to the log writer
if ($null -ne $result) {
foreach ($method in $logger.Method) {
$_args = @(, $result) + @($method.Arguments) + @(, $rawItems)
$null = Invoke-PodeScriptBlock -ScriptBlock $method.ScriptBlock -Arguments $_args -UsingVariables $logger.Method.UsingVariables -Splat
}
# small sleep to lower cpu usage
Start-Sleep -Milliseconds 100
}
else {
Test-PodeLoggerBatch
Start-Sleep -Seconds 5
}

# small sleep to lower cpu usage
Start-Sleep -Milliseconds 100
}
}

Expand Down Expand Up @@ -948,7 +941,7 @@ function Write-PodeMainLog {
if (!(Test-PodeLoggerEnabled -Name $name)) {
return
}

switch ($PSCmdlet.ParameterSetName.ToLowerInvariant()) {
'parameter' {
$Message = if ($Parameters) {
Expand Down Expand Up @@ -1010,10 +1003,9 @@ function Write-PodeMainLog {
}

# add the item to be processed
$null = $PodeContext.LogsToProcess.Add(@{
$null = $PodeContext.LogsToProcess.Enqueue(@{
Name = $name
Item = $item
})


}
4 changes: 2 additions & 2 deletions src/Public/Logging.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -1053,7 +1053,7 @@ function Write-PodeErrorLog {
$item['ThreadId'] = [System.Threading.Thread]::CurrentThread.ManagedThreadId #[int]$ThreadId

# add the item to be processed
$null = $PodeContext.LogsToProcess.Add(@{
$null = $PodeContext.LogsToProcess.Enqueue(@{
Name = $name
Item = $item
})
Expand Down Expand Up @@ -1161,7 +1161,7 @@ function Write-PodeLog {
}

# add the item to be processed
$null = $PodeContext.LogsToProcess.Add($logItem)
$PodeContext.LogsToProcess.Enqueue($logItem)
}

<#
Expand Down

0 comments on commit 313b01f

Please sign in to comment.