2

I have created a module for cron job in magento.

I have created a Observer

public function abc()
{
//my action here
}

I have created cron job for this in config.xml file.

<crontab>
            <jobs>
                <Mbyte_abcd>
                        <schedule>
                            <cron_expr>* * * * *</cron_expr>
                        </schedule>
                        <run>
                    <model>abcd/observer::abc</model>
                                   </run>
        </Mbyte_abcd>
    </jobs>
</crontab>

This is working fine for me.

Now my question is i want to run cron job for my controller.

public function cdeAction()
{
//my action here
}
  1. Is this possible to run cron job for controller?
  2. Is this possible to use to use controller method in observer?

If yes please explain how.

Deepak Mankotia
  • 4,404
  • 6
  • 30
  • 55

2 Answers2

2

The purpose of controllers is to manage request/responses registered by user actions. In order to do your trick here and call the function from controller you have some choices:

  1. do a redirect from observer to controller action
  2. copy the controller function into observer (with proper changes)
  3. register a new cron in server that makes a wget/url call to your controller action

Regards

aki
  • 699
  • 7
  • 25
  • I am us $this->_redirect('module/controller/action'); in my observer but this is not redirecting to controller method. – Deepak Mankotia Oct 01 '14 at 08:56
  • @DeepakMankotia You need to access your server files or cPanel and register a new cron job. – aki Oct 01 '14 at 12:01
  • @DeepakMankotia Also, you can try digging into `Mage::getControllerInstance()` command and try various experiments with it. You can find it in app/Mage.php. I personally didn't used this so far. – aki Oct 01 '14 at 12:05
0

create a controller in magento 2, in the action of controller create a function with the help of objectmanager and call the cron function as shown below.

$objectManager = \Magento\Framework\App\ObjectManager::getInstance();           
$cronvariable = $objectManager->create(\Vendor\module\Cronname\foldername\function::class);
               $cronvariable->execute();
vimuth
  • 5,064
  • 33
  • 79
  • 116