1

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!

3
  • I have one possible idea you disable you can cron job using crontab.xml file and using controller you can set cron job Schedule with 1 min. When controller call you can Schedule cron job manully and it's may be work. If you need code let know. Commented Jan 27, 2023 at 14:05
  • @MohitPatel, Thank you for your suggestion. can you please share the sample code example of controller and cron files
    – Manjunath
    Commented Jan 27, 2023 at 16:50
  • Also, This need to call when I call the admin UI save controller, As soon as I click save, it should save and reload the form, Internally cron should run.
    – Manjunath
    Commented Jan 27, 2023 at 16:54

1 Answer 1

0

It is not recommended to call a cron job directly on form save action. Instead, you can use ajax call for your requirement, So that after the form submission, it will call the controller action and perform the cron job in the background. This way you can avoid the loading screen.

This controller action can be called when the form is saved, and it will trigger the cron job to run in the background, allowing the user to continue using the form without being blocked by the loading screen.

Hope this helps you to understand.

Happy coding!

1
  • could you please help me with the sampe code snippet how to make ajax call, As I am using the adminhtml UI form with button save controller. Thanks
    – Manjunath
    Commented Jan 27, 2023 at 7:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.