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 jupyterthemes 设置完毕后没有效果
  • ¥15 matlab图像高斯低通滤波
  • ¥15 针对曲面部件的制孔路径规划,大家有什么思路吗
  • ¥15 钢筋实图交点识别,机器视觉代码
  • ¥15 如何在Linux系统中,但是在window系统上idea里面可以正常运行?(相关搜索:jar包)
  • ¥50 400g qsfp 光模块iphy方案
  • ¥15 两块ADC0804用proteus仿真时,出现异常
  • ¥15 关于风控系统,如何去选择
  • ¥15 这款软件是什么?需要能满足我的需求
  • ¥15 SpringSecurityOauth2登陆前后request不一致