• Version 4.5.8

    I’m using a cron job with the preloader URL, and while I am getting a “Preloader started” response back, the debug.log file shows the preloader is not running every time the cron job runs.

    Settings:
    -DISABLE_WP_CRON is enabled, and cron is set up to run wp-cron.php once a minute.
    -SWCFPC_CURL_TIMEOUT is set to 60.
    -SWCFPC_PRELOADER_MAX_POST_NUMBER is set to 1000. (The built-in FAQ says this is set to 1000 by default, but it’s actually 50.)
    -Cron is set up to run the preloader URL once per hour. The debug.log shows it’s actually only running every 3 hours.

Viewing 9 replies - 1 through 9 (of 9 total)
  • Plugin Contributor iSaumya

    (@isaumya)

    Are you running the WP Cron with PHP execution instead of wget?

    Thread Starter PurpleNurple

    (@purplenurple)

    WP Cron is disabled, and I’m running cron from my server. The issue is that Cloudflare is caching both your purge and preloader URLs. They work the first time, then they get cached, and won’t work again until the cache clears.

    These are the URLs I’m using – both are getting cached by Cloudflare:

    
    https://[domain]?swcfpc=1&swcfpc-purge-all=1&swcfpc-sec-key=[key]
    https://[domain]?swcfpc=1&swcfpc-preloader=1&swcfpc-sec-key=[key]
    • This reply was modified 2 years, 10 months ago by PurpleNurple.
    • This reply was modified 2 years, 10 months ago by PurpleNurple.
    Plugin Contributor iSaumya

    (@isaumya)

    I’m running cron from my server

    – Yes but when you do that you can wither do it via wget or via php. Example: https://www.looklinux.com/how-to-execute-php-script-in-crontab-in-linux/#Configure_Corn_Task

    The issue is that Cloudflare is caching both your purge and preloader URLs

    – I don’t understand. From the example log you have given above you are first running a purge request swcfpc-purge-all=1 and then the preloader request swcfpc-preloader=1

    Thread Starter PurpleNurple

    (@purplenurple)

    Yes, I am running WP Cron via PHP. It’s working fine.

    The two commands in my last post are not from a log. They are the commands shown within your plugin for purging and preloading, respectively. They would not be run at the same time. Both the purge and preloader pages are being cached by Cloudflare, because you are not setting the proper headers for those pages.

    Plugin Contributor iSaumya

    (@isaumya)

    If the request is sent via CRON job then the cache-control headers are supposed to be no-cache

    Thread Starter PurpleNurple

    (@purplenurple)

    Cache control headers are best handled at the server (reply), not the client (request). It would also seem to be fairly trivial to modify your plugin code to handle the reply headers. That said, I went ahead and added cache headers to my cron job, butit appears that Cloudflare is ignoring cache headers in the request.

    My new cron job uses the following code:
    curl -sv -H "cache-control: no-store, no-cache, max-age=0" -H "pragma: no-cache" "https://[domain]?swcfpc=1&swcfpc-preloader=1&swcfpc-sec-key=[key]"

    Cron request headers:

    GET /?swcfpc=1&swcfpc-preloader=1&swcfpc-sec-key=[key] HTTP/1.1
    User-Agent: curl/7.29.0
    Host: [domain]
    Accept: */*
    cache-control: no-store, no-cache, max-age=0
    pragma: no-cache

    First reply headers (after purging Cloudflare cache):

    HTTP/1.1 200 OK
    Date: Thu, 05 May 2022 18:18:05 GMT
    Content-Type: text/html; charset=UTF-8
    Transfer-Encoding: chunked
    Connection: keep-alive
    x-powered-by: PHP/7.4.29
    vary: Accept-Encoding
    alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400
    x-turbo-charged-by: LiteSpeed
    CF-Cache-Status: EXPIRED
    Last-Modified: Thu, 05 May 2022 17:19:04 GMT
    Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
    Report-To: [...]
    NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
    Server: cloudflare
    CF-RAY: [...]

    Second reply headers:

    HTTP/1.1 200 OK
    Date: Thu, 05 May 2022 18:19:01 GMT
    Content-Type: text/html; charset=UTF-8
    Transfer-Encoding: chunked
    Connection: keep-alive
    x-powered-by: PHP/7.4.29
    vary: Accept-Encoding
    alt-svc: h3=":443"; ma=86400, h3-29=":443"; ma=86400
    x-turbo-charged-by: LiteSpeed
    CF-Cache-Status: HIT
    Age: 56
    Last-Modified: Thu, 05 May 2022 18:18:05 GMT
    Expect-CT: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
    Report-To: [...]
    NEL: {"success_fraction":0,"report_to":"cf-nel","max_age":604800}
    Server: cloudflare
    CF-RAY: [...]
    Plugin Contributor iSaumya

    (@isaumya)

    Cache control headers are best handled at the server (reply), not the client (request). It would also seem to be fairly trivial to modify your plugin code to handle the reply headers. That said, I went ahead and added cache headers to my cron job, butit appears that Cloudflare is ignoring cache headers in the request.

    – The plugin set the proper cache control header on the Response and not Request. Here are a few snapshot from the plugin code:

    View post on imgur.com

    There is also a .htaccess rule and nginx rule based on what server you are using to set cache control header for the /wp-cron.php endpoint:

    View post on imgur.com

    View post on imgur.com

    Please check if you have added the required rules in your htaccess or nginx.conf file.

    Thread Starter PurpleNurple

    (@purplenurple)

    Thank you for working with me on this issue. I do very much appreciate your time.

    I’m not having issues with wp-cron.php. As I mentioned before I’m running it via PHP not via URL, so it’s not affected by caching. Also, since I’m running LiteSpeed (see the reply headers in my last comment), which is compatible with Apache, the plugin already adds the proper .htaccess rules. Cron is not the issue.

    The plugin is adding proper cache headers in most cases, but it is not adding them for the preloader and purge-all pages. If you look at the headers in my previous message, you’ll see that this is true.

    I spent about an hour digging into the plugin’s code, and I found the bug. The plugin is using the nocache_headers filter hook to add headers, but for these two pages, die() is being called before this hook executes. One way to fix this is to add the following code above die() within the cronjob_preloader() and cronjob_purge_cache() functions:

    if ( ! headers_sent() ) {
        nocache_headers();
    }
    Plugin Contributor iSaumya

    (@isaumya)

    Hi,
    I have added the fix that you have mentioned. It will be added in the upcoming release of the plugin. In the mean time you can try replacing the plugin code with the content of this ZIP file and check if this resolves your issue.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Preloader via cron job issue’ is closed to new replies.