Problems, need help? Have a tip or advice? Post it here.
15 posts Page 2 of 2
Thank you very much for sharing the find :)

gc_collect_cycles() function is indeed very useful and I'll look deeper into it (didn't do that before because it required at least PHP 5.3 - the most recent commit of Couch, however, has bumped the min requirement to 5.3 so this comes into our ken now).

As for the changes not getting committed, could you please share exactly at what point you are trying the commit while aborting execution? I would have expected $DB->commit( 1 ), where '1' stands for 'force', to work.
Sure, there are several methods i've tried. Let's start with where I put the code to do commit:

Code: Select all
<cms:php>
    $GLOBALS['script_execution_time'] = microtime(true);
</cms:php>
<cms:each files as='file' >
    <cms:php>
          global $CTX; 
          $time_used = microtime(true) - $GLOBALS['script_execution_time'];
          $CTX->set('current_execution_time', $time_used);
          unset($time_used);
    </cms:php>
    <cms:if current_execution_time gt '20' >
       
        <---CODE GOES HERE--->

    </cms:if>
    <cms:if "<cms:page_exists "<cms:show key />" masterpage='scheduled/images/images.php' />" >
      <cms:if "<cms:get_custom_field "is_compressed" masterpage="scheduled/images/images.php" page="<cms:show key />" />" >
        <cms:set db_compressed_size="<cms:get_custom_field 'compressed_size' masterpage='scheduled/images/images.php' page="<cms:show key />" />" />
        <cms:if db_compressed_size ne file.size >
          <cms:db_persist
            _masterpage="scheduled/images/images.php"
            _page_id="<cms:pages masterpage="scheduled/images/images.php" page_name="<cms:show key />" ids_only='1' limit='1' ></cms:pages>"
            _mode='edit'
            original_size=file.size
            compressed_size=''
            is_compressed='0'
          />
        </cms:if>
        <cms:else />
          <cms:set db_original_size="<cms:get_custom_field 'original_size' masterpage='scheduled/images/images.php' page="<cms:show key />" />" />
          <cms:if db_original_size ne file.size >
            <cms:db_persist
              _masterpage="scheduled/images/images.php"
              _page_id="<cms:pages masterpage="scheduled/images/images.php" page_name="<cms:show key />" ids_only='1' limit='1' ></cms:pages>"
              _mode='edit'
              original_size=file.size
            />
          </cms:if>
      </cms:if>
      <cms:else />
        <cms:db_persist
          _masterpage='scheduled/images/images.php'
          _mode='create'
          _invalidate_cache='0'
          _autotitle='0'
          k_page_title="<cms:show file.filename />"
          k_page_name="<cms:show key />"
          path="<cms:show file.path />"
          original_size="<cms:show file.size />"
        />
    </cms:if>
    Memory used: <cms:php>echo memory_get_usage();</cms:php><br/>
    <cms:php>
      if (memory_get_usage() > 20000000) {
        gc_collect_cycles();
        echo "Memory cleanup";
      }
    </cms:php>
  </cms:each>


And here's what I've tried to commit changes to DB:

Code: Select all
<cms:break /> <--- No luck :(


Code: Select all
<cms:abort /> <--- No luck :(


Code: Select all
<cms:php> <--- No luck :(
  global $DB;
  $DB->commit( 1 ); // force commit, we are finished.
  $DB->disconnect();
  echo "Time limit reached.";
  exit;
</cms:php>
A working code is worth a thousand attempts, each attempt is worth a thousand words.
Below is a working sample of aborting and pages are correctly created and accessible.

Code: Select all
<cms:set masterpage = 'data.php' scope='global' /><cms:ignore>

    // Template ^^^^^^^^^^^^^^^^^^^ to create new cloned pages - must be clonable template!

</cms:ignore>
<cms:set newpages = '[]' is_json='1' scope='global'/>
<cms:repeat count='20'><cms:ignore>

        // Repeat 20 times -> create 1 new page. Expect a total of 20 new pages.

    </cms:ignore>
    <cms:db_persist _mode='create' _masterpage=masterpage _auto_title='1'>
        <cms:if k_success><cms:ignore>
           
                // Page created, save links to list
           
            </cms:ignore>
            <cms:capture into='newpages.' scope='global' >
                <a href="<cms:link id=k_last_insert_id />" target="_blank"><cms:show k_last_insert_page_name /></a>,
                <cms:pages masterpage=masterpage id=k_last_insert_id skip_custom_fields='1' show_future_entries='1'>
                <a href="<cms:admin_link />" target="_blank">admin link</a>
                </cms:pages>
            </cms:capture>
        </cms:if>
    </cms:db_persist><cms:ignore>
   
        // Now goes the code which halts after 10 created pages instead of 20 planned
   
    </cms:ignore>
    <cms:if k_count eq '10' ><cms:ignore>
       
            // Output links of 10.
       
        </cms:ignore>
        <cms:abort >
            <ul>
            <cms:each newpages as='page'>
                <li><cms:show page /></li>
            </cms:each>
            </ul>
        </cms:abort>
    </cms:if>
   
</cms:repeat><cms:ignore>

    // output links if abort was ignored

</cms:ignore>
<ul>
    <cms:each newpages as='page'>
    <li><cms:show page /></li>
    </cms:each>
</ul>


Try to adapt it to your situation..

I have used a modified cms:link, which outputs page link only by its id..
Mod can be copied from here viewtopic.php?f=8&t=11342 or cms:link in code above can be rewritten the regular way with masterpage and page parameters.
Thank you for your time. From my point of view your code is similar to mine. Could it be so that fetching pages right after creation sort of forces commit? Also, worth mentioning that i’m on Couch 2.0(version just before 2.1 release) and I really don’t want to upgrade at the moment
Here is a working sample of running code before time limit hit.

Code: Select all
<cms:set script_started_time = "<cms:php>echo microtime(true);</cms:php>" scope='global' />
<cms:set script__passed_time = '0' scope='global' />
     
<cms:while script__passed_time lt '0.01' ><cms:ignore>
       
        // Do something before planned time runs out.
        // '0.01' (seconds) is about 30 iterations. Hard limit in CouchCMS is 1000 iterations.

    </cms:ignore>
    <cms:capture into='script__passed_time' trim='1'>
        <cms:sub "<cms:php>echo microtime(true);</cms:php>" script_started_time />
    </cms:capture>
   
    <cms:show script__passed_time /><br/><cms:ignore>
   
        // below goes some code to run during each iteration
   
    </cms:ignore>
    <cms:php>sleep(0.001);</cms:php>
   
</cms:while>
15 posts Page 2 of 2