Skip to content

Commit c12ce1b

Browse files
author
Rafael Grigorian
committed
Fixed #26
1 parent b200da2 commit c12ce1b

File tree

4 files changed

+75
-3
lines changed

4 files changed

+75
-3
lines changed

src/app/code/JetRails/Varnish/Helper/Purger.php

Lines changed: 42 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,45 @@ public function __construct (
5050
$this->_urlFinder = $urlFinder;
5151
}
5252

53+
/**
54+
* This method simply traverses all the configured Varnish servers and attempts to connect with
55+
* them. If they are ALL healthy then an empty array is returned.
56+
* @return array Collection of errors
57+
*/
58+
public function isConfiguredServersHealthy () {
59+
// Collection of messages
60+
$responses = [];
61+
// Traverse through all the configured servers
62+
foreach ( $this->_data->getVarnishServersWithPorts () as $varnishServer ) {
63+
// Initialize a curl object
64+
$handle = curl_init ( $varnishServer->host );
65+
// Set curl options
66+
curl_setopt ( $handle, CURLOPT_PORT, $varnishServer->port );
67+
curl_setopt ( $handle, CURLOPT_FOLLOWLOCATION, true );
68+
curl_setopt ( $handle, CURLOPT_RETURNTRANSFER, false );
69+
curl_setopt ( $handle, CURLOPT_AUTOREFERER, true );
70+
curl_setopt ( $handle, CURLOPT_HEADER, true );
71+
curl_setopt ( $handle, CURLOPT_CONNECTTIMEOUT, 3 );
72+
curl_setopt ( $handle, CURLOPT_TIMEOUT, 3 );
73+
curl_setopt ( $handle, CURLOPT_MAXREDIRS, 3 );
74+
curl_setopt ( $handle, CURLOPT_CUSTOMREQUEST, "HEAD" );
75+
// Execute curl request and save response code
76+
$response = curl_exec ( $handle );
77+
$responseCode = curl_getinfo ( $handle, CURLINFO_HTTP_CODE );
78+
// Close curl request using handle and return response code
79+
curl_close ( $handle );
80+
// Return unhealthy if unreachable
81+
if ( $responseCode === 0 ) {
82+
array_push (
83+
$responses,
84+
$varnishServer->host . ":" . $varnishServer->port
85+
);
86+
}
87+
}
88+
// Return error responses
89+
return $responses;
90+
}
91+
5392
/**
5493
* This is a helper method that helps resolve url rewrites. It looks for all url rewrites with a
5594
* given target path. It then uses this target path to find all request paths that lead to said
@@ -98,9 +137,9 @@ private function _purge ( $url, $additionalHeaders = [] ) {
98137
curl_setopt ( $handle, CURLOPT_RETURNTRANSFER, true );
99138
curl_setopt ( $handle, CURLOPT_AUTOREFERER, true );
100139
curl_setopt ( $handle, CURLOPT_HEADER, true );
101-
curl_setopt ( $handle, CURLOPT_CONNECTTIMEOUT, 120 );
102-
curl_setopt ( $handle, CURLOPT_TIMEOUT, 120 );
103-
curl_setopt ( $handle, CURLOPT_MAXREDIRS, 10 );
140+
curl_setopt ( $handle, CURLOPT_CONNECTTIMEOUT, 10 );
141+
curl_setopt ( $handle, CURLOPT_TIMEOUT, 10 );
142+
curl_setopt ( $handle, CURLOPT_MAXREDIRS, 3 );
104143
curl_setopt ( $handle, CURLOPT_CUSTOMREQUEST, "PURGE" );
105144
curl_setopt ( $handle, CURLOPT_HTTPHEADER, $additionalHeaders );
106145
// Execute curl request and save response code

src/app/code/JetRails/Varnish/Observer/AutoPurge/Category.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,17 @@ class Category extends AutoPurge {
2828
public function execute ( Observer $observer ) {
2929
// Check to see if option is enabled
3030
if ( $this->_data->isEnabled () && $this->_data->shouldPurgeAfterCategorySave () ) {
31+
// Check to see that all varnish servers are reachable
32+
$unhealthyVarnishServers = $this->_purger->isConfiguredServersHealthy ();
33+
if ( count ( $unhealthyVarnishServers ) > 0 ) {
34+
$this->_messageManager->addError (
35+
"<font color='#E22626' ><b>" .
36+
"Unreachable Varnish Server(s): " .
37+
"</b></font>" .
38+
implode ( $unhealthyVarnishServers, "," )
39+
);
40+
return;
41+
}
3142
// Get id and purge all urls related to route
3243
$cid = $observer->getCategory ()->getId ();
3344
if ( $cid !== null ) {

src/app/code/JetRails/Varnish/Observer/AutoPurge/Page.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ class Page extends AutoPurge {
2727
public function execute ( Observer $observer ) {
2828
// Check to see if event is enabled
2929
if ( $this->_data->isEnabled () && $this->_data->shouldPurgeAfterCmsPageSave () ) {
30+
// Check to see that all varnish servers are reachable
31+
$unhealthyVarnishServers = $this->_purger->isConfiguredServersHealthy ();
32+
if ( count ( $unhealthyVarnishServers ) > 0 ) {
33+
$this->_messageManager->addError (
34+
"<font color='#E22626' ><b>" .
35+
"Unreachable Varnish Server(s): " .
36+
"</b></font>" .
37+
implode ( $unhealthyVarnishServers, "," )
38+
);
39+
return;
40+
}
3041
// Get id and purge all urls related to route
3142
$pid = $observer->getPage ()->getId ();
3243
if ( $pid !== null ) {

src/app/code/JetRails/Varnish/Observer/AutoPurge/Product.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,17 @@ class Product extends AutoPurge {
2727
public function execute ( Observer $observer ) {
2828
// Check to see if event is enabled
2929
if ( $this->_data->isEnabled () && $this->_data->shouldPurgeAfterProductSave () ) {
30+
// Check to see that all varnish servers are reachable
31+
$unhealthyVarnishServers = $this->_purger->isConfiguredServersHealthy ();
32+
if ( count ( $unhealthyVarnishServers ) > 0 ) {
33+
$this->_messageManager->addError (
34+
"<font color='#E22626' ><b>" .
35+
"Unreachable Varnish Server(s): " .
36+
"</b></font>" .
37+
implode ( $unhealthyVarnishServers, "," )
38+
);
39+
return;
40+
}
3041
// Get id and purge all urls related to route
3142
$pid = $observer->getProduct ()->getId ();
3243
if ( $pid !== null ) {

0 commit comments

Comments
 (0)