Search code examples
crontimeoutprestashopcdncloudflare

Using long running Cron Jobs with Prestashop and Cloudflare CDN


So I've got Cloudflare and Prestashop running together to improve load times however a side effect of the CDN is that my cronjobs can only run for a maximum of 90 seconds. Any longer and Cloudflare will send out a 524 error and the cronjob will not be properly launched.

According to Cloudflare there are two ways to get around this problem. I can either reduce the size of the cronjob process so it fits within the 90 second window (which is not an option) or I can run the cronjobs on a separate subdomain that Cloudflare has no effect on. The problem with this option however is that Prestashop has something built into it so that regardless of what subdomain you use to visit the site it simply redirects to the main domain.

Does anyone have experience with this kind of issue and if so what are the best methods of getting around the problem. Thanks!


Solution

  • So after a fair bit of time I managed to resolve this issue. I had to modify a few of prestashop's controllers to add an exception for my particular subdomain. I needed to amend an if statement on line 370 of shop.php (classes/shop/Shop.php) so that it looks like this (make sure you update "exemption.myshop.com" with your subdomain:

     if ((!$id_shop && defined('_PS_ADMIN_DIR_')) || Tools::isPHPCLI() || in_array($http_host, $all_media) || $http_host == 'exemption.myshop.com') {
    

    I then had to modify two functions in frontController.php (classes/controllers/frontController.php). I needed to add the following piece of code to the top of both the sslRedirection and canonicalRedirection functions above everything else inside each of the functions.

            if (Tools::getHttpHost() == 'exemption.myshop.com'){
          return;
      }
    

    Then I deleted class_index.php from the cache folder on the main directory and the changes were made. You can test to see if it has worked by visiting the subdomain, it should load the page without changing the url.