CGI

Upgrading Xojo CGI Apps

One of the things my customers and I have discovered is that updating Xojo CGI applications can be difficult.

Some people used special key combinations or administrator-only buttons to shut down their application executable. Others just tried to upload the new binary executable right over the old. Of course the FTP may let you do that but the old version is still running because people are still hitting it! Then you encounter “can not launch application on port 1234” for example because the old version of your binary is still running on that port.

So working with our customers we have developed a solution:

1. Add a timer to your WebApplication’s properties.

2. In the App.Open event let’s instantiate that timer and set it to fire every 5 seconds. Let’s also use AddHandler to handle the Timer.Action() event.

3. Let’s create the “timerShutdown_Action” method. This method will be fired every 5 seconds and it will look for the existence of the Appname.cgi. This file is created for you by Xojo when the application is compiled as a Linux CGI. Don't forget to add "sender As Timer" to the parameters or it won't compile!

You can see here we are getting a FolderItem object for the current directory. We are then appending the path of our current directory to the lowercase version of our application’s executable + “.cgi”. So if you name your Xojo WebApplication “TestApp” then when you compiled there was an accessory “testapp.cgi” file created. This is the file that connects your web application to the web server. It is the gate way in which your users use the application.

So we check to see if its still in the root folder sitting next to your application. If it is, great! That means everything is humming along exactly as it should. However if that file were to go missing, the WebApplication would just shut itself down.

4. So to deploy a new version of your Xojo Web Application you just delete the accessory .cgi file for your app, then wait a healthy 5-10 seconds and the app will shut itself down. Replace your files (including the CGI) with your new version and your users will be back in business.

If you attempt to shut down all sessions or shut down the app from inside the application itself you will run into issues. One customer was doing exactly this. They had set the WebApplication.AutoQuit property to true and then would loop through all the sessions performing WebSession.Quit(). This sounds great in theory but the problem is even if all sessions disappeared and the application shutdown, the CGI file sitting next to your application will just launch a new version of it.

So I hope this solution works for you as a quick and easy way to update your Xojo CGI Web Applications.