背景信息:
我有一个看似简单的任务,每小时向ftp位置发送一个包含股票和价格数据的xml。因此,一个带有cron作业的php脚本应该完成这项任务。
这些数据是从一家现有的magento商店中检索到的。cron设置之所以有效,是因为我们将cron作业用于许多其他任务。为了运行这个任务,我设置了一个简单的扩展结构,从模型中运行下面的代码。由于magento识别出了扩展,并且cron作业正在计划中,我不认为在这种情况下问题出在magento身上,而是实际php代码中的问题。
问题:
在过去的两天里,下面的代码一直在战胜我,搜索谷歌和Stack Overflow都没有结果。当我手动运行代码时,我会得到一个结果,文件被创建,脚本需要大约1分钟才能完成。但是,如果我安排cron作业,脚本会运行大约4个小时,之后我会收到消息:
作业运行的时间超过了配置的最大运行时间
代码:
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
set_time_limit(0);
class VuyaniSoftware_NeckConnection_Model_StockUpdate
{
public function sendStock(){
//get the relavent products:
$attributeVarientId = 'Ja';
$attributeCode = 'neck_active';
$products = Mage::getModel('catalog/product')
->getCollection()
->addAttributeToSelect(array('sku','name','description','price','special_price',))
->addFieldToFilter(array(
array('attribute'=>'neck_active','eq'=>'Ja'),
array('attribute'=>'type_id','eq'=>'configurable'),
array('attribute'=>'status','eq' => Mage_Catalog_Model_Product_Status::STATUS_ENABLED),
))
->load();
//create the structure for the xml feed:
$xmlDoc = new DOMDocument();
$root = $xmlDoc ->appendChild($xmlDoc ->createElement("NECK_STOCK"));
foreach ($products as $product) {
$childProducts = Mage::getModel('catalog/product_type_configurable')->getUsedProducts(null,$product);
foreach($childProducts as $childProduct){
$ARTICLE = $root->appendChild($xmlDoc->createElement("product"));
$ARTICLE->appendChild($xmlDoc->createElement("mpn", $childProduct->sku));
$stock = Mage::getModel('cataloginventory/stock_item')->loadByProduct($childProduct)->getQty();
$ARTICLE->appendChild($xmlDoc->createElement("stock", round($stock,0)));
$special_price=$product->special_price;
$ARTICLE->appendChild($xmlDoc ->createElement("salesPrice",round($product->price,2)));
if(($special_price !== NULL) && ($special_price !== 0)){
$ARTICLE->appendChild($xmlDoc ->createElement("special_price",round($product->special_price,2)));
}else{
$ARTICLE->appendChild($xmlDoc ->createElement("special_price",round($product->price,2)));
}
}
}
//Save the feed to memory:
$xmlDoc->formatOutput = true;
$nameoffile="Stock_Export_".date("Ymd_His", time()+120*60).".xml";
$data = $xmlDoc->saveXML();
$tempHandle = fopen('php://memory', 'r+');
fwrite($tempHandle, $data);
rewind($tempHandle);
// Connect to server //Job was running longer than the configured max_running_time
$conn = ftp_connect("ftp.example.com") or die("Could not connect");
ftp_login($conn,"***","***") or die("invelid username or password");
// move to path where you need to upload file
ftp_chdir($conn, "/Team Folders/NeckData/StockData") or die("could not find dir");
ftp_pasv($conn, true);
// upload file to particular path
$upload = ftp_fput($conn, $nameoffile, $tempHandle, FTP_BINARY);
if (!$upload) { echo 'FTP upload failed!'; }
ftp_close($conn);
}
}
?>
出于安全考虑,我屏蔽了ftp用户名和密码,并更改了ftp服务器。我确信脚本中的数据是正确的。
我试过什么:
使用Save($nameOfFile(和saveXML((保存dom文档。
之后将文件保存到当前文件夹中的磁盘作为临时位置我可以连接到ftp。fopen(php://memory)以及fopen(php://temp)。
在使用ftp_put或ftp_fput的不同场景中。
- 设置可能用于归档权限777的所有位置(除了php://memory和temp,我认为这没有必要?(
编辑:
这是config.xml:
<?xml version="1.0" encoding="UTF-8"?>
<!-- The root node for Magento module configuration -->
<config>
<modules>
<VuyaniSoftware_NeckConnection>
<version>0.0.1</version>
</VuyaniSoftware_NeckConnection>
</modules>
<global>
<models>
<NeckConnection>
<class>VuyaniSoftware_NeckConnection_Model</class>
</NeckConnection>
</models>
</global>
<crontab>
<jobs>
<NeckConnection_StockUpdate>
<schedule><cron_expr>52 */1 * * *</cron_expr></schedule>
<run><model>NeckConnection/StockUpdate::sendStock</model></run>
</NeckConnection_StockUpdate>
</jobs>
</crontab>
</config>
添加exit;
之后ftp_close($conn);
如果没有帮助,您需要从前端检查脚本:
在config.xml中添加下一节:
<frontend>
<routers>
<sendstock>
<use>standard</use>
<args>
<module>VuyaniSoftware_NeckConnection</module>
<frontName>sendstock</frontName>
</args>
</sendstock>
</routers>
</frontend>
创建文件app/code//VuyaniSoftware/NeckConnection/controllers/TestController.php与下一个内容:
<?php
class VuyaniSoftware_NeckConnection_TestController extends Mage_Core_Controller_Front_Action {
public function runAction() {
Mage::getModel('neck_connection/stock_update')->sendStock();
echo 'Done';
}
}
清除Magento缓存并在浏览器中打开:
http://your_site.com/sendstock/test/run
如果一切正常,您可以通过cron:进行测试
52***/usr/bin/wget--无检查证书-O/var/log/cron_result.loghttp://your_site.com/sendstock/test/run