1
0
Fork 0
mirror of https://github.com/Oreolek/ifhub.club.git synced 2024-05-21 10:18:19 +03:00

Пробный вариант крон-процесса для отложенной отправки почты.

This commit is contained in:
Alexey Kachayev 2009-09-10 18:51:58 +00:00
parent 6918edded1
commit 0eecbe72af
7 changed files with 135 additions and 18 deletions

View file

@ -53,7 +53,7 @@ class LsNotify extends Module {
*/
public function Init() {
if (!class_exists('LsViewer')) {
require_once(Config::Get('path.root.engine')."/modules/sys_viewer/Viewer.class.php");
require_once(Config::Get('path.root.engine')."/modules/viewer/Viewer.class.php");
}
$this->oViewerLocal=new LsViewer(Engine::getInstance());
$this->oViewerLocal->Init();
@ -581,5 +581,48 @@ class LsNotify extends Module {
$this->aTask=array();
}
}
/**
* Получает массив заданий на публикацию из базы
* с указанным количественным ограничением (выборка FIFO)
*
* @param int $iLimit
* @return array
*/
public function GetTasksDelayed($iLimit=10) {
return ($aResult=$this->oMapper->GetTasks($iLimit))
? $aResult
: array();
}
/**
* Отправляет на e-mail
*
* @param NotifyEntity_Task $oTask
*/
public function SendTask($oTask) {
$this->Mail_SetAdress($oTask->getUserMail(),$oTask->getUserLogin());
$this->Mail_SetSubject($oTask->getNotifySubject());
$this->Mail_SetBody($oTask->getNotifyText());
$this->Mail_setHTML();
$this->Mail_Send();
}
/**
* Удаляет отложенное Notify-задание из базы
*
* @param NotifyEntity_Task $oTask
* @return bool
*/
public function DeleteTask($oTask) {
return $this->oMapper->DeleteTask($oTask);
}
/**
* Удаляет отложенные Notify-задания по списку идентификаторов
*
* @param array $aArrayId
* @return bool
*/
public function DeleteTaskByArrayId($aArrayId) {
return $this->oMapper->DeleteTaskByArrayId($aArrayId);
}
}
?>

View file

@ -91,7 +91,17 @@ class Mapper_Notify extends Mapper {
}
public function GetTasks($iLimit) {
return array();
$sql = "SELECT *
FROM ".Config::Get('db.table.notify_task')."
ORDER BY date_created ASC
LIMIT ?d";
$aTasks=array();
if ($aRows=$this->oDb->select($sql,$iLimit)) {
foreach ($aRows as $aTask) {
$aTasks[]=Engine::GetEntity('Notify_Task',$aTask);
}
}
return $aTasks;
}
}
?>

View file

@ -184,6 +184,10 @@ $config['block']['blogs']['row'] = 10; // сколько записей выв
*/
$config['path']['root']['web'] = 'http://'.$_SERVER['HTTP_HOST']; // полный WEB адрес сайта
$config['path']['root']['server'] = $_SERVER['DOCUMENT_ROOT']; // полный путь до сайта в файловой системе
/**
* Для CLI режима использовать
* $config['path']['root']['server'] = dirname(dirname(__FILE__)); // полный путь до сайта в файловой системе
*/
$config['path']['root']['engine'] = $config['path']['root']['server'].'/engine'; // полный путь до сайта в файловой системе;
$config['path']['root']['engine_lib'] = $config['path']['root']['web'].'/engine/lib'; // полный путь до сайта в файловой системе
$config['path']['static']['root'] = $config['path']['root']['web']; // чтоб можно было статику засунуть на отдельный сервер
@ -298,6 +302,7 @@ $config['module']['lang']['delete_undefined'] = true; // Если устано
// Модуль Notify
$config['module']['notify']['delayed'] = false; // Указывает на необходимость использовать режим отложенной рассылки сообщений на email
$config['module']['notify']['insert_single'] = false; // Если опция установлена в true, систему будет собирать записи заданий удаленной публикации, для вставки их в базу единым INSERT
$config['module']['notify']['per_process'] = 10; // Количество отложенных заданий, обрабатываемых одним крон-процессом
// Какие модули должны быть загружены на старте
$config['module']['autoLoad'] = array('Cache','Session','User', 'Lang', 'Message');

