0

Can I trigger a cron job at the time of form submission?

Actually, I need to download a product pdf when the form gets submitted.

But my pdf file has a large amount of data. So the site gets slow down.

So that I have moved on to cron.

I've successfully set a cron by referring this link.

Here is my code,

/etc/crontab.xml

<?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="test_cronjob" instance="Test\CatalogPrint\Cron\Pdfdownload" method="execute">
        <schedule>*/1 * * * *</schedule>
    </job>
</group>
</config>

Cron/Pdfdownload.php

<?php

namespace Test\CatalogPrint\Cron;
class Pdfdownload extends \Magento\Framework\App\Action\Action 
{
    // doing my stuff on here
}
?>

And my form in the template file,

form.phtml

<form action="<?php echo $block->getFormAction() ?>" method="post" class="downloadPDF-account">
    <label>Brochure Name: <input name="pdfname" type="text" /></label>
    <input type="submit" value="Download" />
</form>

Actually now in this form action <?php echo $block->getFormAction() ?>' , I given my controller path.

But I need to run my cron file (Test\CatalogPrint\Cron\Pdfdownload) on here.

If I'm doing the same stuff on my controller, means it takes too much time and slow down my site.

Please help me. I am a novice in magento and I am stuck at this point. Thank you in advance!!

1 Answer 1

6

No, you cannot trigger a cron on any particular event. But you can do something like this in your case.

  1. Save the form in the database as submitted.
  2. Run a cron job maybe every minute. Check if there is any form submitted and email is not sent. Here generate the PDF and send it to the customer via email as an attachment.
  3. Mark the record as email sent so that next time when cron is executed it will not resend the email.

Hope it finds helpful.

Your Answer

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

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