I have a Magento Cron which works fine when I call it using the CLI. I want to call the same Cron in a Controller. Is it possible to do it? (ie) I want to run the execute() function in the Cron script via the Controller code.
-
Do you just want to test how your cron works by calling it from the controller or anything else?– Smita KagwadeCommented Sep 13, 2022 at 8:32
-
@SmitaKagwade I actually want to make it such that on click of a button, the cron should execute. Hence I want the button to make a call to a controller, which can then in turn call the cron.– Lennox SherwinCommented Sep 13, 2022 at 8:39
Add a comment
|
1 Answer
suppose your cron file is in app/code/Vendor/Module/Cron/RemoveProcut.php
, then you can call the cron method in the controller like this :
class YourController extend anyClass{
protected $cronRemoveProduct;
/**
* Constructor
*
* @param \Vendor\Module\Cron\RemoveProcut $cronRemoveProduct
*/
public function __construct(
\Vendor\Module\Cron\RemoveProcut $cronRemoveProduct
){
$this->cronRemoveProduct = $cronRemoveProduct;
}
public function execute()
{
$this->cronRemoveProduct->execute();
// another logic
}
}
hope this help
-
-
-
-
@DekiAkbar, suppose if we are redirecting to some page in the controller ex admin custom save controller, the page will show as loading till the cron execute function completed. is there any way to avoid this? Commented Jan 27, 2023 at 6:05