Category Archives: coding tips

Freezing curl causing a lot of troubles

Not-so-infinite loops

Our website monitoring mechanism is actually quite simple.  We use curl to make a connection with the website and then we grab some data to check if your site is up or down (actually, curl has more power and we use it in our new features we develop right now as well).

To make sure we are able to test your site in almost equal time intervals, we need to have a couple of testing agents working at the same time.

The job of these agents is to sit and wait for available tasks for some period of time. If there are any, agent reserves one of them and does the actual website uptime test.

However, since we started the Ukrainian and Russian government websites monitoring site (lots of downtimes), we found out that some of the scripts sometimes freeze for no good reason.

It wasn’t a serious problem, because it didn’t influence the uptime monitoring service reliability. However, the zombie agents were taking our precious server resources to do nothing.

Curl has two timeout options

And then, there comes the time when we decided to fix this issue. While debugging we found out that the solution is quite trivial.

As you probably know, we assumed that if your site is not responding in 10 seconds after our call, it means that the website is down. Nobody is waiting 10 seconds for first document request anyway. Even if your server works, the user nearby our agent’s server location might not be able to notice this.

These 10 seconds of timeout are set as a curl option (CURLOPT_CONNECTTIMEOUT). It means “if the server is not responding in X seconds, do not wait more and return connection error [http 0]”.

What we didn’t notice is the other curl option – CURLOPT_TIMEOUT and it was the cause of our problems. The option is more important in this case since it forbids curl to freeze. Connection timeout is related to the connection only. If connection is made quickly, the clock for the whole script is not ticking any more.

Summary (tl;dr)

If you plan to use a curl mechanism inside a do-while loop with iteration limit, remember to set a curlopt_timeout or your loop won’t die sometimes.

 

 

Tutorial for custom maintenance mode in WordPress

Maintenance mode in WordPress

Maintenance mode is the “be right back” state of your website.  You should use it any time you are changing the WordPress code or making anything else with a non-zero possibility to fail and affect a user experience.

You’ve probably noticed that WordPress is actually turning the built-in maintenance mode while upgrading the WP core, or adding new plugins.

In this article, you’ll learn how it works, how to turn the maintenance mode on manually, and how to make it look better.

How the built-in maintenance mode works

When you click the “upgrade WordPress” button,  Wordpress is inserting the .maintenance php file into the site’s root directory with $upgrading variable set to time().

Wordpress maintenance mode function

Every single time someone enters your site, WordPress is looking for this particular file and if it exists, WordPress is comparing the $upgrading variable with the time() value. If the difference is less than 600 seconds (10 minutes), the maintenance mode function is looking for a custom maintenance.php inside wp-content directory.

If it exists, WordPress loads that file and exits the compilation process.

If not, the maintenance function compiles the built-in maintenance landing page and ends with the compilation process. What’s important, WordPress handles the maintenance mode well with a proper http code sending to a user.

Find out why the proper http code is important.

How to make a custom maintenance mode landing page?

To make a custom maintenance mode landing page, you just need to make your own php script and save it into wp-content directory as maintenance.php file. Every time your site is in the maintenance mode, WordPress will look for this file and compile it, ignoring the built-in landing page.

To make it easier for you, we’ve made an example script. You can grab it from our GitHub repository and customise it any way you like.

How to turn on the maintenance mode manually?

If you would like to turn on the maintenance mode manually, without any plugin you just need to insert the .maintenance file into your WordPress main folder.

We made the file for you, extending it with an IP white list and secret key white list support. You can grab it from our GitHub repo as well as the maintenance.php example file.