0

I implemented a custom plugin that loops through all products and generates a CSV file.

Even if the execute() function has the same code in both files (cron job and controller), the CSV file generated is slightly different. The controller generates the CSV as expected, but the cron job is not doing that.

For example, the controller generates a CSV file with 1000+ products, but the cron job only 400 products. The images generated by the controller are the ones that are assigned to the products, but the ones generated by the cron job are only placeholder images.

I don't know the source of these differences, but it is strange.

Are there any differences in Magento 2 between cron jobs and controllers that should be considered?

Thank you!

1
  • It will be better if you share the code that what you have done, so better help will be provided.
    – Yogita
    Jan 17 at 5:20

2 Answers 2

1

Yes, the main difference between running functions via cron and a controller is the area you are running the function. The controller is probably launched in frontend area and the cron job in crontab. The difference is that many settings depend on the area, you have many more plugins and observers in frontend area. Check Magento\Framework\App\Area for possible areas and check modules etc catalog and see that some of them have a separation for frontend,adminhtml etc.

PS there is a way to change the area, just inject via DI Magento\Framework\App\State class and use setAreaCode() function.

PS2 without the source code is hard to say so I assume that in both situations you used the same code, but I don't think that the product count should be different depending on the area, images yes, but the product count I think that should be the same

1

There could be a few reasons why the output generated by a cron job in Magento 2.4.1 is different from the output generated by a controller. Some potential causes include:

  1. Differences in environment: Cron jobs run on the server, while controllers run on the client. There may be differences in the environment that affect the output, such as different PHP versions or configurations.
  2. Differences in dependencies: Cron jobs and controllers may have different dependencies that affect the output. For example, a cron job may use a different database connection or configuration than a controller.
  3. Differences in user context: Cron jobs run as the server user, while controllers run as the client user. This can affect the output if the code relies on user-specific data or permissions.
  4. Differences in timing: Cron jobs are designed to run at specific intervals, while controllers are triggered by user requests. This can affect the output if the code relies on timing-specific data.
  5. Differences in code: There could be code differences between the cron job and the controller that affect the output. For example, the cron job may have different function calls, different variables or different parameters.

Also If you can't understand so please share your code.

Let me know if any query.

THANKS.

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.