Enterprise_SnsServer.php
3.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
<?php
namespace W3TC;
/**
* Purge using AmazonSNS object
*/
require_once W3TC_LIB_DIR . '/SNS/services/MessageValidator/Message.php';
require_once W3TC_LIB_DIR . '/SNS/services/MessageValidator/MessageValidator.php';
/**
* class Sns
*/
class Enterprise_SnsServer extends Enterprise_SnsBase {
/**
* Processes message from SNS
*
* @throws Exception
*/
function process_message() {
$this->_log( 'Received message' );
try {
$message = \Message::fromRawPostData();
$validator = new \MessageValidator();
$error = '';
if ( $validator->isValid( $message ) ) {
$topic_arn = $this->_config->get_string( 'cluster.messagebus.sns.topic_arn' );
if ( empty( $topic_arn ) || $topic_arn != $message->get( 'TopicArn' ) )
throw new \Exception ( 'Not my Topic. Request came from ' .
$message->get( 'TopicArn' ) );
if ( $message->get( 'Type' ) == 'SubscriptionConfirmation' )
$this->_subscription_confirmation( $message );
elseif ( $message->get( 'Type' ) == 'Notification' )
$this->_notification( $message->get( 'Message' ) );
} else {
$this->_log( 'Error processing message it was not valid.' );
}
} catch ( \Exception $e ) {
$this->_log( 'Error processing message: ' . $e->getMessage() );
}
$this->_log( 'Message processed' );
}
/**
* Confirms subscription
*
* @param Message $message
* @throws Exception
*/
private function _subscription_confirmation( $message ) {
$this->_log( 'Issuing confirm_subscription' );
$response = $this->_get_api()->confirm_subscription(
$topic_arn, $message->get( 'Token' ) );
$this->_log( 'Subscription confirmed: ' .
( $response->isOK() ? 'OK' : 'Error' ) );
}
/**
* Processes notification
*
* @param array $v
*/
private function _notification( $v ) {
$m = json_decode( $v, true );
if ( isset( $m['hostname'] ) )
$this->_log( 'Message originated from hostname: ' . $m['hostname'] );
define( 'DOING_SNS', true );
$this->_log( 'Actions executing' );
do_action( 'w3tc_messagebus_message_received' );
if ( isset( $m['actions'] ) ) {
$actions = $m['actions'];
foreach ( $actions as $action )
$this->_execute( $action );
} else {
$this->_execute( $m['action'] );
}
do_action( 'w3tc_messagebus_message_processed' );
$this->_log( 'Actions executed' );
}
/**
* Execute action
*
* @param unknown $m
* @throws Exception
*/
private function _execute( $m ) {
$action = $m['action'];
$this->_log( 'Executing action ' . $action );
//Needed for cache flushing
$executor = new CacheFlush_Locally();
//Needed for cache cleanup
$pgcache_admin = Dispatcher::component( 'PgCache_Plugin_Admin' );
//See which message we got
if ( $action == 'dbcache_flush' )
$executor->dbcache_flush();
elseif ( $action == 'objectcache_flush' )
$executor->objectcache_flush();
elseif ( $action == 'fragmentcache_flush' )
$executor->fragmentcache_flush();
elseif ( $action == 'fragmentcache_flush_group' )
$executor->fragmentcache_flush_group( $m['group'] );
elseif ( $action == 'minifycache_flush' )
$executor->minifycache_flush();
elseif ( $action == 'browsercache_flush' )
$executor->browsercache_flush();
elseif ( $action == 'cdn_purge_files' )
$executor->cdn_purge_files( $m['purgefiles'] );
elseif ( $action == 'pgcache_cleanup' )
$pgcache_admin->cleanup_local();
elseif ( $action == 'opcache_flush' )
$executor->opcache_flush();
elseif ( $action == 'opcache_flush_file' )
$executor->opcache_flush_file( $m['filename'] );
elseif ( $action == 'flush_all' )
$executor->flush_all(
isset( $m['extras'] ) ? $m['extras'] : null );
elseif ( $action == 'flush_post' )
$executor->flush_post( $m['post_id'] );
elseif ( $action == 'flush_url' )
$executor->flush_url( $m['url'] );
elseif ( $action == 'prime_post' )
$executor->prime_post( $m['post_id'] );
else
throw new \Exception( 'Unknown action ' . $action );
$this->_log( 'succeeded' );
}
}