Performing Garbage Collection on an AEM instance. Which is Automated using cURL and Jenkins. In this post, we are focusing on AEM deployed on a Windows Server. So, PowerShell script will be used to trigger the Garbage Collection.
PRE-REQUISITES:
AEM – it is assumed that the AEM instance is up and running
cURL – it is assumed that cURL is already installed and configured on the windows server
Jenkins – it is assumed that the Jenkins server is up and running and also assumed that you already know how to create a Job on Jenkins
LIMITATIONS:
The script will be modified in upcoming updates to accommodate the respective AEM version.
As of Now, the script will run on AEM 6.3 ONLY
SYNOPSIS:
We shall check if we are running Garbage Collection on Author/Publish and based on that we shall set the CQ_PORT to 4502/4503 appropriately
we shall check the OAK Version of the AEM instance and based on the OAK version we shall determine which AEM version we are running
Based on the AEM version we shall trigger the appropriate Garbage Collection command.
GUIDE:
Step-01: Add the Project Description

Description of the Jenkins Job
Step-02: Setup House Keeping to minimise the no# of builds
Step-03: Define Parameters for the Build – Remote Host Parameter





Step-04: Build step PowerShell Script
[code lang=powershell]
# stopping the job if it encounters error
$ErrorActionPreference = ‘Stop’
# Credentials are stored in env and dynamic variables
$SecurePassword = $env:Password | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $env:User, $SecurePassword
# Parameters that are dynamically injected during runtime
$HOSTNAME=$env:Computer
$AEMENV=$env:AEMINST
$CQ_USER=$env:cquser
$CQ_PASS=$env:cqpass
# Invoke a command on the remote machine.
Invoke-Command -ComputerName $env:Computer -Credential $cred -ScriptBlock {
# calling Dynamically injected Parameters
param($HOSTNAME,$AEMENV,$CQ_USER,$CQ_PASS)
# Path of the executable cURL
$CURLEXE = ‘C:\Program Files\curl76\bin\curl.exe’
# Checking AEM Instance Type and seting appropriate PORT
switch ( $AEMENV )
{
Publish { “We are running Garbage Collection on $AEMENV at $HOSTNAME”
$CQ_PORT=4503
}
Author { “We are running Garbage Collection on $AEMENV at $HOSTNAME”
$CQ_PORT=4502
}
AuthorMongo { “We are running Garbage Collection on $AEMENV at $HOSTNAME”
$CQ_PORT=4502
}
}
# Checking the AEM OAK Version to determine the AEM version
$url = “http://${HOSTNAME}:${CQ_PORT}/crx/explorer/index.jsp”
$webContent = Invoke-WebRequest -Uri $url -UseBasicParsing
$webContent.Content -match “(?.*)” | out-null
$OAK_VERSION = $matches[‘title’] | %{ $_.Split(‘ ‘)[3]; }
$NEW_OAKVER = $OAK_VERSION | %{ $_.Split(‘.’)[1]; }
# Setting the AEM version
switch ( [int]$NEW_OAKVER )
{
0 {“$HOSTNAME is running Oak $OAK_VERSION on AEM 6.0”}
2 {“$HOSTNAME is running Oak $OAK_VERSION on AEM 6.1”}
4 {“$HOSTNAME is running Oak $OAK_VERSION on AEM 6.2”}
6 {“$HOSTNAME is running Oak $OAK_VERSION on AEM 6.3”}
default {“Can not determine OAK Version. Aborting..”
break
}
}
# Needs some work to be done here to add other AEM versions, as of now works on AEM 6.3
# cURL command to trigger the AEM Garbage Collection
$GCURL= “http://${HOSTNAME}:${CQ_PORT}/system/console/jmx/org.apache.jackrabbit.oak:name=Segment+node+store+blob+garbage+collection,type=BlobGarbageCollection/op/startBlobGC/boolean,boolean”
$CurlArgument = ‘-s’, ‘-o’, ‘/dev/null’,
‘–user’, “${CQ_USER}:${CQ_PASS}”,
‘-w’, “%{http_code}”,
‘-X’, ‘POST’,
“$GCURL”,
‘–data’, “markOnly=true&forceBlobIdRetrieve=false”
$HTTP_RES_CODE = & $CURLEXE @CurlArgument
$HTTPRESCODE_SW = $HTTP_RES_CODE.subString(0,2)
switch ( $HTTPRESCODE_SW )
{
20 {“BlobGC Successfully Triggered.”}
40 {“That didn’t work. HTTP $HTTP_RES_CODE received.”
break
}
50 {“Server error. Result code $HTTP_RES_CODE received; fix the server then try again.”
break
}
default {“$HTTP_RES_CODE received; I have no idea how I got here. Aborting.”
break
}
}
} -ArgumentList $HOSTNAME,$AEMENV,$CQ_USER,$CQ_PASS
[/code]
Step-05: Jenkins Build Options to trigger the Build
Step-06: Console Output of Build Job
Step-02: Setup House Keeping to minimise the no# of builds

