Aaron Ahearne Posted January 29, 2016 Share Posted January 29, 2016 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 More sharing options...
adrian Posted January 29, 2016 Share Posted January 29, 2016 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 More sharing options...
BitPoet Posted January 29, 2016 Share Posted January 29, 2016 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. 2 Link to comment Share on other sites More sharing options...
Aaron Ahearne Posted January 29, 2016 Author Share Posted January 29, 2016 $pageModel->save() has the same effect pretty much. Heres an example of that selector for you: has_parent=Series,has_parent!=Trash,body_style_id=F20,include=all Link to comment Share on other sites More sharing options...
adrian Posted January 29, 2016 Share Posted January 29, 2016 I bet @BitPoet is spot on with his diagnosis! Link to comment Share on other sites More sharing options...
Aaron Ahearne Posted January 29, 2016 Author Share Posted January 29, 2016 Haha that is correct, is meant to be pageCar, Ill update the code there. That was just a mistake when I was trying to debug. The problem still persists! Link to comment Share on other sites More sharing options...
BitPoet Posted January 29, 2016 Share Posted January 29, 2016 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 More sharing options...
Aaron Ahearne Posted February 1, 2016 Author Share Posted February 1, 2016 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 More sharing options...
Aaron Ahearne Posted February 8, 2016 Author Share Posted February 8, 2016 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 More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now