I'm working on creating a simple email server status page that calls two different CFCs.
The status page requirements:
- Query a MariaDB database table via a CFC and return data from two fields: server_name (ie. MyServerName) & server_domain (ie. mail.domain.com). Currently, there are 4 rows in the database table to pull.
- Hand the database data from step 1 to a CFC that checks if port 25 is listening. If the CFC can reach port 25 the result is true, if not the result is false. This step needs to be threaded.
- Hand the boolean result from step 2 through a loop to print the server_name and boolean result.
Output something similar to this:
MyServerName - <up arrow>
MyServerName2 - <up arrow>
MyServerName3 - <up arrow>
MyServerName4 - <down arrow>
The code:
RetrieveEmailServers = APPLICATION.selectQueries.RetrieveEmailServers()
if (RetrieveEmailServers.recordCount) {
for(i = 1; i <= RetrieveEmailServers.recordCount(); i++) {
LOCAL.theDomains = RetrieveEmailServers.check_servers_domain[i];
LOCAL.theNames = RetrieveEmailServers.check_servers_name[i];
thread action="run" name="thread#i#" theDomains="#LOCAL.theDomains#" theNames="#LOCAL.theNames#" {
VARIABLES.theServers = APPLICATION.emailCheck.checkSMTPServer('#domains#',25,'','');
}
}
thread action="join" timeout="6000"{}
for(i = 1; i <= RetrieveEmailServers.recordCount(); i++) {
VARIABLES.theResult = cfthread["thread#i#"];
if (VARIABLES.theResult.theServers) {
LOCAL.theStatus = "<i class='fad fa-angle-double-up text-success fs-1'></i>"
}
else {
LOCAL.theStatus = "<i class='fad fa-angle-double-down text-danger fs-1'></i>"
}
writeOutput(ATTRIBUTES.theNames & " - " & LOCAL.theStatus & "<br>");
}
}
else {
writeOutput("No servers listed at this time.")
}
The error: The key [THESERVERS] does not exist, the structure is empty
For consideration:
- I know my code is not great and I know it could be written better. I'm working hard to improve.
- I'm not a full time coder but I have been coding off and on for many years. I still consider myself a newbie with CFML so a lot of the methodology goes over my head.
- The above code mostly works but I'm having difficulty understanding how to pass information outside of a CFTHREAD to be used in the rest of the page, especially when dealing with CFLOOP.
- I have read many times, but still don't completely understand, how to correctly use the thread-local scope, the Thread scope and the Attributes scope.
- The code above has a simple task, to check a port, but the end goal is to use similar code for other parts of my application. I'm aware there are better monitoring tools available; this is an exercise to help me understand and learn.
- Specific to Lucee, I'm aware that
threadData()['thread#i#'].status;
or similar may be a required modification tocfthread[]
.