Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot purge; no valid URLs #66

Closed
ptrykulich opened this issue Nov 29, 2023 · 15 comments
Closed

Cannot purge; no valid URLs #66

ptrykulich opened this issue Nov 29, 2023 · 15 comments
Labels

Comments

@ptrykulich
Copy link

If I change an entry through the edit page, cache will purged correctly, but if programmatically update entries and do it through a queue, I will get the error - Cannot purge; no valid URLs.

Version
Cloudflare 2.0.1
PHP version 8.1.16
MySQL 5.7.42
Craft Pro 4.5.9
Yii version 2.0.48.1

By programmatically, I mean that I do it using the PHP code in the module, something like this:

$entry = new Entry();
$entry->sectionId = 1;
$entry->typeId = 1;
$entry->authorId = 1;
$entry->title = 'Some Title';
Craft::$app->elements->saveElement($entry)
@ptrykulich ptrykulich added the bug label Nov 29, 2023
@bencroker
Copy link
Contributor

Have you added the Entry element class to the purgeElements config setting?

//purgeElements => [
// 'craft\elements\Asset',
//],

@ptrykulich
Copy link
Author

Yes, I've added it:
image

@bencroker
Copy link
Contributor

In that case it should work. Can you verify that Craft::$app->elements->saveElement($entry) returns true?

@ptrykulich
Copy link
Author

When I save one entry it returns true, but the thing is that I programmatically save about 4000 entries through a queue and just after that I get about 100 errors in the logs like this.

Is there some way to use something like you implemented in Blitz:

// Before processing the feed
Blitz::$plugin->refreshCache->batchMode = true;

// Process the feed

// After processing the feed
Blitz::$plugin->refreshCache->refresh();

@bencroker
Copy link
Contributor

No, there is not – this plugin is nowhere near as sophisticated as Blitz.

A possible workaround would be to disable purging of entries by default, and manually call putyourlightson\cloudflare\services\Api::purgeUrls(), passing in the URLs of the entries saved.

@ptrykulich
Copy link
Author

Yes, but this will disable this option for other sections. Is it possible to add specific sections instead of the element type?

@bencroker
Copy link
Contributor

bencroker commented Dec 1, 2023

It’s not, but you can disable the entry element type just for the duration of your custom code.

Cloudflare::$plugin->settings->purgeElements = [];

// Save elements and store URLs

Cloudflare::$plugin->api->purgeUrls($urls);

@ptrykulich
Copy link
Author

Thanks! I'll try that and get back to you.

@ptrykulich
Copy link
Author

Sorry, can you please explain? Why do we call a method to purge urls if they don't exist? I can't disable updating of entries.

public function purgeCachesForUrl(string $url, bool $immediately = false): void
    {
        // max limit for Cloudflare API
        $cloudflareRuleCountLimit = 30;
        $relatedRules = $this->getRulesForUrl($url);
        $urlsToPurge = [];

        foreach ($relatedRules as $rule) {
            $clearList = Json::decode($rule->urlsToClear);

            foreach ($clearList as $clearUrl) {
                $urlsToPurge[] = UrlHelper::siteUrl($clearUrl);
            }
        }

        $numRules = count($urlsToPurge);

        if ($numRules > $cloudflareRuleCountLimit) {
            throw new NotSupportedException(
                sprintf(
                    'Too many rules; API requests are limited to %d and you provided %d',
                    $cloudflareRuleCountLimit,
                    $numRules
                )
            );
        }
        // here
        if ($immediately) {
            Cloudflare::$plugin->api->purgeUrls($urlsToPurge);
        } else {
            Queue::push(new PurgeCloudflareCache(['urls' => $urlsToPurge]));
        }
    }

@bencroker
Copy link
Contributor

What I was suggesting is that you first disable automatic purging of all element types:

Cloudflare::$plugin->settings->purgeElements = [];

Then, after you’ve programmatically updated entries, you manually purge their URLs:

Cloudflare::$plugin->api->purgeUrls($urls);

But now I also see what you meant about errors being logged. Are you concerned about the errors themselves or is something not working the way it should?

@ptrykulich
Copy link
Author

ptrykulich commented Dec 6, 2023

I've tried your advice and it's doesn't work for me.
I am concerned about the error - Cannot purge; no valid URLs. It seems to me that even with the cleaning of the entry disabled, I will receive it. Maybe I'm wrong.

I have disabled - Automatically Purge Elements:

image

But after resave entries, there are queue to purge urls. I see that this error occurs if the array of urls is empty.

public function purgeUrls(array $urls = []): mixed
    {
        if (!$this->getClient()) {
            return null;
        }

        $urls = UrlHelper::prepUrls($urls);

        // don’t do anything if URLs are missing
        if (count($urls) === 0) {
            return $this->_failureResponse(
                'Cannot purge; no valid URLs.'
            );
        }
        //...

In the end of _handleElementChange method. There is call of method:

$this->rules->purgeCachesForUrl(
            $element->getUrl()
        );

This method calls method purgeUrls:

 if ($immediately) {
      Cloudflare::$plugin->api->purgeUrls($urlsToPurge);
  } else {
      Queue::push(new PurgeCloudflareCache(['urls' => $urlsToPurge]));
  }

End if I have empty rules, or url which is not related to rule, this method will call with empty urls, and it will give the error.

@bencroker
Copy link
Contributor

I believe that error message is just being incorrectly logged, there’s nothing to worry about and I’ll add a fix for this.

But I’m confused as to whether otherwise everything is working for you or not? This issue is getting very long and I’m not sure where we are with it.

@ptrykulich
Copy link
Author

Yes, everything else works, thanks. I was just interested in this particular error.

@bencroker
Copy link
Contributor

Great! Fixed in 763e66f and released in 2.0.2.

@ptrykulich
Copy link
Author

Thank you so much!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants