Jump to content

Multiple new pages via API fails - Timeout/500 error?


Erik Richter
 Share

Recommended Posts

Hey everyone,

creating multiple pages with a foreach loop from a template file fails, breaks everything and results in not loading the page and sometimes a 500 error.

Creating a single page works perfectly fine. Can someone point me to the right direction? Much appreciated ?

 

...

 foreach ($period as $slot)
{
    
$p = new Page();
$p->setOutputFormatting(false);
$p->template = 'test';
$p->parent =  $pages->get(1);
$p->save();
}

..

 

Link to comment
Share on other sites

 

55 minutes ago, Erik Richter said:

creating multiple pages with a foreach loop from a template file fails, breaks everything and results in not loading the page and sometimes a 500 error.

Server's probably running out of memory. Maybe you are adding and manipulating images? Maybe show us the code?

Otherwise, you can create in batches or using CLI. $pages->uncacheAll() in the loop also helps. Btw, if you have a single parent, better to fetch it outside the loop (although ProcessWire would probably save it in cache so as not to call it again and again).

Link to comment
Share on other sites

thanks for you quick reply!

I am actually using this simple code, just for testing purposes. Also changed the array to something like "array("1","2","3","4","5");" - not working either and resulting in a 504 Gateway Error.

Like I said - adding one page via API is completely fine. I already checked PHP memory limit, but everything seems okay there.. Hopefully you have some more ideas where this error is coming from - thanks in advance!

...

 foreach ($period as $slot)
{
    
$p = new Page();
$p->setOutputFormatting(false);
$p->template = 'test';
$p->parent =  $pages->get(1);
$p->save();
}

..

Also sometimes actions on more than one page (in a short period of time) the pagetree (trash) result in a "network error". Maybe there is a connection? 

Link to comment
Share on other sites

What version of ProcessWire are you on? I suggest you install the module TracyDebugger to try and catch any errors. Is this site online? If not, also set $config->debug = true. What is $period? Just wondering whether there is something causing an endless loop. I see also that you are not setting a title (and there no name as well) in your code. Is that intentional? I don't know if that could be the issue.

1 hour ago, Erik Richter said:

Also sometimes actions on more than one page (in a short period of time) the pagetree (trash) result in a "network error".

This bug/issue has been reported before and I have personally experienced it as well. I have never found a solution. I have never been able to reproduce it. It just comes and goes.

Link to comment
Share on other sites

2 hours ago, kongondo said:

This bug/issue has been reported before and I have personally experienced it as well. I have never found a solution. I have never been able to reproduce it. It just comes and goes.

Crazy, since today I have it all the time, and nothing has really changed. The never ending spinning circle and then the Bad gateway/unknown error.

Regarding the main issue: $period is just a placeholder, I also tried it with other variables. The template file only has this little piece of code:

<?php  

$pgs = array("1","2","3","4","5");

foreach ($pgs as $pi) {
    
$p = new Page();
$p->setOutputFormatting(false);
$p->template = 'test';
$p->parent =  $pages->get(1);
$p->save();
    $p->title  = $pi;
    $pages->uncacheAll();
    }
    
?>

I also tried it with title and name, that should not be the problem tho, since PW gives titles automatically if none are provided.
Also creating only one page like this works perfectly.

I work with the latest stable version of PW, not too many modules, since I just started to build the site. Also debugging is on, and Tracy doesn't provide interesting insights.

Also: after the page breaks I can't access admin for some seconds to minutes. If I open a new private window tho, it works?

Also: it seems like sometimes at least 2 or 3 of those pages are being created (see them in admin), even tho the page with the template gives the error and doesnt load when accessed. 

This is really crazy and I have no idea whereelse I could look

Link to comment
Share on other sites

3 hours ago, kongondo said:

I see also that you are not setting a title (and there no name as well) in your code. Is that intentional? I don't know if that could be the issue.

Shame on me!

Looks like you HAVE to set a title when creating via api? at least inside a foreach loop. When I set a title, everything works - thank you again!

Now I need to solve this ajax admin endless spinner thing, when trashing pages..

Link to comment
Share on other sites

26 minutes ago, Erik Richter said:

Looks like you HAVE to set a title when creating via api? at least inside a foreach loop.

Actually you don't ? . You will just get pages with 'untitled-some-hash' as names and without titles.

26 minutes ago, Erik Richter said:

When I set a title, everything works - thank you again!

But glad it's working ? 

Edited by kongondo
typo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...