Cron update + removing console.log

- use Setting::getValue for getDaemon method
- added "updated_at" property within cron_daemon value (instead of using the setting's column)
- converted line endings to Unix in notice.js and removed console.log
This commit is contained in:
Jonathan Labreuille
2016-01-08 12:02:11 +01:00
parent 5996696cc9
commit 82a736ffbb
3 changed files with 226 additions and 233 deletions

View File

@@ -1,75 +1,75 @@
define('notice', ['mailpoet', 'jquery'], function(MailPoet, jQuery) { define('notice', ['mailpoet', 'jquery'], function(MailPoet, jQuery) {
"use strict"; "use strict";
/*================================================================================================== /*==================================================================================================
MailPoet Notice: MailPoet Notice:
description: Handles notices description: Handles notices
version: 0.2 version: 0.2
author: Jonathan Labreuille author: Jonathan Labreuille
company: Wysija company: Wysija
dependencies: jQuery dependencies: jQuery
Usage: Usage:
// success message (static: false) // success message (static: false)
MailPoet.Notice.success('Yatta!'); MailPoet.Notice.success('Yatta!');
// error message (static: false) // error message (static: false)
MailPoet.Notice.error('Boo!'); MailPoet.Notice.error('Boo!');
// system message (static: true) // system message (static: true)
MailPoet.Notice.system('You need to updated ASAP!'); MailPoet.Notice.system('You need to updated ASAP!');
Examples: Examples:
MailPoet.Notice.success('- success #1 -'); MailPoet.Notice.success('- success #1 -');
setTimeout(function() { setTimeout(function() {
MailPoet.Notice.success('- success #2 -'); MailPoet.Notice.success('- success #2 -');
setTimeout(function() { setTimeout(function() {
MailPoet.Notice.error('- error -'); MailPoet.Notice.error('- error -');
setTimeout(function() { setTimeout(function() {
MailPoet.Notice.system('- system -'); MailPoet.Notice.system('- system -');
setTimeout(function() { setTimeout(function() {
MailPoet.Notice.hide(); MailPoet.Notice.hide();
}, 2500); }, 2500);
}, 300); }, 300);
}, 400); }, 400);
}, 500); }, 500);
==================================================================================================*/ ==================================================================================================*/
MailPoet.Notice = { MailPoet.Notice = {
version: 0.2, version: 0.2,
// default options // default options
defaults: { defaults: {
type: 'success', type: 'success',
message: '', message: '',
static: false, static: false,
hideClose: false, hideClose: false,
id: null, id: null,
positionAfter: false, positionAfter: false,
scroll: false, scroll: false,
timeout: 2000, timeout: 2000,
onOpen: null, onOpen: null,
onClose: null onClose: null
}, },
options: {}, options: {},
init: function(options) { init: function(options) {
// set options // set options
this.options = jQuery.extend({}, this.defaults, options); this.options = jQuery.extend({}, this.defaults, options);
// clone element // clone element
this.element = jQuery('#mailpoet_notice_'+this.options.type).clone(); this.element = jQuery('#mailpoet_notice_'+this.options.type).clone();
// add data-id to the element // add data-id to the element
if (this.options.id) this.element.attr('data-id', 'notice_' + this.options.id); if (this.options.id) this.element.attr('data-id', 'notice_' + this.options.id);
// remove id from clone // remove id from clone
this.element.removeAttr('id'); this.element.removeAttr('id');
// insert notice after its parent // insert notice after its parent
var positionAfter; var positionAfter;
if (typeof this.options.positionAfter === 'object') { if (typeof this.options.positionAfter === 'object') {
positionAfter = this.options.positionAfter; positionAfter = this.options.positionAfter;
@@ -78,136 +78,135 @@ define('notice', ['mailpoet', 'jquery'], function(MailPoet, jQuery) {
} else { } else {
positionAfter = jQuery('#mailpoet_notice_'+this.options.type); positionAfter = jQuery('#mailpoet_notice_'+this.options.type);
} }
console.log('positionAfter', typeof this.options.positionAfter); positionAfter.after(this.element);
positionAfter.after(this.element);
// setup onClose callback
// setup onClose callback var onClose = null;
var onClose = null; if(this.options.onClose !== null) {
if(this.options.onClose !== null) { onClose = this.options.onClose;
onClose = this.options.onClose; }
}
// listen to remove event
// listen to remove event jQuery(this.element).on('close', function() {
jQuery(this.element).on('close', function() { jQuery(this).fadeOut(200, function() {
jQuery(this).fadeOut(200, function() { // on close callback
// on close callback if(onClose !== null) {
if(onClose !== null) { onClose();
onClose(); }
} // remove notice
// remove notice jQuery(this).remove();
jQuery(this).remove(); });
}); }.bind(this.element));
}.bind(this.element));
// listen to message event
// listen to message event jQuery(this.element).on('message', function(e, message) {
jQuery(this.element).on('message', function(e, message) { MailPoet.Notice.setMessage(message);
MailPoet.Notice.setMessage(message); }.bind(this.element));
}.bind(this.element));
return this;
return this; },
}, isHTML: function(str) {
isHTML: function(str) { var a = document.createElement('div');
var a = document.createElement('div'); a.innerHTML = str;
a.innerHTML = str; for(var c = a.childNodes, i = c.length; i--;) {
for(var c = a.childNodes, i = c.length; i--;) { if(c[i].nodeType == 1) return true;
if(c[i].nodeType == 1) return true; }
} return false;
return false; },
}, setMessage: function(message) {
setMessage: function(message) { // if it's not an html message, let's sugar coat the message with a fancy <p>
// if it's not an html message, let's sugar coat the message with a fancy <p> if(this.isHTML(message) === false) {
if(this.isHTML(message) === false) { message = '<p>'+message+'</p>';
message = '<p>'+message+'</p>'; }
} // set message
// set message return this.element.html(message);
return this.element.html(message); },
}, show: function(options) {
show: function(options) { // initialize
// initialize this.init(options);
this.init(options);
// show notice
// show notice this.showNotice();
this.showNotice();
// return this;
// return this; },
}, showNotice: function() {
showNotice: function() { // set message
// set message this.setMessage(this.options.message);
this.setMessage(this.options.message);
// position notice
// position notice this.element.insertAfter(jQuery('h2.title'));
this.element.insertAfter(jQuery('h2.title'));
// set class name
// set class name switch(this.options.type) {
switch(this.options.type) { case 'success':
case 'success': this.element.addClass('updated');
this.element.addClass('updated'); break;
break; case 'system':
case 'system': this.element.addClass('update-nag');
this.element.addClass('update-nag'); break;
break; case 'error':
case 'error': this.element.addClass('error');
this.element.addClass('error'); break;
break; }
}
// make the notice appear
// make the notice appear this.element.fadeIn(200);
this.element.fadeIn(200);
// if scroll option is enabled, scroll to the notice
// if scroll option is enabled, scroll to the notice if(this.options.scroll === true) {
if(this.options.scroll === true) { this.element.get(0).scrollIntoView(false);
this.element.get(0).scrollIntoView(false); }
}
// if the notice is not static, it has to disappear after a timeout
// if the notice is not static, it has to disappear after a timeout if(this.options.static === false) {
if(this.options.static === false) { this.element.delay(this.options.timeout).trigger('close');
this.element.delay(this.options.timeout).trigger('close'); } else if (this.options.hideClose === false) {
} else if (this.options.hideClose === false) { this.element.append('<a href="javascript:;" class="mailpoet_notice_close"><span class="dashicons dashicons-dismiss"></span></a>');
this.element.append('<a href="javascript:;" class="mailpoet_notice_close"><span class="dashicons dashicons-dismiss"></span></a>'); this.element.find('.mailpoet_notice_close').on('click', function() {
this.element.find('.mailpoet_notice_close').on('click', function() { jQuery(this).trigger('close');
jQuery(this).trigger('close'); });
}); }
}
// call onOpen callback
// call onOpen callback if(this.options.onOpen !== null) {
if(this.options.onOpen !== null) { this.options.onOpen(this.element);
this.options.onOpen(this.element); }
} },
}, hide: function(all) {
hide: function(all) { if(all !== undefined && all === true) {
if(all !== undefined && all === true) { jQuery('.mailpoet_notice:not([id])').trigger('close');
jQuery('.mailpoet_notice:not([id])').trigger('close'); } else if (all !== undefined && jQuery.isArray(all)) {
} else if (all !== undefined && jQuery.isArray(all)) { for (var id in all) {
for (var id in all) { jQuery('[data-id="notice_' + all[id] + '"]')
jQuery('[data-id="notice_' + all[id] + '"]') .trigger('close');
.trigger('close'); }
} } if (all !== undefined) {
} if (all !== undefined) { jQuery('[data-id="notice_' + all + '"]')
jQuery('[data-id="notice_' + all + '"]') .trigger('close');
.trigger('close'); } else {
} else { jQuery('.mailpoet_notice.updated:not([id]), .mailpoet_notice.error:not([id])')
jQuery('.mailpoet_notice.updated:not([id]), .mailpoet_notice.error:not([id])') .trigger('close');
.trigger('close'); }
} },
}, error: function(message, options) {
error: function(message, options) { this.show(jQuery.extend({}, {
this.show(jQuery.extend({}, { type: 'error',
type: 'error', message: '<p>'+message+'</p>'
message: '<p>'+message+'</p>' }, options));
}, options)); },
}, success: function(message, options) {
success: function(message, options) { this.show(jQuery.extend({}, {
this.show(jQuery.extend({}, { type: 'success',
type: 'success', message: '<p>'+message+'</p>'
message: '<p>'+message+'</p>' }, options));
}, options)); },
}, system: function(message, options) {
system: function(message, options) { this.show(jQuery.extend({}, {
this.show(jQuery.extend({}, { type: 'system',
type: 'system', static: true,
static: true, message: message
message: message }, options));
}, options)); }
} };
};
}); });

