Merge pull request #286 from mailpoet/cron_update

Cron update
This commit is contained in:
Tautvidas Sipavičius
2016-01-08 19:23:50 +02:00
3 changed files with 224 additions and 232 deletions

View File

@@ -78,7 +78,6 @@ define('notice', ['mailpoet', 'jquery'], function(MailPoet, jQuery) {
} else {
positionAfter = jQuery('#mailpoet_notice_'+this.options.type);
}
console.log('positionAfter', typeof this.options.positionAfter);
positionAfter.after(this.element);
// setup onClose callback

View File

@@ -79,21 +79,27 @@ class Daemon {
if($elapsed_time < 30) {
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();
if($daemon['status'] === 'stopping') $daemon['status'] = 'stopped';
if($daemon['status'] === 'starting') $daemon['status'] = 'started';
$daemon['token'] = $this->refreshed_token;
$daemon['counter']++;
$this->saveDaemon($daemon);
if($daemon['status'] === 'started') $this->callSelf();
}
function getDaemon() {
return Setting::getValue('cron_daemon', null);
return Setting::getValue('cron_daemon');
}
function saveDaemon($daemon_data) {
$daemon_data['updated_at'] = time();
return Setting::setValue(
'cron_daemon',
$daemon_data

View File

@@ -1,7 +1,6 @@
<?php
namespace MailPoet\Cron;
use Carbon\Carbon;
use MailPoet\Config\Env;
use MailPoet\Models\Setting;
@@ -22,25 +21,27 @@ class Supervisor {
if(!$this->daemon) {
return $this->startDaemon();
}
if(!$this->force_start && (
$this->daemon['value']['status'] === 'stopped' ||
$this->daemon['value']['status'] === 'stopping')
if(
!$this->force_start &&
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) {
return;
}
if($this->daemon['value']['status'] === 'stopping' ||
$this->daemon['value']['status'] === 'starting'
if($this->daemon['status'] === 'stopping' ||
$this->daemon['status'] === 'starting'
) {
return $this->daemon['value']['status'];
return $this->daemon['status'];
}
}
$this->daemon['value']['status'] = 'starting';
$this->saveDaemon($this->daemon['value']);
$this->daemon['status'] = 'starting';
$this->saveDaemon($this->daemon);
return $this->startDaemon();
}
@@ -62,12 +63,7 @@ class Supervisor {
}
function getDaemon() {
$daemon = Setting::where('name', 'cron_daemon')
->findOne();
if(!$daemon) return false;
$daemon = $daemon->asArray();
$daemon['value'] = unserialize($daemon['value']);
return $daemon;
return Setting::getValue('cron_daemon');
}
function saveDaemon($daemon_data) {
@@ -104,13 +100,4 @@ class Supervisor {
// throw an error if all connections fail
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);
}
}