我在Zend框架1.11.11中有一个脚本,它作为一个cron作业运行,它工作了大约10天然后停止了。该脚本采用XML并将内容插入数据库表。
当我使用SSH运行脚本时:
我有GoDaddy托管的豪华计划。
我运行的脚本代码是:
<?php
require_once 'init.php';
$started = Zend_Date::now();
$log = 'started: '.$started->toString('dd/MM/YYYY H:m:s').PHP_EOL;
$xmlOptions = $options['xml'];
$mapper = new Application_Model_Mapper();
$striper = new Zend_Filter_StripTags();
$count = 0;
try {
$client = new Zend_Http_Client($xmlOptions['url']);
$client->setConfig(array('timeout' => 60));
$request = $client->request();
if ($request->getStatus() != 404) {
file_put_contents('downloaded.xml', $request->getBody());
$items = simplexml_load_file('downloaded.xml',
'SimpleXMLElement',LIBXML_NOCDATA);
foreach ($items as $xml) {
if (!$mapper->exist($xml->{$xmlOptions['id']},
$xml->{$xmlOptions['store']})) {
$endDate = new Zend_Date($xml->{$xmlOptions['until']});
$diff = Zend_Date::now()->compare($endDate);
if ($diff == -1) {
$item =new Application_Model_Item();
$url = $xml->{$xmlOptions['image']};
$url_length = strlen($url);
$image_url = 'images/items/'.$xml->
{$xmlOptions['id']}.'_'.$xml->
{$xmlOptions['store']}. substr($xml->
$xmlOptions['image']},$url_length-4,4);
if (file_get_contents($url))
file_put_contents('../'.$xmlOptions['public'].'/'.
$image_url, file_get_contents($url));
$item->xml_id = $xml->{$xmlOptions['id']};
$item->title = $xml->{$xmlOptions['title']};
$item->description = $striper->filter($xml->
{$xmlOptions['description']});
$item->link = $xml->{$xmlOptions['link']};
$item->image = $image_url;
$mapper->addItem($item);
$count++;
}
}
}
}
$log .= "added: $count items".PHP_EOL;
$log .= 'removed: '.$mapper->removeExpiredItems().' items'.PHP_EOL;
}
catch (Exception $ex) {
$log .= PHP_EOL.'ERROR: '.$ex->getLine().':'.$ex->getMessage().PHP_EOL;
}
$end = Zend_Date::now();
$log .= 'ended: '.$end->toString('dd/MM/YYYY H:m:s').PHP_EOL;
$elapsed = $end->sub($started);
$log .= 'elapsed: '.$elapsed->toString('m:s').PHP_EOL;
$log .= 'Errors'.PHP_EOL.$errors;
echo PHP_EOL.PHP_EOL.PHP_EOL.$log;
$h = Zend_Date::now()->toString('dd_MM H_m_s');
file_put_contents('logs/xml '.$h.'.log', $log);
?>
和init.php:
<?php
ignore_user_abort(true);
set_time_limit(600);
ini_set('memory_limit', '96M');
//log errors
set_error_handler('err_handle',E_ALL);
$errors = '';
function err_handle($code,$string,$file,$line,$context) {
$errors .= $string.PHP_EOL;
}
defined('APPLICATION_PATH')
|| define('APPLICATION_PATH', realpath(dirname(__FILE__) . '/../application'));
// Define application environment
defined('APPLICATION_ENV')
|| define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') :
'development'));
set_include_path(implode(PATH_SEPARATOR, array(
realpath(APPLICATION_PATH . '/../library'),
get_include_path(),
)));
/** Zend_Application */
require_once 'Zend/Application.php';
$application = new Zend_Application(
APPLICATION_ENV,
APPLICATION_PATH . '/configs/application.ini');
$options = $application->getOptions();
$application->bootstrap();
?>