Skip to main content
1 of 3
Manjunath
  • 187
  • 7
  • 31

How to execute Cron from controller file in magento2

I have created a UI form admin html.

I have a requirement when the form is saved, Need to fetch data from custom module and perform some action.

The logic is working but if I have records of more than 1000, then getting timeout issue.

So, I have written a cron file to do that, So my question here is Can I call the cron from my controller file?

As like below

class Controller{ 

protected $cronFile;

/** * Constructor * * @param \Vendor\Module\Cron\Custom $cronFile */ public function __construct( \Vendor\Module\Cron\Custom $cronFile ){ $this->cronFile = $cronFile; } public function execute() { $this->cronFile->execute(); // another logic } 

}

This way if I use, Unless the cron sucess, I still get the loading screen when save button is clicked.

Is there any other easy method to achieve this. Thanks!

Manjunath
  • 187
  • 7
  • 31