Question

Is there any way to restart the CF server through the Application.cfc, when the application times out? As per Adobe documentation, they showed as follows:

<cffunction name="onApplicationEnd">
    <cfargument name="ApplicationScope" required=true/>
    <cflog file="#This.Name#" type="Information" 
        text="Application #Arguments.ApplicationScope.applicationname# Ended" >
</cffunction>

What I would like to do is replace the <cflog> above with <cfexecute> as follows:

<cfexecute name = "C:\CFRestart.bat"
    outputFile = "C:\output.txt"
    timeout = "1">
</cfexecute>

So OnApplicationEnd will run the CFRestart.bat file when the application times out. Is this possible or not?

Was it helpful?

Solution

onApplicationEnd is not likely to be reached unless you have a very quiet application because every time someone access the application the timeout is reset.

I'd be very uncomfortable using an application to restart a coldfusion instance. I can see all sorts of horrible security issues etc looming. To be honest I'm not really sure why you'd want to restart the server if your application end.

Also, according to the docs onApplicationEnd is called when the server is restarted, so if you did get this working, when you restart your server the application would also have a go at restarting your server. This would get very messy.

OTHER TIPS

Don't believe you can call the .bat script from ColdFusion. Because once it stops the service the <cfexecute> will also terminate (think it runs under the CF service), never reaching the restart.

Guessing you have a server that routinely fails because you're hitting an Out of Memory (OOM) exception. To get over the hump in those situations I setup as batch script as a Windows Scheduled Task (see the first answer there for how) that restarts the server periodically, say every 24, 12, or 6 hours. Choose an interval that makes sense for your situation.

Assuming OOM is the root cause, I suggest downloading a Java JDK, configuring ColdFusion to use it (i.e. jvmhome in jvm.config file), and passing parameters to enable a JMX connection. You use this JMX connection to monitor ColdFusion using Visual VM, which comes with the JDK. From there you can generate a heap dump file and/or tell the VM to generate one on OOM. Tehn I've had very good success running that through the Eclipse Memory Analyzer Tool, which has a suspected leaks report that more than once has helped track down the root cause of server OOM crashes.

If that is not your scenario then I suggest enabling snapshots if you're using ColdFusion enterprise, otherwise cfstat is you friend on standard. For either one, you can also setup probes that send a notification when the server is running slowly. This can help you connect to the server in question and generate a dump at the appropriate time or identify if the problem is load related instead.

This may not be your answer, but I use this often to help the garbage collection in JVM memory.

Set this as a scheduled task to run every 5 minutes, and i never get jvm memmory problems anymore.

<cfparam name="url.maxused" default="999">
<cfparam name="url.minfree" default="300">

<cfif NOT isDefined("runtime")>
    <Cfset runtime = CreateObject("java","java.lang.Runtime").getRuntime()>
</cfif>

<cfset fm = runtime.freememory()/>
<Cfset fm = int((fm/1024)/1024)/>
<cfset usedmem = 1270-fm/>
<cfoutput>
 #Now()#<br>
 Before<br>
 Free: #fm# megs<br>
 Used: #usedmem# megs<br>
</cfoutput>
<br>
<!--- check if we are using too much memory --->
<cfif usedmem gt url.maxused or fm lt url.minfree>
 <cfset runtime.gc()>
 Released Memory<br>
<cfelse>
 No need to release memory using the thresholds you provided<br>
</cfif>
<br>
<cfset fm = runtime.freememory()/>
<Cfset fm = int((fm/1024)/1024)/>
<cfset usedmem = 1270-fm/>
<cfoutput>
After<br>
Free: #fm# megs<br>
Used: #usedmem# megs<br>
</cfoutput>

This has been hanging around unanswered for an age, so I thought I'd help getting it cleared up.

First, this: "Server Error The server encountered an internal error and was unable to complete your request. Could not connect to JRun Server."

This is NOT an application timeout, this is just the server becoming unresponsive, or running out of memory, or just encountering something it doesn't like. But it's nothing to do with the application timing out.

The application times out when there's been no activity (ie: no page requests... no visitors) on that site for longer than the application timeout period, which by default is two days (or whatever you have set in your Application.cfc).

Now... I can understand why you might want to recover if your server becomes unresponsive, bur you're approaching this from the wrong angle. Intrinsically if the server ain't working, you can't use that server to do anything (like cure itself)! What is generally done here is that some other process checks that the server is responsive, and if that service determines the server is not responsive, issues a restart.

So you should look at some other software which can perform an HTTP request to your CF server, and if the reaction to the HTTP request suggests the CF server is unresponsive, then the monitoring software tells CF to restart.

To add to the answer by Stephen Moretti and may be the possible solution you were looking for to your interesting question above:

Will the OnApplicationEnd run CFRestart BAT file when application is timeout?

The straight answer is no. Since the OnApplicationEnd() event is a part of the Application's life cycle, so when the application itself is timed out, there is no event that will be called here. This must be clear.

Getting straight to your question though, yes, you can make a custom script file run on the event of an application timeout or end (whatever is the case). You will have to deal with the Serve Scope here.

First up, the application doesn't timeout, a page request does. On request timeout the onApplicationEnd() function is not called. That is only called if the application is shutting down. Here is some info on the CF application life cycle.

Second, in my experience, restarting application servers for whatever reason is probably masking your real problem. If you application is running slow / crashing etc. then I suggest you look into the real reason this is happening rather than restarting it.

However, I can't think of a reason this would not work in principle, but I would suggest you conduct a quick test if this really is what you wish to do.

Hope that helps.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top