【问题标题】:Magento: monitoring execution time of all cron jobs?Magento:监控所有 cron 作业的执行时间?
【发布时间】:2013-01-07 12:19:07
【问题描述】:

我继承了一个基于 Magento Enterprise 1.9 的网站,该网站有很多定期运行的 cron 作业。

这些 cron 作业由第三方模块配置,不是 Magento 核心的一部分。

一组作业处理将数据文件从 Magento 导出和导入到仓库管理系统,有时 cron 作业会卡在某种循环中,并且会持续处理数小时,而本来应该只需要几分钟。

我想以某种方式监控这些作业需要多长时间,但不修改每个脚本本身。也许某种观察者,如果这样的事情是可能的?

有什么想法吗?

【问题讨论】:

标签: php magento


【解决方案1】:

可以从core_schedule 表中收集有关 Magento 中计划作业的调度和运行的基本信息。请注意,根据系统 > 配置 > 系统 > Cron 计划 中的设置添加和删除条目。可以从任何给定模块的 config.xml 中的 <crontab /> 节点确定发起的计划作业配置(类、方法和 cron 表达式)。

其中许多可以通过使用Fabrizio Branca's AOE_Scheduler module (link) 在管理员中进行可视化和交互。

要捕获性能(内存使用情况、开始/停止时间),可能需要在这些 cron 触发的脚本中删除一些日志记录调用。

【讨论】:

    【解决方案2】:

    构建一个脚本,该脚本执行从 cron_schedule 表创建的格式化表。

    我知道发布链接的常见做法,但 MagentoCommerce.com 可能不会很快消失,在这里发布代码是一个皇家 PITA。 So here's the link to a PHP script that reads cron_schedule and creates a full report. 代码见底部。进入系统配置 cron 设置并告诉它保存信息 24 小时。

    您现在可以看到正在计划的所有内容以及已执行的所有内容,如以下输出示例所示。

    很抱歉链接上的 MageBayvaporation,我试图将最重要的东西移到这里,但错过了这个。幸运的是,它仍在其中一个站点上运行。

    <?php
    
    /******************************************************
     * Magento Log File Contents Monitor.
     * Released under the Digital Damnation Copyright
     * Free to do whatever the 7734 you want to do with it.
     * provided without warranty or support
     ******************************************************/
    
    /***********************
     * Scan Magento local.xml file for connection information
     * Run out of password protected scripts/ directory
     ***********************/
    
    if (file_exists('../app/etc/local.xml')) {
    
    $xml = simplexml_load_file('../app/etc/local.xml', NULL, LIBXML_NOCDATA);
    
    $db['host'] = $xml->global->resources->default_setup->connection->host;
    $db['name'] = $xml->global->resources->default_setup->connection->dbname;
    $db['user'] = $xml->global->resources->default_setup->connection->username;
    $db['pass'] = $xml->global->resources->default_setup->connection->password;
    $db['pref'] = $xml->global->resources->db->table_prefix;
    
    $tables = array(
       'dataflow_batch_export',
       'dataflow_batch_import',
       'log_customer',
       'log_quote',
       'log_summary',
       'log_summary_type',
       'log_url',
       'log_url_info',
       'log_visitor',
       'log_visitor_info',
       'log_visitor_online',
       'catalog_compare_item',
       'report_event',
       'report_compared_product_index',
       'report_viewed_product_index',
       'sales_flat_quote',
       'sales_flat_quote_address',
       'sales_flat_quote_address_item',
       'sales_flat_quote_item',
       'sales_flat_quote_item_option',
       'sales_flat_quote_payment',
       'sales_flat_quote_shipping_rate'
    );
    
    } 
    
    else {
        exit('Failed to open ../app/etc/local.xml');
    }
    
    /** Get current date, time, UTC and offset **/
    
    $date   = date("Y-m-d");
    $time   = date("H:i:s T");
    $offset = date("P");
    $utc    = gmdate("H:i:s");
    
    /***********************
     * Start HTML output
     ***********************/
    
    echo '<html><head><title>Magento Log File Contents on ' . $date . ' ' . $time . '</title>
    <meta http-equiv="refresh" content="30">
    <style type="text/css">html {width: 100%; font-family:  Arial,Helvetica, sans-serif;}
    body {line-height:1.0em; font-size: 100%;}
    table {border-spacing: 1px;}
    th.stattitle {text-align: left; font-size: 100%; font-weight: bold; color: white; background-color: #101010;}
    th {text-align: center; font-size: 90%; font-weight: bold; padding: 5px; border-bottom: 1px solid black; border-left: 1px solid black; }
    td {font-size: 90%; padding: 4px; border-bottom: 1px solid black; border-left: 1px solid black;}
    </style>
    </head><body>';
    
    /** Output title, connection info and cron job monitor runtime **/
    
    echo '<h2>Magento Log File Contents Report</h2><h3>Connection: '.$db['user'].'@'.$db['host'].'&nbsp;&nbsp;&ndash;&nbsp;&nbsp;Database: ' . $db['name'] . '</h3>';
    echo '<h3>Runtime: ' . $date . '&nbsp;&nbsp;&nbsp;' .$time . '&nbsp;&nbsp;&nbsp;' . $utc . ' UTC</h3>';
    echo '<h4>Note: Your timezone offset is ' . $offset . ' hours from UTC</h4>';
    
    /** Connect to database **/
    
    $conn = mysql_connect($db['host'],$db['user'],$db['pass']);
    @mysql_select_db($db['name']) or die(mysql_error());
    
    /***********************
     * Get table record counts
     ***********************/
    
    echo '<table><tbody><tr><th class="stattitle" colspan="2">Log Tables</th></tr>';
    echo '<tr><th>Table</th><th>Row Count</th></tr>';
    
    foreach($tables as $tblname) {
       $result  = mysql_query('SELECT COUNT(*) FROM ' . $db['pref'] . $tblname) or die(mysql_error());
       $numrows = mysql_fetch_array($result);
       $num     = $numrows[0];
    
       /* Table output */
       echo '<tr>';
       echo '<td>'.$db['pref'].$tblname.'</td>';
       echo '<td align="right">'.$num.'</td>'; 
       echo '</tr>';                 
    }
    
    echo '</tbody></table></body></html>';
    
    /***********************
     * End of report
     ***********************/
    
    mysql_close($conn);
    ?> 
    

    【讨论】:

    • 不客气。这对让触发 cron 进程正常运行非常有帮助。 Web 服务器有时是偏执狂和泡泡糖包装纸的奇怪小混合物。我遇到过拒绝从 crontab 运行 Magento 的 cron.sh 的系统,所以你最终会直接触发 cron.php。此外,很高兴看到在模块设置 xml 中启用 cron 部分实际上是在拿起它的末端并触发。
    • 我设法找到了不同的来源,但我很感激寻找和添加代码的努力:) 它肯定会帮助其他人。
    【解决方案3】:

    我有一个客户也有同样的担忧,他们没有看到作业运行了多长时间,也没有很好地了解他们的 cron 中发生的事情。有一些解决方案,但他们并没有完全做到他们想要的,所以我最终构建了一个小型服务,我称之为Job Health Data Vault。任何人都可以免费使用。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-12-13
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      相关资源
      最近更新 更多