Skip to content

Commit da06e49

Browse files
committed
Refactor Get-CIPPTimerFunctions and clean up stale entries
Refactored logic to improve handling of orchestrator status, cron string selection, and preferred processor checks. Added cleanup of stale timer function entries from the status table. Removed unnecessary Write-Host statements for cleaner output.
1 parent c12b7fb commit da06e49

File tree

1 file changed

+44
-35
lines changed

1 file changed

+44
-35
lines changed

Modules/CIPPCore/Public/Get-CIPPTimerFunctions.ps1

Lines changed: 44 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,8 @@ function Get-CIPPTimerFunctions {
4040
}
4141

4242
$CIPPRoot = (Get-Item $CIPPCoreModuleRoot).Parent.Parent
43-
Write-Host "Loading CIPPTimers from $CIPPRoot\CIPPTimers.json"
4443
$CippTimers = Get-Content -Path $CIPPRoot\CIPPTimers.json
45-
Write-Host "CIPPTimers content: $CippTimers"
44+
4645
if ($ListAllTasks) {
4746
$Orchestrators = $CippTimers | ConvertFrom-Json | Sort-Object -Property Priority
4847
} else {
@@ -61,45 +60,47 @@ function Get-CIPPTimerFunctions {
6160
}
6261

6362
foreach ($Orchestrator in $Orchestrators) {
64-
$Status = $OrchestratorStatus | Where-Object { $_.RowKey -eq $Orchestrator.Id }
65-
if ($Status.Cron) {
66-
$CronString = $Status.Cron
67-
} else {
68-
$CronString = $Orchestrator.Cron
69-
}
70-
$CronCount = ($CronString -split ' ' | Measure-Object).Count
71-
if ($CronCount -eq 5) {
72-
$Cron = [Ncrontab.Advanced.CrontabSchedule]::Parse($CronString)
73-
} elseif ($CronCount -eq 6) {
74-
$Cron = [Ncrontab.Advanced.CrontabSchedule]::Parse($CronString, [Ncrontab.Advanced.Enumerations.CronStringFormat]::WithSeconds)
75-
} else {
76-
Write-Warning "Invalid cron expression for $($Orchestrator.Command): $($Orchestrator.Cron)"
77-
continue
78-
}
63+
if (Get-Command -Name $Orchestrator.Command -Module CIPPCore -ErrorAction SilentlyContinue) {
64+
$Status = $OrchestratorStatus | Where-Object { $_.RowKey -eq $Orchestrator.Id }
65+
if ($Status.Cron -and $Orchestrator.IsSystem -eq $true -and -not $ResetToDefault.IsPresent) {
66+
$CronString = $Status.Cron
67+
} else {
68+
$CronString = $Orchestrator.Cron
69+
}
7970

80-
if (!$ListAllTasks.IsPresent) {
81-
if ($Orchestrator.PreferredProcessor -and $AvailableNodes -contains $Orchestrator.PreferredProcessor -and $Node -ne $Orchestrator.PreferredProcessor) {
82-
# only run on preferred processor when available
83-
continue
84-
} elseif ((!$Orchestrator.PreferredProcessor -or $AvailableNodes -notcontains $Orchestrator.PreferredProcessor) -and $Node -notin ('http', 'proc')) {
85-
# Catchall function nodes
71+
$CronCount = ($CronString -split ' ' | Measure-Object).Count
72+
if ($CronCount -eq 5) {
73+
$Cron = [Ncrontab.Advanced.CrontabSchedule]::Parse($CronString)
74+
} elseif ($CronCount -eq 6) {
75+
$Cron = [Ncrontab.Advanced.CrontabSchedule]::Parse($CronString, [Ncrontab.Advanced.Enumerations.CronStringFormat]::WithSeconds)
76+
} else {
77+
Write-Warning "Invalid cron expression for $($Orchestrator.Command): $($Orchestrator.Cron)"
8678
continue
8779
}
88-
}
8980

90-
$Now = Get-Date
91-
if ($ListAllTasks.IsPresent) {
92-
$NextOccurrence = [datetime]$Cron.GetNextOccurrence($Now)
93-
} else {
94-
$NextOccurrences = $Cron.GetNextOccurrences($Now.AddMinutes(-15), $Now.AddMinutes(15))
95-
if (!$Status -or $Status.LastOccurrence -eq 'Never') {
96-
$NextOccurrence = $NextOccurrences | Where-Object { $_ -le (Get-Date) } | Select-Object -First 1
81+
if (!$ListAllTasks.IsPresent) {
82+
if ($Orchestrator.PreferredProcessor -and $AvailableNodes -contains $Orchestrator.PreferredProcessor -and $Node -ne $Orchestrator.PreferredProcessor) {
83+
# only run on preferred processor when available
84+
continue
85+
} elseif ((!$Orchestrator.PreferredProcessor -or $AvailableNodes -notcontains $Orchestrator.PreferredProcessor) -and $Node -notin ('http', 'proc')) {
86+
# Catchall function nodes
87+
continue
88+
}
89+
}
90+
91+
$Now = Get-Date
92+
if ($ListAllTasks.IsPresent) {
93+
$NextOccurrence = [datetime]$Cron.GetNextOccurrence($Now)
9794
} else {
98-
$NextOccurrence = $NextOccurrences | Where-Object { $_ -gt $Status.LastOccurrence.DateTime.ToLocalTime() -and $_ -le (Get-Date) } | Select-Object -First 1
95+
$NextOccurrences = $Cron.GetNextOccurrences($Now.AddMinutes(-15), $Now.AddMinutes(15))
96+
if (!$Status -or $Status.LastOccurrence -eq 'Never') {
97+
$NextOccurrence = $NextOccurrences | Where-Object { $_ -le (Get-Date) } | Select-Object -First 1
98+
} else {
99+
$NextOccurrence = $NextOccurrences | Where-Object { $_ -gt $Status.LastOccurrence.DateTime.ToLocalTime() -and $_ -le (Get-Date) } | Select-Object -First 1
100+
}
99101
}
100-
}
101102

102-
if (Get-Command -Name $Orchestrator.Command -Module CIPPCore -ErrorAction SilentlyContinue) {
103+
103104
if ($NextOccurrence -or $ListAllTasks.IsPresent) {
104105
if (!$Status) {
105106
$Status = [pscustomobject]@{
@@ -117,8 +118,9 @@ function Get-CIPPTimerFunctions {
117118
}
118119
Add-CIPPAzDataTableEntity @Table -Entity $Status -Force
119120
} else {
121+
$Status.Command = $Orchestrator.Command
120122
if ($Orchestrator.IsSystem -eq $true -or $ResetToDefault.IsPresent) {
121-
$Status.Cron = $CronString
123+
$Status.Cron = $Orchestrator.Cron
122124
}
123125
$Status.NextOccurrence = $NextOccurrence.ToUniversalTime()
124126
$PreferredProcessor = $Orchestrator.PreferredProcessor ?? ''
@@ -153,4 +155,11 @@ function Get-CIPPTimerFunctions {
153155
}
154156
}
155157
}
158+
159+
foreach ($StaleStatus in $OrchestratorStatus) {
160+
if ($Orchestrators.Id -notcontains $StaleStatus.RowKey) {
161+
Write-Warning "Removing stale timer function entry: $($StaleStatus.RowKey)"
162+
Remove-AzDataTableEntity @Table -Entity $StaleStatus
163+
}
164+
}
156165
}

0 commit comments

Comments
 (0)