We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 8784
    • 32 Posts
    Quote from: BobRay at Nov 21, 2011, 04:02 AM
    How are you triggering update.php?


    update.php is triggered when I modify a page resource in the manager's content editor and I click "save".
      • 3749
      • 24,544 Posts
      Yes, but what are you doing to make update.php execute? Saving a doc in the Manager, or what?
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 8784
        • 32 Posts
        Quote from: BobRay at Nov 21, 2011, 04:12 AM
        Yes, but what are you doing to make update.php execute? Saving a doc in the Manager, or what?
        Yes.

        If I add…
        $modx->invokeEvent('OnSiteRefresh');

        …back into update.php (beneath my 'The refresh happened.' log entry), the plug-in fires:
        [2011-11-20 23:15:09] (DEBUG @ /modxRevo/connectors/resource/index.php) The plugin fired.
        • discuss.answer
          • 3749
          • 24,544 Posts
          It looks like you're right. In 2.1.5, the only place I can see that event being invoked is in the clearcache.php processor, which as far as I can tell, is never called. So I'd consider it a possible bug.

          Some of the processors are for future use and you may have gotten caught in the transition (i.e., the event invocation got moved, but the processor didn't get implemented.

          I could be completely wrong about this, but I don't see any way to refresh the cache in a plugin since all invoked events occur before the cache is cleared.

          For now, you may have to put the code in a snippet and just run the snippet when you're done working. Disabling the empty_cache permission might also be an option if you create a plugin that manually clears the cache for just that one resource.

          OpenGeek or splittingred may have a better idea.
          [ed. note: BobRay last edited this post 12 years, 5 months ago.]
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
          • discuss.answer
            • 8784
            • 32 Posts
            Quote from: BobRay at Nov 21, 2011, 05:09 AM
            It looks like you're right. In 2.1.5, the only place I can see that event being invoked is in the clearcache.php processor, which as far as I can tell, is never called. So I'd consider it a possible bug.

            Thanks for confirming this. I think I'll file it.

            Quote from: BobRay at Nov 21, 2011, 05:09 AM

            I could be completely wrong about this, but I don't see any way to refresh the cache in a plugin since all invoked events occur before the cache is cleared.

            Hey Bob thanks for looking at this.

            Just to be clear, I'm fine with the default modx cache clearing behavior--whenever a resource is "saved". But what I want to generate specific cache files automatically for resources I deem "high traffic" immediately after the cache is cleared (hoped to tie this to the "siteRefresh" system event).

            Quote from: BobRay at Nov 21, 2011, 05:09 AM

            For now, you may have to put the code in a snippet and just run the snippet when you're done working.

            I'm still confused why "generateResource" works in a snippet but not in a plug-in? Do plug-ins miss out on some context that snippets have? (Might this also be a bug?)

            How can I invoke a snippet when the cache is cleared in update.php? Are you thinking I should just put "runsnippet" after the cache clearing happens in update.php? I'm happy to do this but was hoping to avoid hacking core files.
            $modx->runSnippet("generate high traffic cache files")...

              • 3749
              • 24,544 Posts
              I meant to just put the snippet tag in a resource and "view" it. Not an elegant or convenient solution, but it avoids hacking the core.
                Did I help you? Buy me a beer
                Get my Book: MODX:The Official Guide
                MODX info for everyone: http://bobsguides.com/modx.html
                My MODX Extras
                Bob's Guides is now hosted at A2 MODX Hosting
              • Looking at the new processors, it looks like we need to have a "OnCleanup" event that fires during the cleanup() method, just before it returns the success message. This will allow for proper manipulation of anything that needs to happen after the cache is cleared.

                I might also assert that the calls to the refresh() method of modCacheManager should be refactored in the processors to use the clearcache processor, which would also provide access to the OnSiteRefresh event appropriately.
                  • 8784
                  • 32 Posts
                  Quote from: opengeek at Nov 21, 2011, 07:05 PM
                  Looking at the new processors, it looks like we need to have a "OnCleanup" event that fires during the cleanup() method, just before it returns the success message. This will allow for proper manipulation of anything that needs to happen after the cache is cleared.

                  Thanks for the reply OG.

                  Quick question: Does "new processors" mean 2.2?
                    • 8784
                    • 32 Posts
                    Thought I would update this thread in case anyone was interested.

                    Issue:
                    "generateReource()" not writing cache files when executed via plug-in (but works fine in a snippet).

                    Resolution:
                    Add the line: "$resource->_contextKey = $resource->get('context_key');"
                    Insert it before: "$modx->cacheManager->generateResource($resource);"

                    Issue:
                    When writing multiple cache files, "generateResource()" writes mismatched content into the "_content" portion of the cache file.

                    If a snippet (ID1) executes "generateResource(ID3)" and "generateResource(ID5)", the "_content" in both "3.cache.php" and "5.cache.php" is for ID1.

                    If a plug-in executes "generateResource()" for ID3 and ID5, the "_content" in "5.cache.php" will contain the content for ID3.

                    Sample code:
                    <?php
                    $modx->setLogLevel(modX::LOG_LEVEL_DEBUG);
                    $modx->log(modX::LOG_LEVEL_DEBUG, 'Write some cache files'); 
                    
                    // Get all published page resources:
                    $matches = $modx->getCollection('modResource',array(
                        'published' => 1
                    ));
                    
                    // Generate cache files for each resource
                    $modx->getCacheManager();
                    foreach($matches as $match) {
                    	$match->process();
                    	//Next line is for plugin:
                    	$match->_contextKey = $match->get('context_key');
                    	$modx->cacheManager->generateResource($match);
                    	$modx->log(modX::LOG_LEVEL_DEBUG, '>>>Cache created for: '.$match->get('id')); 
                    }