drgm51600 2014-07-31 08:32
浏览 53
已采纳

通过cron作业运行的PHP脚本冻结。 找不到原因,请帮帮我

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>
  • 写回答

1条回答 默认 最新

  • dongling4288 2014-08-01 10:28
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?