-
Notifications
You must be signed in to change notification settings - Fork 6.6k
Expand file tree
/
Copy pathSet-CIPPOffloadFunctionTriggers.ps1
More file actions
131 lines (118 loc) · 6.11 KB
/
Set-CIPPOffloadFunctionTriggers.ps1
File metadata and controls
131 lines (118 loc) · 6.11 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
function Set-CIPPOffloadFunctionTriggers {
<#
.SYNOPSIS
Manages non-HTTP triggers on function apps based on offloading configuration.
.DESCRIPTION
Automatically detects if running on an offloaded function app (contains hyphen in name).
If this is the main function app (no hyphen), checks the offloading state from Config table
and disables/enables timer, activity, orchestrator, and queue triggers accordingly.
Offloaded function apps (with hyphen) are skipped as they should have triggers enabled.
.EXAMPLE
Set-CIPPOffloadFunctionTriggers
Automatically manages triggers based on current function app context and offloading state.
#>
[CmdletBinding(SupportsShouldProcess)]
param()
# Get current function app name
$FunctionAppName = $env:WEBSITE_SITE_NAME
# Check if this is an offloaded function app (contains hyphen)
if ($FunctionAppName -match '-') {
return $true
}
# Get offloading state from Config table
$Table = Get-CippTable -tablename 'Config'
$OffloadConfig = Get-CIPPAzDataTableEntity @Table -Filter "PartitionKey eq 'OffloadFunctions' and RowKey eq 'OffloadFunctions'"
$OffloadEnabled = $false
[bool]::TryParse($OffloadConfig.state, [ref]$OffloadEnabled) | Out-Null
# Trigger Last change table
$TriggerChangeTable = Get-CippTable -tablename 'OffloadTriggerChange'
$LastChange = Get-CIPPAzDataTableEntity @TriggerChangeTable
if ($LastChange -and $LastChange.Timestamp -gt (Get-Date).AddMinutes(-30).ToUniversalTime() -and $LastChange.Offloading -eq $OffloadEnabled) {
Write-Information "Last trigger change was at $LastChange, skipping update to avoid rapid changes."
return $true
}
# Determine resource group
if ($env:WEBSITE_RESOURCE_GROUP) {
$ResourceGroupName = $env:WEBSITE_RESOURCE_GROUP
} else {
$Owner = $env:WEBSITE_OWNER_NAME
if ($env:WEBSITE_SKU -ne 'FlexConsumption' -and $Owner -match '^(?<SubscriptionId>[^+]+)\+(?<RGName>[^-]+(?:-[^-]+)*?)(?:-[^-]+webspace(?:-Linux)?)?$') {
$ResourceGroupName = $Matches.RGName
} else {
throw 'Could not determine resource group. Please provide ResourceGroupName parameter.'
}
}
# Define the triggers to disable when offloading is enabled
$TargetedTriggers = @(
'CIPPTimer'
'CIPPActivityFunction'
'CIPPOrchestrator'
'CIPPQueueTrigger'
)
try {
if ($OffloadEnabled -and $env:WEBSITE_SKU -ne 'FlexConsumption') {
$AppSettings = @{}
$SkippedTriggers = [System.Collections.Generic.List[string]]::new()
foreach ($Trigger in $TargetedTriggers) {
$SettingKey = "AzureWebJobs.$Trigger.Disabled"
# Convert setting key to environment variable format (dots become underscores)
$EnvVarName = $SettingKey -replace '\.', '_'
$CurrentValue = [System.Environment]::GetEnvironmentVariable($EnvVarName)
if ($CurrentValue -eq '1') {
Write-Verbose "Skipping $SettingKey - already set to 1"
$SkippedTriggers.Add($Trigger)
} else {
$AppSettings[$SettingKey] = '1'
Write-Verbose "Setting $SettingKey = 1"
}
}
# Update app settings only if there are changes to make
if ($AppSettings.Count -gt 0) {
if ($PSCmdlet.ShouldProcess($FunctionAppName, 'Disable non-HTTP triggers')) {
$LastChange = @{
PartitionKey = 'TriggerChange'
RowKey = 'LastChange'
Offloading = $OffloadEnabled
}
Add-CIPPAzDataTableEntity @TriggerChangeTable -Entity $LastChange -Force | Out-Null
Update-CIPPAzFunctionAppSetting -Name $FunctionAppName -ResourceGroupName $ResourceGroupName -AppSetting $AppSettings | Out-Null
Write-Information "Successfully disabled $($AppSettings.Count) non-HTTP trigger(s) on $FunctionAppName"
}
}
} else {
$RemoveKeys = [System.Collections.Generic.List[string]]::new()
$SkippedTriggers = [System.Collections.Generic.List[string]]::new()
foreach ($Trigger in $TargetedTriggers) {
$SettingKey = "AzureWebJobs.$Trigger.Disabled"
# Convert setting key to environment variable format (dots become underscores)
$EnvVarName = $SettingKey -replace '\.', '_'
$CurrentValue = [System.Environment]::GetEnvironmentVariable($EnvVarName)
if ([string]::IsNullOrEmpty($CurrentValue) -or $CurrentValue -ne '1') {
Write-Verbose "Skipping $SettingKey - already enabled or not set"
$SkippedTriggers.Add($Trigger)
} else {
$RemoveKeys.Add($SettingKey)
Write-Verbose "Removing $SettingKey"
}
}
# Update app settings with removal of keys only if there are changes to make
if ($RemoveKeys.Count -gt 0) {
if ($PSCmdlet.ShouldProcess($FunctionAppName, 'Re-enable non-HTTP triggers')) {
$LastChange = @{
PartitionKey = 'TriggerChange'
RowKey = 'LastChange'
Offloading = $OffloadEnabled
}
Add-CIPPAzDataTableEntity @TriggerChangeTable -Entity $LastChange -Force | Out-Null
Update-CIPPAzFunctionAppSetting -Name $FunctionAppName -ResourceGroupName $ResourceGroupName -AppSetting @{} -RemoveKeys $RemoveKeys | Out-Null
Write-Information "Successfully re-enabled $($RemoveKeys.Count) non-HTTP trigger(s) on $FunctionAppName"
}
}
}
return $true
} catch {
$ErrorMessage = Get-CippException -Exception $_
Write-Warning "Failed to update trigger settings: $($ErrorMessage.NormalizedError)"
return $false
}
}