Background Info:
I have what seemed like a simple task, send an xml with stock and price data to an ftp location every hour. So a php script with a cron job should do the task.
This data is being retrieved from an existing magento shop. The cron setup is working because we use cron jobs for a lot of other tasks. To run do this task I set up a simple extension structure running the below code from a model. Seeing as magento recognizes the extension and the cron job is being scheduled I don't believe that the problem is magento in this case, rather something in the actual php code.
The Problem:
The code below has been getting the better of me for the last 2 days, searching google as well as Stack Overflow has not yielded results. When I run the code manually I get a result, the file is created and the script needs about 1 minute to complete. However if I schedule the cron job the script runs for about 4 hours after which I get the message :
Job was running longer than the configured max_running_time
The Code:
<?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);
}
}
?>
I did for security reasons block out the ftp username and password as well as change the ftp server. I am sure that that data is correct in the script.
What have I tried:
Save the dom document with save($nameOfFile) as well as saveXML().
Save the file to disk in current folder as a temp location after
which I could connect to ftp.fopen(php://memory) as well as fopen(php://temp).
In different scenarios using either ftp_put or ftp_fput.
- set all locations that might be used to file permission 777 (except php://memory and temp, I don't believe this is necessary?)
Edit:
Here is the 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>