How to Setup a Cron Job in Magento Module

How to setup a cron job in Magento module?

In your modules config.xml put the following:

<config>
<global>
<models>
<roomstoryinvoicecron>
<class>Roomstory_Invoice_Model</class>
</roomstoryinvoicecron>
</models>
</global>
<crontab>
<jobs>
<roomstoryinvoicecron>
<schedule>
<cron_expr>*/10 * * * *</cron_expr>
</schedule>
<run>
<model>roomstoryinvoicecron/observer::setStatus</model>
</run>
</roomstoryinvoicecron>
</jobs>
</crontab>
</config>

In app/code/local/Roomstory/Invoice/Model/Observer.php add the following:

<?php
class Roomstory_Invoice_Model_Observer {
public function setStatus() {
Mage::log("WORKS!");
}
}

Make sure logging is enabled and it should work, check the log to be sure ;)

How to setup cron dynamically via admin in Magento 2

Magento2 has a different scheme to merge layout config so you have to create a new file that called crontab.xml under your_custom_module/etc folder. And then you can add your cron config like this one:

<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:module:Magento_Cron:etc/crontab.xsd">
<group id="default">
<job name="custom_cronjob" instance="YourVenDoerName\CustomModule\Cron\Test" method="execute">
<schedule>* * * * *</schedule>
</job>
</group>
</config>

How to setup magento cron job and init action of the controller?

As far as I have ever seen, you cannot directly run controller actions in a cronjob, since that is not a terribly sensible thing to do outside of a user context. The easiest thing to do here is to put that functionality you need into a model and call that from the cronjob.

More information on the controller action you want to fire would be helpful in giving further advice.

Hope that helps!

Thanks,
Joe



Related Topics



Leave a reply



Submit