Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b0d4a17
commit 8757b9a
Showing
2 changed files
with
109 additions
and
108 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,55 +1,56 @@ | ||
<?php | ||
class CronsEvents extends AppEvents { | ||
/** | ||
* @copydoc AppError::onRequireTodoList() | ||
* | ||
* Check if the crons are running and if not add a notice to the admin | ||
* so that they can take action. If they are setup but something has | ||
* gone wrong and they are not running show an error. | ||
*/ | ||
public function onRequireTodoList($event) { | ||
$crons = $this->onAreCronsSetup(); | ||
if(!$crons) { | ||
return array( | ||
array( | ||
'name' => __('Crons are not configured yet'), | ||
'type' => 'warning', | ||
'url' => '#' | ||
) | ||
); | ||
} | ||
|
||
else if(date('Y-m-d H:i:s', strtotime('-' . Configure::read('Cron.run_every'))) > $crons) { | ||
return array( | ||
array( | ||
'name' => sprintf(__('The crons are not running, last run was %s'), $crons), | ||
'type' => 'error', | ||
'url' => '#' | ||
) | ||
); | ||
} | ||
|
||
return true; | ||
class CronsEvents extends AppEvents { | ||
/** | ||
* @copydoc AppError::onRequireTodoList() | ||
* | ||
* Check if the crons are running and if not add a notice to the admin | ||
* so that they can take action. If they are setup but something has | ||
* gone wrong and they are not running show an error. | ||
*/ | ||
public function onRequireTodoList(Event $Event) { | ||
$crons = $this->onAreCronsSetup(); | ||
if(!$crons) { | ||
return array( | ||
array( | ||
'name' => __d('crons', 'Crons are not configured yet'), | ||
'type' => 'warning', | ||
'url' => '#' | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @brief check if crons are running | ||
* | ||
* @return string|bool false if not, or datetime of last run | ||
*/ | ||
public function onAreCronsSetup() { | ||
$date = ClassRegistry::init('Crons.Cron')->getLastRun(); | ||
return ($date) ? date('Y-m-d H:i:s', strtotime($date)) : $date; | ||
if(date('Y-m-d H:i:s', strtotime('-' . Configure::read('Cron.run_every'))) > $crons) { | ||
return array( | ||
array( | ||
'name' => sprintf(__d('crons', 'The crons are not running, last run was %s'), $crons), | ||
'type' => 'error', | ||
'url' => '#' | ||
) | ||
); | ||
} | ||
|
||
/** | ||
* @brief housekeeping, clear out old cron logs. | ||
* | ||
* @param <type> $event | ||
* @return <type> | ||
*/ | ||
public function onRunCrons($event) { | ||
ClassRegistry::init('Crons.Cron')->clearOldLogs(); | ||
return true; | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @brief check if crons are running | ||
* | ||
* @return string|bool false if not, or datetime of last run | ||
*/ | ||
public function onAreCronsSetup() { | ||
$date = ClassRegistry::init('Crons.Cron')->getLastRun(); | ||
return $date ? date('Y-m-d H:i:s', strtotime($date)) : $date; | ||
} | ||
|
||
/** | ||
* @brief housekeeping, clear out old cron logs. | ||
* | ||
* @param Event $Event | ||
* | ||
* @return bool | ||
*/ | ||
public function onRunCrons(Event $Event) { | ||
ClassRegistry::init('Crons.Cron')->clearOldLogs(); | ||
return true; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,67 @@ | ||
<?php | ||
class CronsEventsTest extends CakeTestCase { | ||
public $fixtures = array( | ||
'plugin.crons.cron' | ||
); | ||
/** | ||
* @brief CronsEventsTest | ||
* | ||
* These tests are extended from InfinitasEventTestCase which does most of the | ||
* automated testing for simple events | ||
*/ | ||
|
||
public function startTest() { | ||
$this->Event = EventCore::getInstance(); | ||
$this->Cron = ClassRegistry::init('Crons.Cron'); | ||
} | ||
App::uses('InfinitasEventTestCase', 'Events.Test/Lib'); | ||
|
||
public function endTest() { | ||
unset($this->Event, $this->Cron); | ||
ClassRegistry::flush(); | ||
} | ||
class CronsEventsTest extends InfinitasEventTestCase { | ||
public $fixtures = array( | ||
'plugin.crons.cron' | ||
); | ||
|
||
/** | ||
* test if the checks are working fine | ||
*/ | ||
public function testAreCronsSetup() { | ||
$result = $this->Event->trigger($this, 'Crons.areCronsSetup'); | ||
$expected = array('areCronsSetup' => array('Crons' => '2010-12-07 14:21:01')); | ||
$this->assertEquals($expected, $result); | ||
public function setUp() { | ||
parent::setUp(); | ||
$this->Cron = ClassRegistry::init('Crons.Cron'); | ||
} | ||
|
||
$this->Cron->deleteAll(array('Cron.id != 1')); | ||
$result = $this->Event->trigger($this, 'Crons.areCronsSetup'); | ||
$expected = array('areCronsSetup' => array('Crons' => false)); | ||
$this->assertEquals($expected, $result); | ||
} | ||
public function tearDown() { | ||
parent::tearDown(); | ||
unset($this->Cron); | ||
} | ||
|
||
/** | ||
* test the todo list stuff is working fine | ||
*/ | ||
public function testRequireTodoList() { | ||
/** | ||
* were running but broke/stoped | ||
*/ | ||
$expected = array('requireTodoList' => array('Crons' => array( | ||
array('name' => '/^The crons are not running, last run was (.*)$/', 'type' => 'error', 'url' => '#') | ||
))); | ||
$event = $this->Event->trigger($this, 'Crons.requireTodoList'); | ||
$this->assertEquals($expected['requireTodoList']['Crons'][0]['type'], $event['requireTodoList']['Crons'][0]['type']); | ||
$this->assertEquals($expected['requireTodoList']['Crons'][0]['url'], $event['requireTodoList']['Crons'][0]['url']); | ||
$result = preg_match($expected['requireTodoList']['Crons'][0]['name'], $event['requireTodoList']['Crons'][0]['name']); | ||
$this->assertSame(1, $result); | ||
/** | ||
* test if the checks are working fine | ||
*/ | ||
public function testAreCronsSetup() { | ||
$result = $this->Event->trigger($this, 'Crons.areCronsSetup'); | ||
$expected = array('areCronsSetup' => array('Crons' => '2010-12-07 14:21:01')); | ||
$this->assertEquals($expected, $result); | ||
|
||
/** | ||
* have never been setup | ||
*/ | ||
$this->Cron->deleteAll(array('Cron.id != 1')); | ||
$result = $this->Event->trigger($this, 'Crons.requireTodoList'); | ||
$expected = array('requireTodoList' => array('Crons' => array( | ||
array('name' => 'Crons are not configured yet', 'type' => 'warning', 'url' => '#') | ||
))); | ||
$this->assertEquals($expected, $result); | ||
$this->Cron->deleteAll(array('Cron.id != 1')); | ||
$result = $this->Event->trigger($this, 'Crons.areCronsSetup'); | ||
$expected = array('areCronsSetup' => array('Crons' => false)); | ||
$this->assertEquals($expected, $result); | ||
} | ||
|
||
/** | ||
* running fine | ||
*/ | ||
$result = $this->Cron->start(); | ||
$this->assertTrue($result); | ||
/** | ||
* test the todo list stuff is working fine | ||
*/ | ||
public function testRequireTodoList() { | ||
$expected = array('requireTodoList' => array('Crons' => array( | ||
array('name' => '/^The crons are not running, last run was (.*)$/', 'type' => 'error', 'url' => '#') | ||
))); | ||
$event = $this->Event->trigger($this, 'Crons.requireTodoList'); | ||
$this->assertEquals($expected['requireTodoList']['Crons'][0]['type'], $event['requireTodoList']['Crons'][0]['type']); | ||
$this->assertEquals($expected['requireTodoList']['Crons'][0]['url'], $event['requireTodoList']['Crons'][0]['url']); | ||
$result = preg_match($expected['requireTodoList']['Crons'][0]['name'], $event['requireTodoList']['Crons'][0]['name']); | ||
$this->assertSame(1, $result); | ||
|
||
$result = $this->Event->trigger($this, 'Crons.requireTodoList'); | ||
$expected = array('requireTodoList' => array('Crons' => true)); | ||
$this->assertEqual($expected, $result); | ||
} | ||
} | ||
$this->Cron->deleteAll(array('Cron.id != 1')); | ||
$result = $this->Event->trigger($this, 'Crons.requireTodoList'); | ||
$expected = array('requireTodoList' => array('Crons' => array( | ||
array('name' => 'Crons are not configured yet', 'type' => 'warning', 'url' => '#') | ||
))); | ||
$this->assertEquals($expected, $result); | ||
|
||
$result = $this->Cron->start(); | ||
$this->assertTrue($result); | ||
|
||
$result = $this->Event->trigger($this, 'Crons.requireTodoList'); | ||
$expected = array('requireTodoList' => array('Crons' => true)); | ||
$this->assertEqual($expected, $result); | ||
} | ||
} |