| // | Dmitri Lakachauskis | // +---------------------------------------------------------------------------+ /** * CLI manager, which processes email queue. * * @package seagull * @subpackage emailqueue * @author Peter Termaten * @author Dmitri Lakachauskis */ class EmailQueueMgr extends SGL_Manager { public function __construct() { SGL::logMessage(null, PEAR_LOG_DEBUG); parent::SGL_Manager(); $this->_aActionsMapping = array( 'list' => array('list', 'cliResult'), 'process' => array('process','cliResult'), 'flush' => array('flush', 'process', 'cliResult') ); } public function validate(SGL_Request $req, SGL_Registry $input) { SGL::logMessage(null, PEAR_LOG_DEBUG); $this->validated = true; $input->action = $req->get('action') ? $req->get('action') : 'list'; $input->tty = "\n"; $input->batchId = $req->get('batchId'); $input->deliveryDate = $req->get('deliveryDate'); $input->limit = $req->get('limit'); $input->interval = $req->get('interval'); } /** * By default we just show availabe actions. * * @param SGL_Registry $input * @param SGL_Output $output */ public function _cmd_list(SGL_Registry $input, SGL_Output $output) { SGL::logMessage(null, PEAR_LOG_DEBUG); $input->tty .= <<< HELP Available actions: 1. process process emails in queue --batchId process emails by certain batch id --deliveryDate process emails of specified date --limit process only certain number of emails (unlimited by default) --interval delay in seconds between every email (anti-spam measure) 2. flush process all emails in queue, same as --action=process --deliveryDate=all HELP; } /** * Send emails from queue. * * Example usage: php www/index.php * --moduleName=emailqueue * --managerName=emailqueue * --action=process * --groupId=1 * * @param SGL_Registry $input * @param SGL_Output $output */ public function _cmd_process(SGL_Registry $input, SGL_Output $output) { SGL::logMessage(null, PEAR_LOG_DEBUG); // header $msg = "Notice: `removeSent` option is set, processed messages" . " will be removed from queue\n"; $input->tty .= $msg; $this->_flush($input->tty); // get config $conf = $this->conf['EmailQueueMgr']; if (!empty($input->limit)) { $conf['limit'] = $input->limit; } // queue params $aParams = array(); if (!empty($input->batchId)) { $aParams['batch_id'] = $input->batchId; } $emailerClass = trim($this->conf['EmailQueueMgr']['emailer']); $emailerFile = str_replace('_', DIRECTORY_SEPARATOR, $emailerClass) . '.php'; require_once $emailerFile; // process queue $oQueue = new $emailerClass($conf); $aRet = $oQueue->processQueue($input->deliveryDate, $input->interval, $aParams); if (PEAR::isError($aRet)) { $input->tty .= sprintf("Error: %s\n", $aRet->getMessage()); } else { $msg = "Messages proccesed %s; sent: %s\n"; $input->tty .= sprintf($msg, $aRet['processed'], $aRet['sent']); } } public function _cmd_flush(SGL_Registry $input, SGL_Output $output) { $input->deliveryDate = 'all'; } /** * Action, which outputs CLI result. * * @param SGL_Registry $input * @param SGL_Output $output */ public function _cmd_cliResult(SGL_Registry $input, SGL_Output $output) { SGL::logMessage(null, PEAR_LOG_DEBUG); $input->tty .= "\n"; $this->_flush($input->tty, $stopScript = true); } /** * Send data to terminal. * * @param string $string * @param boolean $stopScript */ private function _flush(&$string, $stopScript = false) { echo $string; flush(); $string = ''; if ($stopScript) { exit; } } } ?>