我正在尝试编写一个简单的扩展程序,为 magento 注册一个 cron 作业,我似乎在这里遗漏了一些东西。cron 作业从未计划过,在 cron_schedule 表中没有任何条目。我什至尝试在那里输入一个,但这项工作从未执行。默认的 magento cron 作业运行良好。通过查看 /var/log/syslog 我可以看到正在执行的 cron.sh。扩展非常简单。只是一个模型(Observer.php)和 config.xml 应该设置 cron 作业。我觉得自己像个傻瓜。从我在网上阅读的内容来看,这应该很容易设置,但我却一无所获。
这是config.xml的内容
<?xml version="1.0"?>
<config>
<modules>
<Twobuy_Import>
<version>1.0</version>
</Twobuy_Import>
</modules>
<global>
<models>
<twobuy_import>
<class>Twobuy_Import_Model</class>
</twobuy_import>
</models>
</global>
<crontab>
<jobs>
<twobuy_import>
<schedule>
<cron_expr>50 13 * * *</cron_expr> <!-- I set this value to say 10-20-30 minutes in the future, and wait, nothing ever happens -->
</schedule>
<run>
<model>twobuy_import/observer::parse</model>
</run>
</twobuy_import>
</jobs>
</crontab>
</config>
这是 app/etc/modules 中的 Twobuy_Import.xml(扩展名显示在 system->configuration->advanced 下)
<?xml version="1.0"?>
<config>
<modules>
<Twobuy_Import>
<active>true</active>
<codePool>local</codePool>
</Twobuy_Import>
</modules>
</config>
和 Observer.php
class Twobuy_Import_Model_Observer
{
public function parse()
{
$row = 1;
if (($handle = fopen(Mage::getBaseDir('var')."/orders/Stock/webstock.csv", "r")) !== FALSE)
{
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE)
{
if($row++ > 1)
{
if($product = Mage::getModel('catalog/product')->loadByAttribute('sku',$data[1]))
{
$product = Mage::getModel('catalog/product')->load($product->getEntityId());
$product->setPrice($data['3']);
$product->setAnalysiscode($data['4']);
if($data[5] == 'Y')
$stockData['is_in_stock'] = 0;
else
$stockData['is_in_stock'] = 1;
$product->setStockData($stockData);
try
{
$product->save();
}
catch (Exception $e)
{
Mage::log('Saving error with the following message: '.$e->getMessage());
Mage::log('Saving product:');
Mage::log($product->getData);
Mage::log('CSV line:');
Mage::log($data);
}
}
else
{
Mage::log('Import error, sku not found: '.$data[1]);
Mage::log('CSV line:');
Mage::log($data);
}
}
}
fclose($handle);
}
else
{
Mage::log('Import, no csv file found!')
}
}
}
这也是我来自系统->配置->系统的cron作业配置:
Generate Schedules Every 1
Schedule Ahead for 5
Missed if Not Run Within 20
History Cleanup Every 30
Success History Lifetime 60
Failure History Lifetime 600