View File

@@ -48,9 +48,14 @@ class Daemon {
$_SESSION['cron_daemon'] = 'started'; $_SESSION['cron_daemon'] = 'started';
$_SESSION['cron_daemon'] = array('result' => true); $_SESSION['cron_daemon'] = array('result' => true);
$this->manageSession('end'); $this->manageSession('end');
$daemon['status'] = 'started'; $daemon['status'] = 'started';
$daemon['token'] = $this->refreshed_token; $daemon['token'] = $this->refreshed_token;
$this->saveDaemon($daemon); $this->saveDaemon(array(
'status' => 'started',
'token' => $this->refreshed_token,
'updated_at' => time()
));
$this->callSelf(); $this->callSelf();
} }
$this->manageSession('end'); $this->manageSession('end');
@@ -79,10 +84,12 @@ class Daemon {
if($elapsed_time < 30) { if($elapsed_time < 30) {
sleep(30 - $elapsed_time); sleep(30 - $elapsed_time);
} }
// after each execution, read daemon in case it's status was modified // after each execution, read daemon in case its status was modified
$daemon = $this->getDaemon(); $daemon = $this->getDaemon();
if($daemon['status'] === 'stopping') $daemon['status'] = 'stopped'; if($daemon['status'] === 'stopping') $daemon['status'] = 'stopped';
if($daemon['status'] === 'starting') $daemon['status'] = 'started'; if($daemon['status'] === 'starting') $daemon['status'] = 'started';
$daemon['token'] = $this->refreshed_token; $daemon['token'] = $this->refreshed_token;
$daemon['counter']++; $daemon['counter']++;
$this->saveDaemon($daemon); $this->saveDaemon($daemon);
@@ -90,7 +97,7 @@ class Daemon {
} }
function getDaemon() { function getDaemon() {
return Setting::getValue('cron_daemon', null); return Setting::getValue('cron_daemon');
} }
function saveDaemon($daemon_data) { function saveDaemon($daemon_data) {

View File

@@ -1,7 +1,6 @@
<?php <?php
namespace MailPoet\Cron; namespace MailPoet\Cron;
use Carbon\Carbon;
use MailPoet\Config\Env; use MailPoet\Config\Env;
use MailPoet\Models\Setting; use MailPoet\Models\Setting;
@@ -22,25 +21,27 @@ class Supervisor {
if(!$this->daemon) { if(!$this->daemon) {
return $this->startDaemon(); return $this->startDaemon();
} }
if(!$this->force_start && ( if(
$this->daemon['value']['status'] === 'stopped' || !$this->force_start &&
$this->daemon['value']['status'] === 'stopping') in_array($this->daemon['status'], array('stopped', 'stopping'))
) { ) {
return $this->daemon['value']['status']; return $this->daemon['status'];
} }
$time_since_last_run = $this->getDaemonLastRunTime();
if($time_since_last_run < 40) { $elapsed_time = time() - (int)$this->daemon['updated_at'];
if($elapsed_time < 40) {
if(!$this->force_start) { if(!$this->force_start) {
return; return;
} }
if($this->daemon['value']['status'] === 'stopping' || if($this->daemon['status'] === 'stopping' ||
$this->daemon['value']['status'] === 'starting' $this->daemon['status'] === 'starting'
) { ) {
return $this->daemon['value']['status']; return $this->daemon['status'];
} }
} }
$this->daemon['value']['status'] = 'starting'; $this->daemon['status'] = 'starting';
$this->saveDaemon($this->daemon['value']); $this->saveDaemon($this->daemon);
return $this->startDaemon(); return $this->startDaemon();
} }
@@ -62,12 +63,7 @@ class Supervisor {
} }
function getDaemon() { function getDaemon() {
$daemon = Setting::where('name', 'cron_daemon') return Setting::getValue('cron_daemon');
->findOne();
if(!$daemon) return false;
$daemon = $daemon->asArray();
$daemon['value'] = unserialize($daemon['value']);
return $daemon;
} }
function saveDaemon($daemon_data) { function saveDaemon($daemon_data) {
@@ -104,13 +100,4 @@ class Supervisor {
// throw an error if all connections fail // throw an error if all connections fail
throw new \Exception(__('Site URL is unreachable.')); throw new \Exception(__('Site URL is unreachable.'));
} }
function getDaemonLastRunTime() {
$current_time = Carbon::now('UTC');
$last_update_time = Carbon::createFromFormat(
'Y-m-d H:i:s',
$this->daemon['updated_at'], 'UTC'
);
return $current_time->diffInSeconds($last_update_time);
}
} }