Skip to content

Commit c833081

Browse files
author
Rafael Grigorian
committed
Finished but not commented
1 parent e6e8486 commit c833081

File tree

18 files changed

+178
-127
lines changed

18 files changed

+178
-127
lines changed

README.md

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# varnish
22
Magento 2 - Manage Varnish Cache
33

4-
sudo varnishd -a 127.0.0.1:80 -T 127.0.0.1:6082 -f /usr/local/etc/varnish/default.vcl -s file,/tmp,500M
5-
sudo apachectl restart
6-
sudo pkill varnishd
4+
5+
sudo apachectl restart && sudo pkill varnishd && sleep 2 ; sudo varnishd -a 127.0.0.1:80 -T 127.0.0.1:6082 -f /usr/local/etc/varnish/default.vcl -s file,/tmp,500M
76

87
varnishadm "vcl.load default /usr/local/etc/varnish/default.vcl"
98
varnishadm "vcl.use default"
@@ -23,3 +22,5 @@ primer:queue
2322
primer:queue:show
2423

2524

25+
26+
THings that need to be added, hole punching based on block path
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?php
2+
3+
namespace JetRails\Varnish\Block\Adminhtml\System\Config\Form;
4+
5+
use Magento\Backend\Block\Template\Context;
6+
use Magento\Config\Block\System\Config\Form\Field;
7+
use Magento\Framework\Data\Form\Element\AbstractElement;
8+
9+
class Link extends Field {
10+
11+
protected $_template = "JetRails_Varnish::adminhtml/system/config/form/link.phtml";
12+
13+
protected function _getElementHtml ( AbstractElement $element ) {
14+
return $this->_toHtml ();
15+
}
16+
17+
public function getActionUrl () {
18+
return $this->getUrl ("adminhtml/cache/index");
19+
}
20+
21+
}

src/app/code/JetRails/Varnish/Console/Command/AbstractCommand.php

