- Adds new "ping" cron daemon router endpoint
- Removes the requirement to have data payload for router requests
This commit is contained in:
@ -2,7 +2,7 @@
|
|||||||
namespace MailPoet\Cron;
|
namespace MailPoet\Cron;
|
||||||
|
|
||||||
use MailPoet\Models\Setting;
|
use MailPoet\Models\Setting;
|
||||||
use MailPoet\Router\Endpoints\Queue as QueueEndpoint;
|
use MailPoet\Router\Endpoints\CronDaemon as CronDaemonEndpoint;
|
||||||
use MailPoet\Router\Router;
|
use MailPoet\Router\Router;
|
||||||
use MailPoet\Util\Security;
|
use MailPoet\Util\Security;
|
||||||
|
|
||||||
@ -46,11 +46,27 @@ class CronHelper {
|
|||||||
return Security::generateRandomString();
|
return Security::generateRandomString();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static function pingDaemon() {
|
||||||
|
$url = Router::buildRequest(
|
||||||
|
CronDaemonEndpoint::ENDPOINT,
|
||||||
|
CronDaemonEndpoint::ACTION_PING_RESPONSE
|
||||||
|
);
|
||||||
|
$url = str_replace(home_url(), self::getSiteUrl(), $url);
|
||||||
|
$args = array(
|
||||||
|
'blocking' => true,
|
||||||
|
'sslverify' => false,
|
||||||
|
'timeout' => self::DAEMON_REQUEST_TIMEOUT,
|
||||||
|
'user-agent' => 'MailPoet (www.mailpoet.com) Cron'
|
||||||
|
);
|
||||||
|
$result = wp_remote_get($url, $args);
|
||||||
|
return wp_remote_retrieve_body($result) === 'pong';
|
||||||
|
}
|
||||||
|
|
||||||
static function accessDaemon($token, $timeout = self::DAEMON_REQUEST_TIMEOUT) {
|
static function accessDaemon($token, $timeout = self::DAEMON_REQUEST_TIMEOUT) {
|
||||||
$data = array('token' => $token);
|
$data = array('token' => $token);
|
||||||
$url = Router::buildRequest(
|
$url = Router::buildRequest(
|
||||||
QueueEndpoint::ENDPOINT,
|
CronDaemonEndpoint::ENDPOINT,
|
||||||
QueueEndpoint::ACTION_RUN,
|
CronDaemonEndpoint::ACTION_RUN,
|
||||||
$data
|
$data
|
||||||
);
|
);
|
||||||
$url = str_replace(home_url(), self::getSiteUrl(), $url);
|
$url = str_replace(home_url(), self::getSiteUrl(), $url);
|
||||||
|
@ -20,6 +20,10 @@ class Daemon {
|
|||||||
$this->timer = microtime(true);
|
$this->timer = microtime(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function ping() {
|
||||||
|
$this->terminateRequest('pong');
|
||||||
|
}
|
||||||
|
|
||||||
function run() {
|
function run() {
|
||||||
ignore_user_abort(true);
|
ignore_user_abort(true);
|
||||||
if(!$this->request_data) {
|
if(!$this->request_data) {
|
||||||
@ -91,7 +95,7 @@ class Daemon {
|
|||||||
exit;
|
exit;
|
||||||
}
|
}
|
||||||
|
|
||||||
function terminateRequest() {
|
function terminateRequest($message = false) {
|
||||||
exit;
|
die($message);
|
||||||
}
|
}
|
||||||
}
|
}
|
38
lib/Router/Endpoints/CronDaemon.php
Normal file
38
lib/Router/Endpoints/CronDaemon.php
Normal file
@ -0,0 +1,38 @@
|
|||||||
|
<?php
|
||||||
|
namespace MailPoet\Router\Endpoints;
|
||||||
|
|
||||||
|
use MailPoet\Cron\CronHelper;
|
||||||
|
use MailPoet\Cron\Daemon;
|
||||||
|
|
||||||
|
if(!defined('ABSPATH')) exit;
|
||||||
|
|
||||||
|
class CronDaemon {
|
||||||
|
const ENDPOINT = 'cron_daemon';
|
||||||
|
const ACTION_RUN = 'run';
|
||||||
|
const ACTION_PING = 'ping';
|
||||||
|
const ACTION_PING_RESPONSE = 'pingResponse';
|
||||||
|
public $allowed_actions = array(
|
||||||
|
self::ACTION_RUN,
|
||||||
|
self::ACTION_PING,
|
||||||
|
self::ACTION_PING_RESPONSE
|
||||||
|
);
|
||||||
|
public $data;
|
||||||
|
|
||||||
|
function __construct($data) {
|
||||||
|
$this->data = $data;
|
||||||
|
}
|
||||||
|
|
||||||
|
function run() {
|
||||||
|
$queue = new Daemon($this->data);
|
||||||
|
$queue->run();
|
||||||
|
}
|
||||||
|
|
||||||
|
function ping() {
|
||||||
|
die(CronHelper::pingDaemon());
|
||||||
|
}
|
||||||
|
|
||||||
|
function pingResponse() {
|
||||||
|
$queue = new Daemon();
|
||||||
|
$queue->ping();
|
||||||
|
}
|
||||||
|
}
|
@ -1,22 +0,0 @@
|
|||||||
<?php
|
|
||||||
namespace MailPoet\Router\Endpoints;
|
|
||||||
|
|
||||||
use MailPoet\Cron\Daemon;
|
|
||||||
|
|
||||||
if(!defined('ABSPATH')) exit;
|
|
||||||
|
|
||||||
class Queue {
|
|
||||||
const ENDPOINT = 'queue';
|
|
||||||
const ACTION_RUN = 'run';
|
|
||||||
public $allowed_actions = array(self::ACTION_RUN);
|
|
||||||
public $data;
|
|
||||||
|
|
||||||
function __construct($data) {
|
|
||||||
$this->data = $data;
|
|
||||||
}
|
|
||||||
|
|
||||||
function run() {
|
|
||||||
$queue = new Daemon($this->data);
|
|
||||||
$queue->run();
|
|
||||||
}
|
|
||||||
}
|
|
@ -57,14 +57,15 @@ class Router {
|
|||||||
return rtrim(base64_encode(json_encode($data)), '=');
|
return rtrim(base64_encode(json_encode($data)), '=');
|
||||||
}
|
}
|
||||||
|
|
||||||
static function buildRequest($endpoint, $action, $data) {
|
static function buildRequest($endpoint, $action, $data = false) {
|
||||||
$data = self::encodeRequestData($data);
|
|
||||||
$params = array(
|
$params = array(
|
||||||
self::NAME => '',
|
self::NAME => '',
|
||||||
'endpoint' => $endpoint,
|
'endpoint' => $endpoint,
|
||||||
'action' => $action,
|
'action' => $action,
|
||||||
'data' => $data
|
|
||||||
);
|
);
|
||||||
|
if($data) {
|
||||||
|
$params['data'] = self::encodeRequestData($data);
|
||||||
|
}
|
||||||
return add_query_arg($params, home_url());
|
return add_query_arg($params, home_url());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -13,7 +13,7 @@ class CronHelperTest extends MailPoetTest {
|
|||||||
expect(CronHelper::DAEMON_SETTING)->equals('cron_daemon');
|
expect(CronHelper::DAEMON_SETTING)->equals('cron_daemon');
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanCreateDaemon() {
|
function testItCreatesDaemon() {
|
||||||
$token = 'create_token';
|
$token = 'create_token';
|
||||||
$time = time();
|
$time = time();
|
||||||
CronHelper::createDaemon($token);
|
CronHelper::createDaemon($token);
|
||||||
@ -26,7 +26,7 @@ class CronHelperTest extends MailPoetTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanRestartDaemon() {
|
function testItRestartsDaemon() {
|
||||||
$token = 'restart_token';
|
$token = 'restart_token';
|
||||||
$time = time();
|
$time = time();
|
||||||
CronHelper::restartDaemon($token);
|
CronHelper::restartDaemon($token);
|
||||||
@ -39,7 +39,7 @@ class CronHelperTest extends MailPoetTest {
|
|||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItLoadDaemon() {
|
function testItLoadsDaemon() {
|
||||||
$daemon = array(
|
$daemon = array(
|
||||||
'token' => 'some_token',
|
'token' => 'some_token',
|
||||||
'updated_at' => '12345678'
|
'updated_at' => '12345678'
|
||||||
@ -51,7 +51,7 @@ class CronHelperTest extends MailPoetTest {
|
|||||||
expect(CronHelper::getDaemon())->equals($daemon);
|
expect(CronHelper::getDaemon())->equals($daemon);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanSaveDaemon() {
|
function testItSavesDaemon() {
|
||||||
// when saving daemon, 'updated_at' value should change
|
// when saving daemon, 'updated_at' value should change
|
||||||
$daemon = array(
|
$daemon = array(
|
||||||
'token' => 'some_token',
|
'token' => 'some_token',
|
||||||
@ -67,7 +67,7 @@ class CronHelperTest extends MailPoetTest {
|
|||||||
expect(CronHelper::getDaemon())->equals($daemon);
|
expect(CronHelper::getDaemon())->equals($daemon);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanCreateRandomToken() {
|
function testItCreatesRandomToken() {
|
||||||
// random token is a string of 5 characters
|
// random token is a string of 5 characters
|
||||||
$token1 = CronHelper::createToken();
|
$token1 = CronHelper::createToken();
|
||||||
$token2 = CronHelper::createToken();
|
$token2 = CronHelper::createToken();
|
||||||
@ -76,7 +76,7 @@ class CronHelperTest extends MailPoetTest {
|
|||||||
expect(strlen($token1))->equals(5);
|
expect(strlen($token1))->equals(5);
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanGetSiteUrl() {
|
function testItGetsSiteUrl() {
|
||||||
// 1. do nothing when the url does not contain port
|
// 1. do nothing when the url does not contain port
|
||||||
$site_url = 'http://example.com';
|
$site_url = 'http://example.com';
|
||||||
expect(CronHelper::getSiteUrl($site_url))->equals($site_url);
|
expect(CronHelper::getSiteUrl($site_url))->equals($site_url);
|
||||||
@ -100,7 +100,7 @@ class CronHelperTest extends MailPoetTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
function testItCanEnforceExecutionLimit() {
|
function testItEnforcesExecutionLimit() {
|
||||||
$time = microtime(true);
|
$time = microtime(true);
|
||||||
expect(CronHelper::enforceExecutionLimit($time))->null();
|
expect(CronHelper::enforceExecutionLimit($time))->null();
|
||||||
try {
|
try {
|
||||||
@ -111,6 +111,11 @@ class CronHelperTest extends MailPoetTest {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItPingsDaemon() {
|
||||||
|
if(getenv('WP_TEST_ENABLE_NETWORK_TESTS') !== 'true') return;
|
||||||
|
expect(CronHelper::pingDaemon())->true();
|
||||||
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
ORM::raw_execute('TRUNCATE ' . Setting::$_table);
|
ORM::raw_execute('TRUNCATE ' . Setting::$_table);
|
||||||
}
|
}
|
||||||
|
@ -195,6 +195,15 @@ class DaemonTest extends MailPoetTest {
|
|||||||
expect(ignore_user_abort())->equals(1);
|
expect(ignore_user_abort())->equals(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function testItRespondsToPingRequest() {
|
||||||
|
$daemon = Stub::make(new Daemon(true), array(
|
||||||
|
'terminateRequest' => Stub::exactly(1, function($message) {
|
||||||
|
expect($message)->equals('pong');
|
||||||
|
})
|
||||||
|
), $this);
|
||||||
|
$daemon->ping();
|
||||||
|
}
|
||||||
|
|
||||||
function _after() {
|
function _after() {
|
||||||
ORM::raw_execute('TRUNCATE ' . Setting::$_table);
|
ORM::raw_execute('TRUNCATE ' . Setting::$_table);
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user