1

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?

Community
  • 1
  • 1
PPShein
  • 13,309
  • 42
  • 142
  • 227
  • +1 for having an interesting idea in terms of how to use CF functionality to solve your problem. – orangepips Feb 17 '11 at 11:46
  • Can you provide any details of the exception you encounter? Not sure what you mean by "application timeout". Is it a request timeout? Or is it more memory related as @orangepips suggests? – JhnSctt Feb 17 '11 at 21:13
  • Yap. Timeout means JRun isn't working anymore and error shown like that "Server Error The server encountered an internal error and was unable to complete your request. Could not connect to JRun Server" At that time, we need to restart CF Server from Window services. – PPShein Feb 18 '11 at 01:02
  • review the server logs, if JRun has stopped you're probably getting an OOM error, which will probably show up in exception.log. – orangepips Feb 18 '11 at 14:04

6 Answers6

4

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.

Stephen Moretti
  • 2,442
  • 1
  • 18
  • 29
  • It's because of our server is timeout (because of low hardware spec:) and need to restart CF in window services. That's why I want to prevent this issue. – PPShein Feb 17 '11 at 10:26
  • ah that timeout is not an application timeout. You need to look at what orangepips has said. Also, you could simply set up a nightly scheduled task to restart your server. – Stephen Moretti Feb 17 '11 at 14:30
  • "Server Error The server encountered an internal error and was unable to complete your request. Could not connect to JRun Server." This error occurring when so many users hit our website. If we setup the night restart schedule, no user can access our website while restarting. – PPShein Feb 18 '11 at 01:06
  • Better to pick a quiet time and restart the server, minimising down time than to try this and kill the server completely. Though you really should find out what the source problem is instead of trying to elastaplast it. – Stephen Moretti Feb 18 '11 at 16:18
4

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.

orangepips
  • 9,891
  • 6
  • 33
  • 57
1

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>
cfEngineers
  • 664
  • 6
  • 16
  • Yap, thanks. JVM memory is because of the low of server specs: and much loaded amount of data. I think your coding will be helpful. – PPShein Mar 01 '11 at 07:46
0

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.

Anurag
  • 1,018
  • 1
  • 14
  • 36
0

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.

Adam Cameron
  • 29,677
  • 4
  • 37
  • 78
-1

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.

Ciaran Archer
  • 12,316
  • 9
  • 38
  • 55
  • 1
    Applications can timeout. That's what the application timeout value is for. However, it doesn't happen very often, especially if you have an active application. – Stephen Moretti Feb 17 '11 at 10:26
  • @Stephen Moretti: You live and you learn :) We don't have timeout values on our application so I stand corrected there. – Ciaran Archer Feb 17 '11 at 12:04
  • 2
    @Ciaran if you don't specify the timeout in your Application.cfc the default timeout that is set in the administrator is used (which is 2 hours I believe). So your application does have a timeout, but you probably don't realize it and/or never hit it. – Todd Sharp Feb 17 '11 at 14:13
  • +1 I'm not sure @ppshein actually means "application timeout" since I'm not sure why that would call for a service restart. When the application scope times out, the only issue is that anything stored there is lost. I suspect the actula issue is a request timeout as @Ciaran suggest or more likely a bigger issue as @orangepips suggests. – JhnSctt Feb 17 '11 at 21:17
  • "Server Error The server encountered an internal error and was unable to complete your request. Could not connect to JRun Server." This error is occurring when so many users hit the our website. That's why I need to implement what I said. – PPShein Feb 18 '11 at 01:04
  • @Todd Sharp - your right. This is something we never really used / needed and you are right, a timeout applies in CF admin, but it's two days by default. @ppshien - you appear to have an underlying issue here and a server restart is really not sustainable. I still think you should try and find the real issue. – Ciaran Archer Feb 18 '11 at 12:28