Lines changed: 30 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -18,58 +18,60 @@ abstract class AbstractCommand extends Command {
1818

1919
protected $_purger;
2020

21+
protected $_runIfDisabled = true;
22+
2123
public function __construct ( Data $data, Logger $logger, Purger $purger ) {
24+
parent::__construct ();
2225
$this->_data = $data;
2326
$this->_logger = $logger;
2427
$this->_purger = $purger;
25-
parent::__construct ();
2628
}
2729

2830
private function _printLabel ( $output, $label, $value ) {
29-
$message = sprintf ( "<fg=cyan>%-16s%s</>", "$label", $value );
31+
$message = sprintf ( "<fg=white>%-16s%s</>", "$label:", $value );
3032
$output->writeln ( $message );
3133
}
3234

3335
private function _printLine ( $output, $label, $value ) {
34-
$message = sprintf ( "<fg=yellow>%-16s</>%s", "$label", $value );
36+
$message = sprintf ( "<fg=cyan>%-16s</>%s", "$label:", $value );
3537
$output->writeln ( $message );
3638
}
3739

3840
protected function execute ( InputInterface $input, OutputInterface $output ) {
41+
// Print out the header message
3942
$output->writeln ("");
4043
$this->_printLabel ( $output, "Powered By", "The JetRails Team" );
4144
$this->_printLabel ( $output, "Email Us", "[email protected]" );
4245
$this->_printLabel ( $output, "Call Us", "+1 (888) 554-9990" );
4346
$output->writeln ("");
44-
45-
$purgeCommand = preg_match ( "/^varnish:purge/", $this->getName () );
46-
47-
if ( $purgeCommand && !$this->_data->isEnabled () ) {
48-
$output->writeln (
49-
"Cache application must be set to <options=underscore>Varnish Cache</>\n" .
50-
"<fg=yellow>Backend</>: Stores → Advanced → Developer → System → Full Page Cache → Caching Application\n" .
51-
"<fg=yellow>Console</>: varnish:status:set enable\n"
52-
);
47+
// Check to see if we should run the command if feature is disabled
48+
if ( !$this->_runIfDisabled && !$this->_data->isEnabled () ) {
49+
$response = ( object ) [
50+
"status" => "unsuccessful",
51+
"message" => "Cache application must be set to <options=underscore>Varnish Cache</>",
52+
"payload" => [ "Run '<fg=red>varnish:status:set enable</>' to set varnish cache as caching application" ]
53+
];
5354
}
5455
else {
55-
56+
// Run the command and reformat the status of the response
5657
$response = ( object ) $this->runCommand ( $input );
57-
58-
$status = $response->status ? "successful" : ( $response->status === null ? "mixed" : "unsuccessful" );
59-
$parameters = $input->getArguments ();
60-
unset ( $parameters ["command"] );
61-
$parametersKeys = array_keys ( $parameters );
62-
$parametersKeys = array_map ( function ( $i ) use ( $parameters ) { return "$i" . $parameters [ $i ]; }, $parametersKeys );
63-
$parameter = $parametersKeys;
64-
$parameters = count ( $parameters ) == 0 ? "none" : implode ( ", ", $parameter );
65-
66-
$this->_printLine ( $output, "Command", $this->getName () );
67-
$this->_printLine ( $output, "Parameters", $parameters );
68-
$this->_printLine ( $output, "Status", $status );
69-
$this->_printLine ( $output, "Response", $response->message );
70-
if ( isset ( $response->payload ) ) $output->writeln ( "\n" . implode ( "\n", $response->payload ) );
71-
$output->writeln ("");
58+
$response->status = $response->status ? "successful" : ( $response->status === null ? "mixed" : "unsuccessful" );
7259
}
60+
// Format the parameters
61+
$parameters = $input->getArguments ();
62+
unset ( $parameters ["command"] );
63+
$parametersKeys = array_keys ( $parameters );
64+
$parametersKeys = array_map ( function ( $i ) use ( $parameters ) { return "$i" . ( $parameters [ $i ] == "" ? "null" : $parameters [ $i ] ); }, $parametersKeys );
65+
$parameter = $parametersKeys;
66+
$parameters = count ( $parameters ) == 0 ? "none" : implode ( ", ", $parameter );
67+
// Print out results
68+
$this->_printLine ( $output, "Command", $this->getName () );
69+
$this->_printLine ( $output, "Parameters", $parameters );
70+
$this->_printLine ( $output, "Status", $response->status );
71+
$this->_printLine ( $output, "Response", $response->message );
72+
// If there is a payload, then print it out
73+
if ( property_exists ( $response , "payload" ) && count ( $response->payload ) > 0 ) $output->writeln ( "\n" . implode ( "\n", $response->payload ) );
74+
$output->writeln ("");
7375
}
7476

7577
protected abstract function runCommand ( InputInterface $input );

src/app/code/JetRails/Varnish/Console/Command/Config/Show.php

Lines changed: 0 additions & 30 deletions
This file was deleted.

src/app/code/JetRails/Varnish/Console/Command/Purge/All.php

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,19 @@
88

99
class All extends AbstractCommand {
1010

11+
protected $_runIfDisabled = false;
12+
1113
protected function configure () {
1214
// Register the command and set the arguments
1315
$this->setName ("varnish:purge:all")
14-
->setDescription ("Purge all cache from Varnish servers");
16+
->setDescription ("Purge all cache from varnish servers");
1517
}
1618

17-
protected function runCommand ( InputInterface $input, OutputInterface $output ) {
19+
protected function runCommand ( InputInterface $input ) {
20+
// Initialize the accounting variables and payload array
21+
$total = 0;
22+
$success = 0;
23+
$payload = [];
1824
// Ask to purge and iterate over responses
1925
foreach ( $this->_purger->purgeAll () as $response ) {
2026
// Log what we are trying to do
@@ -28,17 +34,25 @@ protected function runCommand ( InputInterface $input, OutputInterface $output )
2834
if ( $response->status == 200 ) {
2935
// Add success response message
3036
$serverHtml = "<fg=green>$response->server</>";
31-
$msg = "Successfully purged all cache on $serverHtml";
32-
$output->writeln ( $msg );
37+
$msg = "successfully purged all cache on $serverHtml";
38+
array_push ( $payload, $msg );
39+
$success++;
3340
}
3441
else {
3542
// Otherwise add an error message
3643
$serverHtml = "<fg=red>$response->server</>";
3744
$statusHtml = "<fg=red>$response->status</>";
3845
$msg = "Error Purging all cache on $serverHtml with response code $statusHtml";
39-
$output->writeln ( $msg );
46+
array_push ( $payload, $msg );
4047
}
48+
$total++;
4149
}
50+
// Return every intermediate result as one
51+
return [
52+
"status" => $success > 0 && $total - $success > 0 ? null : $total == $success,
53+
"message" => "purged all cache from $success/$total varnish servers",
54+
"payload" => $payload
55+
];
4256
}
4357

4458
}

src/app/code/JetRails/Varnish/Console/Command/Purge/Store.php

Lines changed: 53 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
class Store extends AbstractCommand {
1111

12+
protected $_runIfDisabled = false;
13+
1214
private function _findStoreViewById ( $storeViews, $id ) {
1315
foreach ( $storeViews as $storeView ) {
1416
if ( $storeView->id == $id ) {
@@ -18,40 +20,46 @@ private function _findStoreViewById ( $storeViews, $id ) {
1820
return false;
1921
}
2022

21-
protected function configure () {
22-
// Register the command and set the arguments
23-
$this->setName ("varnish:purge:store")
24-
->setDescription ("Purge varnish cache based on store view")
25-
->addArgument ( "store", InputArgument::OPTIONAL, "Store view id to purge" );
26-
}
23+
protected function configure () {
24+
// Register the command and set the arguments
25+
$this->setName ("varnish:purge:store")
26+
->setDescription ("Purge varnish cache based on store view")
27+
->addArgument ( "store", InputArgument::OPTIONAL, "Store view id to purge" );
28+
}
2729

28-
protected function runCommand ( InputInterface $input, OutputInterface $output ) {
29-
$store = $input->getArgument ("store");
30-
$storeViews = $this->_data->getStoreViews ();
31-
if ( !$store ) {
32-
$output->writeln ("");
33-
$header = sprintf (
34-
"<options=bold>%-8s</><options=bold>%-35s</> <options=bold>%-35s</>",
35-
"ID",
36-
"Store View Name",
37-
"Store View Base Url"
38-
);
39-
$output->writeln ( $header );
40-
foreach ( $storeViews as $storeView ) {
41-
$msg = sprintf (
42-
"<fg=green>%-8s</>%-35s %-35s",
43-
$storeView->id,
44-
$storeView->name,
45-
$storeView->url
46-
);
47-
$output->writeln ( $msg );
48-
}
49-
$output->writeln ("");
50-
}
51-
else if ( $storeView = $this->_findStoreViewById ( $storeViews, $store ) ) {
30+
protected function runCommand ( InputInterface $input ) {
31+
$store = $input->getArgument ("store");
32+
$storeViews = $this->_data->getStoreViews ();
33+
if ( !$store ) {
34+
$payload = [ sprintf (
35+
"<options=bold>%-16s</><options=bold>%-35s</> <options=bold>%-35s</>",
36+
"ID",
37+
"Store View Name",
38+
"Store View Base Url"
39+
)];
40+
foreach ( $storeViews as $storeView ) {
41+
$msg = sprintf (
42+
"<fg=green>%-16s</>%-35s %-35s",
43+
$storeView->id,
44+
$storeView->name,
45+
$storeView->url
46+
);
47+
array_push ( $payload, $msg );
48+
}
49+
return [
50+
"status" => false,
51+
"message" => "please pass store view id as parameter, store views are below:",
52+
"payload" => $payload
53+
];
54+
}
55+
else if ( $storeView = $this->_findStoreViewById ( $storeViews, $store ) ) {
5256
// Make sure store id is valid
5357
$url = $this->_purger->validateAndResolveStoreId ( $store );
5458
if ( gettype ( $url ) == "object" ) {
59+
// Initialize the accounting variables and payload array
60+
$total = 0;
61+
$success = 0;
62+
$payload = [];
5563
// Ask to purge and iterate over responses
5664
foreach ( $this->_purger->purgeStore ( $url ) as $response ) {
5765
// Log what we are trying to do
@@ -68,22 +76,30 @@ protected function runCommand ( InputInterface $input, OutputInterface $output )
6876
$storeHtml = "<fg=green>$response->target</>";
6977
$serverHtml = "<fg=green>$response->server</>";
7078
$msg = "Successfully purged store view $storeHtml on $serverHtml";
71-
$output->writeln ( $msg );
79+
array_push ( $payload, $msg );
80+
$success++;
7281
}
7382
else {
7483
// Otherwise add an error message
7584
$storeHtml = "<fg=red>$response->target</>";
7685
$serverHtml = "<fg=red>$response->server</>";
7786
$statusHtml = "<fg=red>$response->status</>";
7887
$msg = "Error Purging store view $storeHtml on $serverHtml with response code $statusHtml";
79-
$output->writeln ( $msg );
88+
array_push ( $payload, $msg );
8089
}
90+
// Increment the total accounting variable
91+
$total++;
8192
}
93+
// Return every intermediate result as one
94+
return [
95+
"status" => $success > 0 && $total - $success > 0 ? null : $total == $success,
96+
"message" => "purged store view cache from $success/$total varnish servers",
97+
"payload" => $payload
98+
];
8299
}
83-
}
84-
else {
85-
$output->writeln ("\n<fg=red>Error</>: Could not find store view with id $store\n");
86-
}
87-
}
100+
}
101+
// Return an error message
102+
return [ "status" => false, "message" => "could not find specified store view" ];
103+
}
88104

89105
}

src/app/code/JetRails/Varnish/Console/Command/Purge/Url.php

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@
99

1010
class Url extends AbstractCommand {
1111

12+
protected $_runIfDisabled = false;
13+
1214
protected function configure () {
1315
// Register the command and set the arguments
1416
$this->setName ("varnish:purge:url")
15-
->setDescription ("Purge specific url from Varnish cache")
17+
->setDescription ("Purge specific url from varnish cache")
1618
->addArgument ( "url", InputArgument::REQUIRED, "What URL do you want to purge?" );
1719
}
1820

@@ -22,10 +24,10 @@ protected function runCommand ( InputInterface $input ) {
2224
$url = $this->_purger->validateUrl ( $url );
2325
// If an object was returned, then it was a valid url
2426
if ( gettype ( $url ) == "object" ) {
27+
// Initialize the accounting variables and payload array
2528
$total = 0;
2629
$success = 0;
2730
$payload = [];
28-
2931
// Ask to purge and iterate over responses
3032
foreach ( $this->_purger->purgeUrl ( $url ) as $response ) {
3133
// Log what we are trying to do
@@ -41,7 +43,7 @@ protected function runCommand ( InputInterface $input ) {
4143
// Add success response message
4244
$targetHtml = "<fg=green>$response->target</>";
4345
$serverHtml = "<fg=green>$response->server</>";
44-
$message = "<bg=green> </>\n<bg=green;fg=white;options=bold> SUCCESS </> purged url $targetHtml on $serverHtml\n<bg=green> </>";
46+
$message = "successfully purged url $targetHtml on $serverHtml";
4547
array_push ( $payload, $message );
4648
$success++;
4749
}
@@ -50,12 +52,18 @@ protected function runCommand ( InputInterface $input ) {
5052
$targetHtml = "<fg=red>$response->target</>";
5153
$serverHtml = "<fg=red>$response->server</>";
5254
$statusHtml = "<fg=red>$response->status</>";
53-
$message = "<bg=red> </>\n<bg=red;fg=white;options=bold> ERROR </> couldn't purge url $targetHtml on $serverHtml with response code $statusHtml\n<bg=red> </>";
55+
$message = "couldn't purge url $targetHtml on $serverHtml ";
56+
$message .= "with response code $statusHtml";
5457
array_push ( $payload, $message );
5558
}
5659
$total++;
5760
}
58-
return [ "status" => $success > 0 && $total - $success > 0 ? null : $total == $success, "message" => "purged url from $success/$total varnish servers", "payload" => $payload ];
61+
// Return every intermediate result as one
62+
return [
63+
"status" => $success > 0 && $total - $success > 0 ? null : $total == $success,
64+
"message" => "purged url from $success/$total varnish servers",
65+
"payload" => $payload
66+
];
5967
}
6068
// Otherwise an error was returned in the form of a string
6169
return [ "status" => false, "message" => $url ];

0 commit comments

Comments
 (0)