2

For the past year, I've been using google app engine to make PDF's and then output them onto the page. They have always been working flawlessly until today.

Now, I get the following error - not all the time, but probably around 40% of my requests come back with a 500 error about it being terminated.

Does anyone know why this would be happening and how to avoid it?

While handling this request, the process that handled this request was found to be using too much memory and was terminated. This is likely to cause a new process to be used for the next request to your application. If you see this message frequently, you may have a memory leak in your application.


This is my script. Sometimes I add images to it using $pdf->MemImage(); or I add text with $pdf->Cell() but that is about the only difference.

<?php
    define('FPDF_FONTPATH','lib/fpdi/fonts');
    require_once('lib/fpdf.php');
    require_once('lib/fpdi/fpdi.php');

    $pdfPath = file_get_contents("gs://bucket/template.pdf");
    $temp_id = "12345";
    $max_pages = 5;

    //put the pdf image into a temp directory in google drive
    $object_url = "gs://bucket2/template.pdf";
    file_put_contents($object_url, $pdfPath);

    //Start the FPDI
    $pdf = new FPDI('P', 'pt');
    $pdf->SetAutoPageBreak(false);

    //Set the source PDF file
    $source_file = $pdf->setSourceFile("gs://bucket2/template.pdf");

    for($page_count = 1; $page_count <= $max_pages; $page_count++)
    {
        //Import the first page of the file
        $tppl = $pdf->importPage($page_count);
        $pdf->AddPage();

        //get size of pdf page
        $size = $pdf->getTemplateSize($tppl);

        $pdf->useTemplate($tppl, null, null, $size['w'], $size['h'], true);
    }

    header('Content-Type: application/pdf');
    $pdf->Output("template.pdf", "I");
    unlink("gs://bucket2/template.pdf");
?>
bryan
  • 8,879
  • 18
  • 83
  • 166
  • What does the memory chart looks like for your app? – Andrei Volgin Feb 26 '16 at 20:22
  • @AndreiVolgin here is the [memory usage](http://i.imgur.com/bf0J8EG.png) chart under App Engine dashboard if that's what you are asking for – bryan Feb 26 '16 at 20:24
  • This is for 1 hour. Is this an hour when you had a process terminated? – Andrei Volgin Feb 26 '16 at 20:29
  • @AndreiVolgin Yea, there really aren't any users using my application. It's only me doing the actions and multiple processes were terminated in the past hour. Here is a [6hour chart](http://i.imgur.com/PV6lYqB.png) too – bryan Feb 26 '16 at 20:35
  • Can it be just the size of the file that you create? – Andrei Volgin Feb 26 '16 at 20:37
  • @AndreiVolgin I don't think so. The PDF that keeps getting terminated is only 134kb – bryan Feb 26 '16 at 20:41
  • Do you have threadsafe on or off? If it's on, try turning it off! – Tom Feb 28 '16 at 18:52
  • @tom I removed the thread safe option in app.yaml and it doesn't appear to make a difference. Processes are still – bryan Feb 29 '16 at 03:37
  • Ok, have you tried changing the instance size? To F2 for example? – Tom Feb 29 '16 at 06:17
  • 134kb may be the final size of the PDF, but that doesn't say much about the memory required for the creation of the pdf. PDFs are essentially compressed postscript and the postscript part can be huge before compression. Is it possible that someone replaced the template.pdf with a bigger/more complex document? – konqi Feb 29 '16 at 14:27
  • @Tom there is no instance size for PHP App Engine – bryan Feb 29 '16 at 16:32
  • @Konqi thanks for the extra info. no it's not possible. I am doing tests on a <200kb template.pdf varying from 2-4 pages. I insert anywhere from 1-5 images on there as well but they are also all under 200kb. – bryan Feb 29 '16 at 16:33
  • You can adjust the instance size. See here https://cloud.google.com/appengine/docs/php/modules/#PHP_Instance_scaling_and_class – Tom Feb 29 '16 at 19:12
  • Wow. Pretty obscure! Might be worth adding some peak memory usage logging to those requests. Do they show as 200 responses in the logging? – Tom Mar 02 '16 at 19:43
  • How did you get on with this? As I found a performance issue using FPDI and importing pages which I'm currently in the process of making a pull request for – Chris Townsend Mar 30 '17 at 15:05
  • Hey @ChrisTownsend check my answer I just posted and see if that works for you. – bryan Mar 30 '17 at 15:15
  • Thanks for adding this Bryan, I already altered the instance type which had some impact but didn't do much dramtically. I noticed that when using Google App Engine there is a huge performance issue when importing pages due to having to save the template file on a storage bucket. The FPDI library makes multiple get requests when running freads. Just added a pull request to the library which solves the problem by adding a temp file option to setSourceFile. Glad you found an answer to your issue though :) – Chris Townsend Mar 30 '17 at 15:17
  • @ChrisTownsend do you mind linking me to the pull request? I'd love to implement this to improve what I have! – bryan Mar 30 '17 at 15:20
  • Yeah of course. The contributor had a better solution, but it still applys in the same way to GAE. https://github.com/Setasign/FPDI/pull/16 – Chris Townsend Mar 30 '17 at 15:33

1 Answers1

1

I solved this by updating the instance class and basic scaling in app.yaml. This is just experimentation and might need to be different based on use case but it did solve the problem for me. Thanks to everyone who helped in the above comments.

application: <APPLICATION NAME>
version: 1
runtime: php55
api_version: 1
instance_class: B2
basic_scaling:
  max_instances: 5
bryan
  • 8,879
  • 18
  • 83
  • 166