View file

@ -43,13 +43,6 @@ class Cron extends Object {
if(!empty($sLockFile)) {
$this->oLockFile=fopen($sLockFile,'a');
/**
* Если процесс заблокирован, выкидываем исключение
*/
if($this->isLock()) {
throw new Exception('Try to exec already run process');
}
$this->setLock();
}
}
@ -74,16 +67,12 @@ class Cron extends Object {
* @param ( string|array ) $sFunction
* @param array $aArgs
*/
public function Exec($sFunction, $aArgs) {
public function Exec() {
/**
* Если выполнение процесса заблокирован, завершаемся
*/
if($this->isLock()) {
return;
}
if(!function_exists($sFunction)||!is_callable($sFunction)) {
throw new Exception('Undefined function given');
throw new Exception('Try to exec already run process');
}
/**
* Здесь мы реализуем дополнительную логику:
@ -91,12 +80,14 @@ class Cron extends Object {
* буферизация вывода.
*/
ob_start();
call_user_func_array($sFunction,$aArgs);
$this->Client();
/**
* Получаем весь вывод функции.
*/
$sContent=ob_get_contents();
ob_end_clean();
return $sContent;
}
/**
@ -105,10 +96,15 @@ class Cron extends Object {
public function Shutdown() {
$this->unsetLock();
}
public function __destruct() {
$this->Shutdown();
return;
}
/**
* Клиентская функция будет переопределятся в наследниках класса
* для обеспечивания выполнения основного функционала.
*/
public function Client(){
throw new Exception('Call undefined client function');
}
}
?>

View file

@ -17,6 +17,7 @@
set_include_path(get_include_path().PATH_SEPARATOR.dirname(__FILE__));
require_once(Config::Get('path.root.engine').'/lib/internal/ProfilerSimple/Profiler.class.php');
require_once("Object.class.php");
require_once("Block.class.php");
require_once("Hook.class.php");

2
include/cron/.htaccess Normal file
View file

@ -0,0 +1,2 @@
Order Deny,Allow
Deny from all

60
include/cron/notify.php Normal file
View file

@ -0,0 +1,60 @@
<?php
/*-------------------------------------------------------
*
* LiveStreet Engine Social Networking
* Copyright © 2008 Mzhelskiy Maxim
*
*--------------------------------------------------------
*
* Official site: www.livestreet.ru
* Contact e-mail: rus.engine@gmail.com
*
* GNU General Public License, version 2:
* http://www.gnu.org/licenses/old-licenses/gpl-2.0.html
*
---------------------------------------------------------
*/
define('SYS_HACKER_CONSOLE',false);
$sDirRoot=dirname(dirname(dirname(__FILE__)));
set_include_path(get_include_path().PATH_SEPARATOR.$sDirRoot);
chdir($sDirRoot);
require_once($sDirRoot."/config/loader.php");
require_once($sDirRoot."/engine/classes/Cron.class.php");
class NotifyCron extends Cron {
/**
* Выбираем пул заданий и рассылаем по ним e-mail
*/
public function Client() {
$aNotifyTasks = $this->oEngine->Notify_GetTasksDelayed(Config::Get('module.notify.per_process'));
if(empty($aNotifyTasks)) {
print PHP_EOL."No tasks are found.";
return;
}
/**
* Последовательно загружаем задания на публикацию
*/
$aArrayId=array();
foreach ($aNotifyTasks as $oTask) {
$this->oEngine->Notify_SendTask($oTask);
$aArrayId[]=$oTask->getTaskId();
}
print PHP_EOL."Send notify: ".count($aArrayId);
/**
* Удаляем отработанные задания
*/
$this->oEngine->Notify_DeleteTaskByArrayId($aArrayId);
}
}
$sLockFilePath=Config::Get('sys.cache.dir').'notify.lock';
/**
* Создаем объект крон-процесса,
* передавая параметром путь к лок-файлу
*/
$app=new NotifyCron($sLockFilePath);
print $app->Exec();
?>