Reputation: 1135
I'm trying to write a simple extension that registers a cron job for magento and there seems to be something I'm missing out here. The cron job is never scheduled there is no entry for it in cron_schedule table. I even tried entering one there and the job was never executed. The default magento cron job's are running fine. And by looking at the /var/log/syslog I can see the cron.sh being executed. The extension is very simple. Just a model (Observer.php) and config.xml that should set up the cron job. I feel like a fool. From what I read online this should be fairly simple to set up and yet I'm getting nowhere with it.
Here is the content of 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>
Here is the Twobuy_Import.xml in app/etc/modules (the extension is showed under system->configuration->advanced)
<?xml version="1.0"?>
<config>
<modules>
<Twobuy_Import>
<active>true</active>
<codePool>local</codePool>
</Twobuy_Import>
</modules>
</config>
And the 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!')
}
}
}
Also here is my cron job configuration from system->configuration->system:
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
Upvotes: 1
Views: 2429
Reputation: 1209
Run cron like this so you get an error log to read:
* * * * * /usr/bin/php /home/magento/public_html/cron.php >> /home/magento/public_html/var/log/cron.log 2>&1
Also pop this extension on: http://connect20.magentocommerce.com/community/Aoe_Scheduler it will let you run cron jobs whenever you want, clear the schedule and rebuild it.
No I cannot tell you why your extension is not working, however, with logging and control over your extension you should be on easy street.
You may also want this:
config.xml:
<events>
<mymodule_update_stuff>
<observers>
<mymodule_update_stuff_handler>
<type>model</type>
<class>mymodule/observer</class>
<method>updateStuff</method>
<args/>
</mymodule_update_stuff_handler>
</observers>
</mymodule_update_stuff>
</events>
....
<crontab>
<jobs>
<mymodule_updatestuff>
<schedule>
<!--<cron_expr>*/15 * * * *</cron_expr> -->
<config_path>mymodule/misc/frequency</config_path>
</schedule>
<run>
<model>mymodule/observer::updateStuff</model>
</run>
</mymodule_updatestuff>
</jobs>
</crontab>
Now in system.xml:
<config>
<sections>
<mymodule translate="label" module="mymodule">
<label>Whatever</label>
<tab>catalog</tab>
<frontend_type>text</frontend_type>
<sort_order>200</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
<groups>
<misc translate="label">
<label>Import Options</label>
<frontend_type>text</frontend_type>
<sort_order>0</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>1</show_in_website>
<show_in_store>0</show_in_store>
<fields>
<frequency translate="label">
<label>Update Frequency</label>
<frontend_type>text</frontend_type>
<sort_order>100</sort_order>
<show_in_default>1</show_in_default>
<show_in_website>0</show_in_website>
<show_in_store>0</show_in_store>
</frequency>
You will now be able to put your cron schedule in the admin config for your module rather than have it hard wired into your config.xml. This makes it easier to develop a cron-style module.
Upvotes: 2