From: <gem...@li...> - 2011-10-25 14:32:21
|
Revision: 137 http://gemstracker.svn.sourceforge.net/gemstracker/?rev=137&view=rev Author: matijsdejong Date: 2011-10-25 14:32:10 +0000 (Tue, 25 Oct 2011) Log Message: ----------- Locks now set using LockFile object. #29 closed, mail jobs can be set on or off. Modified Paths: -------------- trunk/library/classes/Gems/Controller/ModelSnippetActionAbstract.php trunk/library/classes/Gems/Default/CronAction.php trunk/library/classes/Gems/Default/MailJobAction.php trunk/library/classes/Gems/Default/ProjectInformationAction.php trunk/library/classes/Gems/Menu/MenuAbstract.php trunk/library/classes/Gems/Util.php trunk/library/classes/GemsEscort.php Added Paths: ----------- trunk/library/classes/Gems/Util/LockFile.php Modified: trunk/library/classes/Gems/Controller/ModelSnippetActionAbstract.php =================================================================== --- trunk/library/classes/Gems/Controller/ModelSnippetActionAbstract.php 2011-10-25 12:17:39 UTC (rev 136) +++ trunk/library/classes/Gems/Controller/ModelSnippetActionAbstract.php 2011-10-25 14:32:10 UTC (rev 137) @@ -85,6 +85,12 @@ public $escort; /** + * + * @var Gems_Menu + */ + public $menu; + + /** * The snippets used for the index action, before those in autofilter * * @var mixed String or array of snippets name Modified: trunk/library/classes/Gems/Default/CronAction.php =================================================================== --- trunk/library/classes/Gems/Default/CronAction.php 2011-10-25 12:17:39 UTC (rev 136) +++ trunk/library/classes/Gems/Default/CronAction.php 2011-10-25 14:32:10 UTC (rev 137) @@ -69,6 +69,12 @@ /** * + * @var Gems_Menu + */ + public $menu; + + /** + * * @var Zend_Session_Namespace */ public $session; @@ -83,6 +89,25 @@ public $useHtmlView = true; /** + * + * @var Gems_Util + */ + public $util; + + /** + * Action that switches the cron job lock on or off. + */ + public function cronLockAction() + { + // Switch lock + $this->util->getCronJobLock()->reverse(); + + // Redirect + $request = $this->getRequest(); + $this->_reroute($this->menu->getCurrentParent()->toRouteUrl()); + } + + /** * Loads an e-mail template * * @param integer|null $templateId @@ -106,7 +131,12 @@ public function indexAction() { $this->initHtml(); - $this->mailJob(); + + if ($this->util->getCronJobLock()->isLocked()) { + $this->html->append($this->_('Cron jobs turned off.')); + } else { + $this->mailJob(); + } } public function mailJob() @@ -165,8 +195,13 @@ } } - $this->html->append($mailer->getMessages()); + $msg = $mailer->getMessages(); + if (! $msg) { + $msg[] = $this->_('No mails sent'); + } + $this->html->append($msg); + if ($currentUser) { $this->escort->loadLoginInfo($currentUser); } else { Modified: trunk/library/classes/Gems/Default/MailJobAction.php =================================================================== --- trunk/library/classes/Gems/Default/MailJobAction.php 2011-10-25 12:17:39 UTC (rev 136) +++ trunk/library/classes/Gems/Default/MailJobAction.php 2011-10-25 14:32:10 UTC (rev 137) @@ -185,6 +185,16 @@ { $this->html->h3($this->_('Automatic mail jobs')); + $lock = $this->util->getCronJobLock(); + if ($lock->isLocked()) { + $this->addMessage(sprintf($this->_('Automatic mails have been turned off since %s.'), $lock->getLockTime())); + + $request = $this->getRequest(); + if ($menuItem = $this->menu->findFirst(array($request->getControllerKey() => 'cron', $request->getActionKey() => 'cron-lock'))) { + $menuItem->set('label', $this->_('Turn Automatic Mail Jobs ON')); + } + } + parent::indexAction(); $this->html->pInfo($this->_('With automatic mail jobs and a cron job on the server, mails can be sent without manual user action.')); Modified: trunk/library/classes/Gems/Default/ProjectInformationAction.php =================================================================== --- trunk/library/classes/Gems/Default/ProjectInformationAction.php 2011-10-25 12:17:39 UTC (rev 136) +++ trunk/library/classes/Gems/Default/ProjectInformationAction.php 2011-10-25 14:32:10 UTC (rev 137) @@ -144,7 +144,8 @@ $data[$this->_('Server OS')] = php_uname('s'); $data[$this->_('Time on server')] = date('r'); - if (file_exists($this->escort->getMaintenanceLockFilename())) { + $lock = $this->util->getMaintenanceLock(); + if ($lock->isLocked()) { $label = $this->_('Turn Maintenance Mode OFF'); } else { $label = $this->_('Turn Maintenance Mode ON'); @@ -161,14 +162,13 @@ $this->html->buttonDiv($buttonList); } + /** + * Action that switches the maintenance lock on or off. + */ public function maintenanceAction() { - $lockFile = $this->escort->getMaintenanceLockFilename(); - if (file_exists($lockFile)) { - unlink($lockFile); - } else { - touch($lockFile); - } + // Switch lock + $this->util->getMaintenanceLock()->reverse(); // Dump the existing maintenance mode messages. $this->escort->getMessenger()->clearCurrentMessages(); Modified: trunk/library/classes/Gems/Menu/MenuAbstract.php =================================================================== --- trunk/library/classes/Gems/Menu/MenuAbstract.php 2011-10-25 12:17:39 UTC (rev 136) +++ trunk/library/classes/Gems/Menu/MenuAbstract.php 2011-10-25 14:32:10 UTC (rev 137) @@ -199,6 +199,23 @@ return $page; } + /** + * Add a menu item that is never added to the navigation tree and only shows up as a button. + * + * @param string $label + * @param string $privilege + * @param string $controller + * @param string $action + * @param array $other + * @return Gems_Menu_SubMenuItem + */ + public function addButtonOnly($label, $privilege, $controller, $action = 'index', array $other = array()) + { + $other['button_only'] = true; + + return $this->addPage($label, $privilege, $controller, $action, $other); + } + public function addContainer($label, $privilege = null, array $other = array()) { $other['label'] = $label; @@ -210,13 +227,6 @@ return $this->add($other); } - public function addButtonOnly($label, $privilege, $controller, $action = 'index', array $other = array()) - { - $other['button_only'] = true; - - return $this->addPage($label, $privilege, $controller, $action, $other); - } - /** * Add a Mail menu tree to the menu * @@ -237,6 +247,8 @@ // MAIL JOB CONTROLLER $page = $setup->addBrowsePage($this->_('Automatic mail'), 'pr.mail.job', 'mail-job'); + $page->addButtonOnly($this->_('Turn Automatic Mail Jobs OFF'), 'pr.mail.job', 'cron', 'cron-lock'); + $page->addPage($this->_('Run'), null, 'cron', 'index'); // MAIL SERVER CONTROLLER $page = $setup->addBrowsePage($this->_('Servers'), 'pr.mail.server', 'mail-server'); Added: trunk/library/classes/Gems/Util/LockFile.php =================================================================== --- trunk/library/classes/Gems/Util/LockFile.php (rev 0) +++ trunk/library/classes/Gems/Util/LockFile.php 2011-10-25 14:32:10 UTC (rev 137) @@ -0,0 +1,124 @@ +<?php + +/** + * Copyright (c) 2011, Erasmus MC + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of Erasmus MC nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + * + * + * @package Gems + * @subpackage Util + * @author Matijs de Jong <mj...@ma...> + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @version $Id: Sample.php 203 2011-07-07 12:51:32Z matijs $ + */ + +/** + * A simple file based locking mechanism. + * + * @package Gems + * @subpackage Util + * @copyright Copyright (c) 2011 Erasmus MC + * @license New BSD License + * @since Class available since version 1.4.4 + */ +class Gems_Util_LockFile +{ + /** + * + * @var string + */ + protected $lockFileName; + + /** + * + * @param string $lockFileName The name of the lockfile + */ + public function __construct($lockFileName) + { + $this->lockFileName = $lockFileName; + } + + /** + * Last time the lock was set. + * + * @return MUtil_Date or null when not locked. + */ + public function getLockTime() + { + if ($this->isLocked()) { + return new MUtil_Date(filectime($this->lockFileName)); + } + } + + /** + * Returns true if this lock exists. + * + * @return boolean + */ + public function isLocked() + { + return file_exists($this->lockFileName); + } + + /** + * Lock this file and updates lock time. + * + * @return Gems_Util_LockFile (continuation pattern) + */ + public function lock() + { + touch($this->lockFileName); + return $this; + } + + /** + * Switches from lock to unlocked state. + * + * @return Gems_Util_LockFile (continuation pattern) + */ + public function reverse() + { + if ($this->isLocked()) { + $this->unlock(); + } else { + $this->lock(); + } + return $this; + } + + /** + * Unlocks this lock file by deleting it + * + * @return Gems_Util_LockFile (continuation pattern) + */ + public function unlock() + { + if ($this->isLocked()) { + unlink($this->lockFileName); + } + return $this; + } +} Modified: trunk/library/classes/Gems/Util.php =================================================================== --- trunk/library/classes/Gems/Util.php 2011-10-25 12:17:39 UTC (rev 136) +++ trunk/library/classes/Gems/Util.php 2011-10-25 14:32:10 UTC (rev 137) @@ -1,6 +1,5 @@ <?php - /** * Copyright (c) 2011, Erasmus MC * All rights reserved. @@ -117,6 +116,16 @@ return array_combine($consentTypes, $consentTypes); } + /** + * Returns the cron job lock + * + * @return Gems_Util_LockFile + */ + public function getCronJobLock() + { + return $this->_loadClass('lockFile', true, array(GEMS_ROOT_DIR . '/var/settings/cron_lock.txt')); + } + public function getCurrentURI($subpath = '') { static $uri; @@ -169,7 +178,17 @@ } /** + * Returns the maintenance lock * + * @return Gems_Util_LockFile + */ + public function getMaintenanceLock() + { + return $this->_loadClass('lockFile', null, array(GEMS_ROOT_DIR . '/var/settings/lock.txt')); + } + + /** + * * @param string $sourceAction The action to get the cache from if not the current one. * @param boolean $readonly Optional, tell the cache not to store any new values * @return Gems_Util_RequestCache Modified: trunk/library/classes/GemsEscort.php =================================================================== --- trunk/library/classes/GemsEscort.php 2011-10-25 12:17:39 UTC (rev 136) +++ trunk/library/classes/GemsEscort.php 2011-10-25 14:32:10 UTC (rev 137) @@ -1133,15 +1133,6 @@ } /** - * - * @return string Name of the file that performs the maintenance lock - */ - public function getMaintenanceLockFilename() - { - return GEMS_ROOT_DIR . '/var/settings/lock.txt'; - } - - /** * Retrieve the GemsEscort object * * @return GemsEscort @@ -1532,7 +1523,7 @@ * Check if we are in maintenance mode or not. This is triggeren by a file in the var/settings * directory with the name lock.txt */ - if (file_exists($this->getMaintenanceLockFilename())) { + if ($this->getUtil()->getMaintenanceLock()->isLocked()) { if ($this->session->user_id && $this->session->user_role !== 'master') { //Still allow logoff so we can relogin as master if (!('index' == $request->getControllerName() && 'logoff' == $request->getActionName())) { This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |