Skip to content

Commit

Permalink
Merge pull request #38 from Automattic/fix/ignore-for-jetpack-requests
Browse files Browse the repository at this point in the history
Don't send 503 on requests coming from Jetpack
  • Loading branch information
rinatkhaziev authored Aug 8, 2019
2 parents a6646c2 + 2f7e7cf commit e2f463d
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 13 deletions.
2 changes: 1 addition & 1 deletion maintenance-mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
* Description: Shut down your site for a little while and do some maintenance on it!
* Author: Automattic / WordPress.com VIP
* Author URI: https://vip.wordpress.com
* Version: 0.2.1
* Version: 0.2.2
* License: GPLv2
* Text Domain: maintenance-mode
* Domain Path: /languages
Expand Down
7 changes: 5 additions & 2 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
Contributors: wpcomvip, automattic, benoitchantre, emrikol, philipjohn
Tags: maintenance-mode maintenance
Requires at least: 4.6
Tested up to: 5.0
Stable tag: 0.2.1
Tested up to: 5.2.2
Stable tag: 0.2.2
License: GPLv2 or later
License URI: https://www.gnu.org/licenses/gpl-2.0.html

Expand All @@ -25,6 +25,9 @@ Usage:

== Changelog ==

= 0.2.2 =
* Stop returning a 503 to Jetpack requests to prevent broken connection verification

= 0.2.1 =
* Stop returning a 503 to Nagios on WPCom and VipGo to prevent alerting as a server error

Expand Down
17 changes: 15 additions & 2 deletions template-maintenance-mode.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,23 @@
<head>
<meta charset="<?php bloginfo( 'charset' ); ?>">
<?php wp_head(); // allow for remote-login on mapped domains ?>
<style>
.mm-wrapper {
display: flex;
width: 100vw;
height: 100vh;
justify-content: center;
align-items: center;
flex-flow: column wrap;
font-size: 2rem;
}
</style>
</head>
<body>
<h1><?php esc_html_e( 'Howdy!', 'maintenance-mode' ); ?></h1>
<p><?php esc_html_e( 'We\'re just freshening things up a bit; back in a few!', 'maintenance-mode' ); ?></p>
<div class="mm-wrapper">
<h1><?php esc_html_e( 'Howdy!', 'maintenance-mode' ); ?></h1>
<p><?php esc_html_e( 'We\'re just freshening things up a bit; back in a few!', 'maintenance-mode' ); ?></p>
</div>
<?php wp_footer(); ?>
</body>
</html>
17 changes: 9 additions & 8 deletions vipgo-helper.php
Original file line number Diff line number Diff line change
@@ -1,25 +1,26 @@
<?php

/**
* Prevent the maintenance_mode plugin returning a 503 HTTP status to Nagios.
* Prevent the Maintenance Mode plugin returning a 503 HTTP status to Nagios and Jetpack.
*
* Maintenance_mode sets a 503 header on page requests if maintenance_mode is enabled and this leads to Nagios
* reporting lots of server errors for sites that are just in maintenance_mode. This function sets the filter
* response that maintenance_mode uses to determine if it shoudl set teh 503 status header or not.
* Maintenance Mode sets a 503 header on page requests if Maintenance Mode is enabled and this leads to Nagios
* reporting lots of server errors and Jetpack not being able to verify connection status for sites that are just in maintenance_mode. This function sets the filter
* response that Maintenance Mode uses to determine if it should set the 503 status header or not.
*
* @return bool Should maintenance_mode set a 503 header
* @return bool Should Maintenance Mode set a 503 header
*/
function wpcom_vip_maintenance_mode_do_not_respond_503_for_nagios( $should_set_503 ) {
function wpcom_vip_maintenance_mode_do_not_respond_503_for_services( $should_set_503 ): bool {
$user_agent = ! empty( $_SERVER['HTTP_USER_AGENT'] ) ? $_SERVER['HTTP_USER_AGENT'] : '';

// The request comes from Nagios so deny the 503 header being set.
// Desktop checks use something like `check_http/v2.2.1 (nagios-plugins 2.2.1)`.
// Mobile checks use `iphone`.
if ( false !== strpos( $user_agent, 'check_http' ) || 'iphone' === $user_agent ) {
// Utilize helper function vip_is_jetpack_request if available
if ( false !== strpos( $user_agent, 'check_http' ) || 'iphone' === $user_agent || ( function_exists( 'vip_is_jetpack_request' ) && vip_is_jetpack_request() ) ) {
return false;
}

return $should_set_503;
}

add_filter( 'vip_maintenance_mode_respond_503', 'wpcom_vip_maintenance_mode_do_not_respond_503_for_nagios', 30 );
add_filter( 'vip_maintenance_mode_respond_503', 'wpcom_vip_maintenance_mode_do_not_respond_503_for_services', 30 );

0 comments on commit e2f463d

Please sign in to comment.