Melvin
Melvin

Reputation: 33

PHP script running through cron job freezes. Can't find the cause, please help me

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:

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>

Upvotes: 3

Views: 1142

Answers (1)

ToxaBes
ToxaBes

Reputation: 1587

Add
exit;
after
ftp_close($conn);

If its not help, you need to check script from frontend:

Add next section in config.xml:

<frontend>
    <routers>
        <sendstock>
            <use>standard</use>
            <args>
                <module>VuyaniSoftware_NeckConnection</module>
                <frontName>sendstock</frontName>
            </args>
        </sendstock>
    </routers>   
</frontend>

Create file app/code/../VuyaniSoftware/NeckConnection/controllers/TestController.php with next content:

<?php

class VuyaniSoftware_NeckConnection_TestController extends Mage_Core_Controller_Front_Action {

    public function runAction() {
        Mage::getModel('neck_connection/stock_update')->sendStock();        
        echo 'Done';
    }
}

Clear Magento Cache and open in browser:

http://your_site.com/sendstock/test/run

If all fine, you can test it via cron:

52 * * * * /usr/bin/wget --no-check-certificate -O /var/log/cron_result.log http://your_site.com/sendstock/test/run

Upvotes: 1

Related Questions