House keeping Jenkins by restricting the max # of build and retention
Step-03: Define Parameters for the Build – Remote Host Parameter

Remote Host on which the Garbage Collection will be Triggered

Username for logging-in to Remote Host

Password for logging-in to Remote Host

Type of AEM instance – Author/Publish

Username of the AEM instance

The password of the AEM instance
Step-04: Build step PowerShell Script
[code lang=powershell]
# stopping the job if it encounters error
$ErrorActionPreference = ‘Stop’
# Credentials are stored in env and dynamic variables
$SecurePassword = $env:Password | ConvertTo-SecureString -AsPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential -ArgumentList $env:User, $SecurePassword
# Parameters that are dynamically injected during runtime
$HOSTNAME=$env:Computer
$AEMENV=$env:AEMINST
$CQ_USER=$env:cquser
$CQ_PASS=$env:cqpass
# Invoke a command on the remote machine.
Invoke-Command -ComputerName $env:Computer -Credential $cred -ScriptBlock {
# calling Dynamically injected Parameters
param($HOSTNAME,$AEMENV,$CQ_USER,$CQ_PASS)
# Path of the executable cURL
$CURLEXE = ‘C:\Program Files\curl76\bin\curl.exe’
# Checking AEM Instance Type and seting appropriate PORT
switch ( $AEMENV )
{
Publish { “We are running Garbage Collection on $AEMENV at $HOSTNAME”
$CQ_PORT=4503
}
Author { “We are running Garbage Collection on $AEMENV at $HOSTNAME”
$CQ_PORT=4502
}
AuthorMongo { “We are running Garbage Collection on $AEMENV at $HOSTNAME”
$CQ_PORT=4502
}
}
# Checking the AEM OAK Version to determine the AEM version
$url = “http://${HOSTNAME}:${CQ_PORT}/crx/explorer/index.jsp”
$webContent = Invoke-WebRequest -Uri $url -UseBasicParsing
$webContent.Content -match “(?.*)” | out-null
$OAK_VERSION = $matches[‘title’] | %{ $_.Split(‘ ‘)[3]; }
$NEW_OAKVER = $OAK_VERSION | %{ $_.Split(‘.’)[1]; }
# Setting the AEM version
switch ( [int]$NEW_OAKVER )
{
0 {“$HOSTNAME is running Oak $OAK_VERSION on AEM 6.0”}
2 {“$HOSTNAME is running Oak $OAK_VERSION on AEM 6.1”}
4 {“$HOSTNAME is running Oak $OAK_VERSION on AEM 6.2”}
6 {“$HOSTNAME is running Oak $OAK_VERSION on AEM 6.3”}
default {“Can not determine OAK Version. Aborting..”
break
}
}
# Needs some work to be done here to add other AEM versions, as of now works on AEM 6.3
# cURL command to trigger the AEM Garbage Collection
$GCURL= “http://${HOSTNAME}:${CQ_PORT}/system/console/jmx/org.apache.jackrabbit.oak:name=Segment+node+store+blob+garbage+collection,type=BlobGarbageCollection/op/startBlobGC/boolean,boolean”
$CurlArgument = ‘-s’, ‘-o’, ‘/dev/null’,
‘–user’, “${CQ_USER}:${CQ_PASS}”,
‘-w’, “%{http_code}”,
‘-X’, ‘POST’,
“$GCURL”,
‘–data’, “markOnly=true&forceBlobIdRetrieve=false”
$HTTP_RES_CODE = & $CURLEXE @CurlArgument
$HTTPRESCODE_SW = $HTTP_RES_CODE.subString(0,2)
switch ( $HTTPRESCODE_SW )
{
20 {“BlobGC Successfully Triggered.”}
40 {“That didn’t work. HTTP $HTTP_RES_CODE received.”
break
}
50 {“Server error. Result code $HTTP_RES_CODE received; fix the server then try again.”
break
}
default {“$HTTP_RES_CODE received; I have no idea how I got here. Aborting.”
break
}
}
} -ArgumentList $HOSTNAME,$AEMENV,$CQ_USER,$CQ_PASS
[/code]
Step-05: Jenkins Build Options to trigger the Build

Jenkins Build Options
Step-06: Console Output of Build Job

Console Output – Job well done.
No comments:
Post a Comment
If you have any doubts or questions, please let us know.