Jump to content

Segmentation fault when batch creating new pages


Aaron Ahearne
 Share

Recommended Posts

Hi Guys,

Im running a script to create a bunch of new pages, but I get a segmentation fault when trying to create/update around 400 pages.

function importModels() {
  echo "Installing Models";

  $seriesJson = json_decode(file_get_contents("./metadata/models/models.json"));
  $templateModel = wire("templates")->get("name=model");
  $nodeModels = wire("pages")->get("name=Models, parent=/, include=all");

  if (!$seriesJson) {
    throw new Exception("/metadata/pages/models/models-2.json not found");
  }

  if (!$templateModel) {
    throw new Exception("Missing template");
  }

  if (!$nodeModels) {
    throw new Exception("Missing Models node");
  }

  foreach ($seriesJson->models as $modelData) {
    $modelName = $modelData->{KEY_BODY} . " " . $modelData->{KEY_MODEL_CODE};
    $pageCarSelector = "has_parent=Series," .
      "has_parent!=Trash," .
      "body_style_id={$modelData->{KEY_BODY}}," .
      "include=all";
    $pageCar = wire("pages")->get($pageCarSelector);
    if (is_a($pageCar, NullPage)) {
      throw new Exception("Car not found with body style ID: " . $modelData->{KEY_BODY});
    }

    $pageModel = wire("pages")->get("parent=Models, has_parent!=Trash, template=model, name={$modelName}");
    if (is_a($pageModel, NullPage)) {
      $pageModel = new Page();
      $pageModel->set("name", $modelName);
      $pageModel->set("template", $templateModel);
      $pageModel->set("parent", $nodeModels);
    }
    $pageModel->set("title", $modelData->{KEY_MODEL});
    $pageModel->set("mapped_car", $pageCar);
    $pageModel->set("car_identifier", $modelData->{KEY_MODEL_CODE});

    wire("pages")->save($pageModel);
    echo ".";
  }

  echo "\n";
}

The problem is not caused by the save, its like wire("pages")->get($pageCarSelector) is doing too much caching. If I add wire("pages")->uncacheAll(); just after the save then it runs a lot further, still eventually breaking.

Do you guys have any ideas of any other caches I need to clear to get this working?

Many thanks.

Link to comment
Share on other sites

Just something quick to try.

Instead of:

wire("pages")->save($pageModel);

what happens with:

$pageModel->save();

As for that selector - can you post what it looks like after all those variables and constants have been parsed so when can see what is actually been sent to the PW selector engine?

Link to comment
Share on other sites

This line looks fishy, likely to create an infinite recursion (though assigning a page to its own page field shouldn't be possible normally):

$pageModel->set("mapped_car", $pageModel);

The second $pageModel in that line should probably be $pageCar.

  • Like 2
Link to comment
Share on other sites

Aaron, a few questions to help us get a clearer picture:

  • are you running your script from command line or in the webserver?
  • if the latter, does php segfault or the webserver?
  • if the former, which error code does the segfault message show?
  • which versions of PHP, PW, MySQL (and, if involved, webserver) are you running?
Link to comment
Share on other sites

Im using

  • PHP 5.5.9
  • MySQL 5.5.46-0ubuntu0.14.04.2
  • PW 2.6.1
  • Apache 2.4.7

I've partially overcome this issue for now by deleting the parent and its children, then creating pages from scratch rather than searching and updating (so less caching to handle) and it works OK but this isn't a long term answer. 

I've noticed that even when separating out this process does not help. I have a similar process which runs before it but does not error. If I run them completely separately they will both work fine, but if they are called directly one after the other it will result in a segmentation fault regardless of the order they are in.

Link to comment
Share on other sites

So... the cleaner version of the code but Im still having issues. Ideally I would like to know if there is a way of disabling caching before I start the script.

$oldNodeModels = wire("pages")->get("name=models, parent.name=home");
if (!is_a($oldNodeModels, NullPage)) {
  wire("pages")->delete($oldNodeModels, true);
}

$nodeModels = new Page();
$nodeModels->set("template", "hidden-container");
$nodeModels->set("name", "models");
$nodeModels->set("title", "Models");
$nodeModels->set("parent", wire("pages")->get("name=home"));
$nodeModels->save();

$seriesJson = json_decode(file_get_contents("./metadata/models/models.json"));
$templateModel = wire("templates")->get("name=model");

if (!$seriesJson) {
  throw new Exception("/metadata/pages/models/models.json not found");
}

if (!$templateModel) {
  throw new Exception("Missing template");
}

if (!$nodeModels) {
  throw new Exception("Missing Models node");
}

echo "\nInstalling Models";

foreach ($seriesJson->models as $modelData) {
  $modelName = $modelData->{KEY_SERIES} . "-" . $modelData->{KEY_MODEL} . "-" . $modelData->{KEY_MODEL_CODE};
  $pageCarSelector = "has_parent=Series," .
    "has_parent!=Trash," .
    "body_style_id={$modelData->{KEY_BODY}}," .
    "include=all";
  $pageCar = wire("pages")->get($pageCarSelector);
  if (is_a($pageCar, NullPage)) {
    throw new Exception("Car not found with body style ID: " . $modelData->{KEY_BODY});
  }

  $pageModel = new Page();
  $pageModel->set("name", $modelName);
  $pageModel->set("template", $templateModel);
  $pageModel->set("parent", $nodeModels);
  $pageModel->set("title", $modelData->{KEY_MODEL});
  $pageModel->set("mapped_car", $pageCar);

  try {
    wire("pages")->save($pageModel);
  } catch (Exception $e) {
    echo $e;
  }
  wire("pages")->uncacheAll();
  echo ".";
}
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...