- Implements starting/stopping/pausing daemon
This commit is contained in:
@@ -11,64 +11,89 @@ define(
|
||||
MailPoet,
|
||||
classNames
|
||||
) {
|
||||
var QueueDaemonControl = React.createClass({
|
||||
var QueueControl = React.createClass({
|
||||
getInitialState: function () {
|
||||
return (queueDaemon) ? {
|
||||
status: queueDaemon.status,
|
||||
timeSinceStart: queueDaemon.time_since_start,
|
||||
timeSinceUpdate: queueDaemon.time_since_update,
|
||||
counter: queueDaemon.counter
|
||||
} : null;
|
||||
return (queueDaemon) ? queueDaemon : null;
|
||||
},
|
||||
getDaemonData: function () {
|
||||
MailPoet.Ajax.post({
|
||||
endpoint: 'queue',
|
||||
action: 'getQueueStatus'
|
||||
action: 'getDaemonStatus'
|
||||
}).done(function (response) {
|
||||
this.setState({
|
||||
status: response.status,
|
||||
timeSinceStart: response.time_since_start,
|
||||
timeSinceUpdate: response.time_since_update,
|
||||
counter: response.counter,
|
||||
});
|
||||
jQuery('.button-primary').removeClass('disabled');
|
||||
if (!response) {
|
||||
this.replaceState();
|
||||
} else {
|
||||
this.setState(response);
|
||||
}
|
||||
}.bind(this));
|
||||
},
|
||||
componentDidMount: function () {
|
||||
componentDidMount: function componentDidMount() {
|
||||
if (this.isMounted()) {
|
||||
this.getDaemonData;
|
||||
setInterval(this.getDaemonData, 5000);
|
||||
}
|
||||
},
|
||||
controlDaemon: function (action) {
|
||||
jQuery('.button-primary').addClass('disabled');
|
||||
MailPoet.Ajax.post({
|
||||
endpoint: 'queue',
|
||||
action: 'controlDaemon',
|
||||
data: {'action': action}
|
||||
}).done(function (response) {
|
||||
if (!response) {
|
||||
this.replaceState();
|
||||
} else {
|
||||
this.setState(response);
|
||||
}
|
||||
}.bind(this));
|
||||
},
|
||||
render: function () {
|
||||
if (!this.state) {
|
||||
return (
|
||||
<div className="QueueControl">
|
||||
return
|
||||
<div>
|
||||
Woops, daemon is not running ;\
|
||||
</div>
|
||||
)
|
||||
}
|
||||
switch (this.state.status) {
|
||||
case 'started':
|
||||
return (
|
||||
<div>
|
||||
<div>
|
||||
Queue is currently <b>{this.state.status}</b>.
|
||||
Queue daemon is running.
|
||||
<br/>
|
||||
<br/>
|
||||
It was started
|
||||
<b> {this.state.timeSinceStart} </b> and was last executed
|
||||
<b> {this.state.timeSinceUpdate} </b> for a total of
|
||||
<b> {this.state.counter} </b> times (once every 30 seconds, unless it was interrupted and restarted).
|
||||
<strong> {this.state.timeSinceStart} </strong> and last executed
|
||||
<strong> {this.state.timeSinceUpdate} </strong> for a total of
|
||||
<strong> {this.state.counter} </strong> times (once every 30 seconds, unless it was interrupted and restarted).
|
||||
<br />
|
||||
</div>
|
||||
<div>
|
||||
|
||||
<br />
|
||||
<a href="#" className="button-primary" onClick={this.controlDaemon.bind(null, 'stop')}>Stop</a>
|
||||
<a href="#" className="button-primary" onClick={this.controlDaemon.bind(null, 'pause')}>Pause</a>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
break;
|
||||
case 'paused':
|
||||
case 'stopped':
|
||||
return (
|
||||
<div>
|
||||
Daemon is {this.state.status}
|
||||
<br />
|
||||
<br />
|
||||
<a href="#" className="button-primary" onClick={this.controlDaemon.bind(null, 'start')}>Start</a>
|
||||
</div>
|
||||
)
|
||||
break;
|
||||
}
|
||||
}
|
||||
});
|
||||
let container = document.getElementById('queue_container');
|
||||
if (container) {
|
||||
ReactDOM.render(
|
||||
<QueueDaemonControl />,
|
||||
container
|
||||
<QueueControl />,
|
||||
document.getElementById('queue_status')
|
||||
)
|
||||
}
|
||||
}
|
||||
|
@@ -16,13 +16,13 @@ class BootStrapMenu {
|
||||
return ($this->daemon) ?
|
||||
array_merge(
|
||||
array(
|
||||
'time_since_start' =>
|
||||
'timeSinceStart' =>
|
||||
Carbon::createFromFormat(
|
||||
'Y-m-d H:i:s',
|
||||
$this->daemon->created_at,
|
||||
'UTC'
|
||||
)->diffForHumans(),
|
||||
'time_since_update' =>
|
||||
'timeSinceUpdate' =>
|
||||
Carbon::createFromFormat(
|
||||
'Y-m-d H:i:s',
|
||||
$this->daemon->updated_at,
|
||||
|
@@ -28,7 +28,10 @@ class Daemon {
|
||||
if(!$daemon) {
|
||||
$daemon = Setting::create();
|
||||
$daemon->name = 'daemon';
|
||||
$daemon->value = json_encode(array('status' => 'stopped'));
|
||||
$daemon->value = json_encode(
|
||||
array(
|
||||
'status' => 'stopped',
|
||||
));
|
||||
$daemon->save();
|
||||
}
|
||||
if($daemonData['status'] !== 'started') {
|
||||
@@ -36,7 +39,7 @@ class Daemon {
|
||||
$daemonData = array(
|
||||
'status' => 'started',
|
||||
'token' => $this->refreshedToken,
|
||||
'counter' => ($daemonData['status'] === 'paused') ?
|
||||
'counter' => $daemonData['status'] === 'paused' ?
|
||||
$daemonData['counter'] :
|
||||
0
|
||||
);
|
||||
@@ -67,7 +70,7 @@ class Daemon {
|
||||
$worker = new Worker();
|
||||
$worker->process();
|
||||
$elapsedTime = microtime(true) - $this->timer;
|
||||
if ($elapsedTime < 30) {
|
||||
if($elapsedTime < 30) {
|
||||
sleep(30 - $elapsedTime);
|
||||
}
|
||||
|
||||
@@ -90,11 +93,11 @@ class Daemon {
|
||||
}
|
||||
|
||||
function refreshToken() {
|
||||
return Security::generateRandomString(5);
|
||||
return Security::generateRandomString();
|
||||
}
|
||||
|
||||
function manageSession($action) {
|
||||
switch ($action) {
|
||||
switch($action) {
|
||||
case 'start':
|
||||
if(session_id()) {
|
||||
session_write_close();
|
||||
|
@@ -17,11 +17,11 @@ class Supervisor {
|
||||
}
|
||||
|
||||
function checkDaemon() {
|
||||
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH'])) return;
|
||||
if(!$this->daemon) {
|
||||
return $this->startDaemon();
|
||||
} else {
|
||||
if(!$this->forceStart && ($this->daemonData['status'] === 'paused' ||
|
||||
if(!$this->forceStart && (
|
||||
$this->daemonData['status'] === 'paused' ||
|
||||
$this->daemonData['status'] === 'stopped'
|
||||
)
|
||||
) {
|
||||
@@ -33,8 +33,13 @@ class Supervisor {
|
||||
$this->daemon->updated_at, 'UTC'
|
||||
);
|
||||
$timeSinceLastStart = $currentTime->diffInSeconds($lastUpdateTime);
|
||||
if($timeSinceLastStart < 50) return;
|
||||
if(!$this->forceStart && $timeSinceLastStart < 50) return;
|
||||
if(
|
||||
($this->forceStart && $this->daemonData['status'] === 'paused') ||
|
||||
!$this->forceStart
|
||||
) {
|
||||
$this->daemonData['status'] = 'paused';
|
||||
}
|
||||
$this->daemon->value = json_encode($this->daemonData);
|
||||
$this->daemon->save();
|
||||
return $this->startDaemon();
|
||||
|
@@ -1,26 +1,24 @@
|
||||
<?php
|
||||
namespace MailPoet\Router;
|
||||
|
||||
use MailPoet\Models\Setting;
|
||||
use MailPoet\Queue\Daemon;
|
||||
use MailPoet\Queue\Supervisor;
|
||||
|
||||
if(!defined('ABSPATH')) exit;
|
||||
|
||||
class Queue {
|
||||
function start() {
|
||||
$supervisor = new Supervisor();
|
||||
function controlDaemon($data) {
|
||||
switch($data['action']) {
|
||||
case 'start':
|
||||
$supervisor = new Supervisor($forceStart = true);
|
||||
wp_send_json(
|
||||
array(
|
||||
'result' => ($supervisor->checkDaemon($forceStart = true)) ?
|
||||
'result' => $supervisor->checkDaemon() ?
|
||||
true :
|
||||
false
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
function update($data) {
|
||||
switch ($data['action']) {
|
||||
break;
|
||||
case 'stop':
|
||||
$status = 'stopped';
|
||||
break;
|
||||
@@ -43,9 +41,8 @@ class Queue {
|
||||
);
|
||||
}
|
||||
|
||||
function getQueueStatus() {
|
||||
function getDaemonStatus() {
|
||||
$daemon = new \MailPoet\Queue\BootStrapMenu();
|
||||
wp_send_json($daemon->bootStrap());
|
||||
|
||||
}
|
||||
}
|
@@ -8,7 +8,14 @@ class Security {
|
||||
return wp_create_nonce('mailpoet_token');
|
||||
}
|
||||
|
||||
static function generateRandomString($length) {
|
||||
return substr(str_shuffle("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"), 0, $length);
|
||||
static function generateRandomString($length = 5) {
|
||||
// non-cryptographically strong random generator
|
||||
return substr(
|
||||
md5(
|
||||
uniqid(
|
||||
mt_rand(), true)
|
||||
),
|
||||
0,
|
||||
(!is_int($length) || $length <= 5 || $length >= 32) ? 5 : $length);
|
||||
}
|
||||
}
|
@@ -1,7 +1,10 @@
|
||||
<% extends 'layout.html' %>
|
||||
|
||||
<% block content %>
|
||||
<div id="queue_container"></div>
|
||||
<div id="queue_container">
|
||||
<div id="queue_status"></div>
|
||||
<div id="queue_control"></div>
|
||||
</div>
|
||||
<script type="text/javascript">
|
||||
</script>
|
||||
<script>
|
||||
|
Reference in New Issue
Block a user