Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 547
  • Last Modified:

PHP Script Dying

HI all, I am experiencing some very strange behavior in PHP and could use some help. We are running PHP5.3 as a module for Apache22 .

I have a script that creates a PDF report which takes a while to load. On smaller reports (limited records) the PDF script completes as designed with no errors. However, with larger ones, it just dies with no error or log.

I have set at the top of the script:
ini_set("memory_limit", "500M");      
ini_set("max_input_time", "7200");      
ini_set("max_execution_time", "7200");      

Running subsequent ini_get verifies that the above settings have been set by the api call and we are not running php in safe mode.

For debugging purposes, my php.ini is set to:
error_reporting = E_ALL
log_errors = On

The script dies with a blank white page and no log output is created. Is there anything else I can do to try and determine why the script is dying?
0
skymander
Asked:
skymander
  • 7
  • 3
  • 3
  • +1
1 Solution
 
VampireofdarknessCommented:
Do you have a log file specified (error_log=?)? If not, set

display_errors=On

in your php.ini. Also,

error_reporting  =  E_ALL | E_STRICT

is more detailed than just E_ALL.

You could always try an echo '1' / echo '2' / echo '3' at steps throughout the PDF generation to see at which point it fails, you may need to flush() output though: http://php.net/manual/en/function.flush.php
0
 
Beverley PortlockCommented:
Have you looked in the system error log? Sometimes when PHP dies the message winds up only in the log and not on the screen no matter what you enable.
0
 
skymanderAuthor Commented:
I tried error_reporting  =  E_ALL | E_STRICT to no avail. I have also printed each loop, it gets (randomly) to about the 1000th iteration then just stops printing anything.

And yes, my log file is set to a custom log but it also works to have it use the system log which directs it to the apache error log.

I have checked the system logs /var/log/messages, the apache error log, and the apache access log and have not found anything.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
VampireofdarknessCommented:
Are you able to see what kind of memory usage httpd gets during execution? (Task Manager) Can you provide an example of code? Does each iteration involve one or more SQL query / are they cached / what is the data size returned? Are you clearing variables each iteration to free memory?
0
 
skymanderAuthor Commented:
I will try and track the memory usage of httpd. Yes the code performs queries and calculations on each iteration including multiple SQL queries. It works when building a report with 1000 records at a time, but if I run all 3200 it fails. Variables are for the most part cleared on each iteration. I can continue go try and reduce memory usage, but the frustrating thing is there is no logs anywhere which makes all optimizations pretty much a total shot in the dark.
0
 
Amar BardoliwalaCommented:
Hello skymander,

try using max_execution_time = 0;

Also it seems that you might need to track your code somehow..

Also confirm your php setting for error using "phpinfo()"

Hope this will help.

Thanks
0
 
skymanderAuthor Commented:
Amar,

Yes I have tried ini_set("max_execution_time", 0) and still the script times out.

What do you mean about tracking my code?

phpinfo shows that
display_errors = on
the error log is set
and my logging levels are the same as above
0
 
Amar BardoliwalaCommented:
Hello skymander,

What do you mean by script time out?

are you getting any message about timeout like "Max execution time exceeded"?

Regarding tracking you can fire emails from your code starting from top of your page to bottom. Put it at main points where you think problem might be.

Execute script and see how many of them are you getting and from which email it stopped.

than you can review that code.

also you can paste your code here. So someone here can help you regarding the matter.

Thanks.

0
 
skymanderAuthor Commented:
Amar,

Thanks for the reply. As stated in my original post, the timeout is a blank white page with no output. And nothing is put in the error log (either system or custom).

Because this script works great with a smaller set of data, I am not suspecting so much a bad piece of code, but some sort of resource problem like memory or a timeout. But I have tried to set values for both of those manually (see original post) so I'm either missing something, or something is not taking. The timeout is after about 2 minutes.

I have traced the loops via a print-flush statement and it will get through roughly 1000 of 3200 entries (varies each time), then just stop printing.

Sky
0
 
VampireofdarknessCommented:
As before: What size output are you getting on each iteration? Are you properly clearing variables each iteration? Can you provide a code example?
0
 
skymanderAuthor Commented:
Top reveals that httpd consumes 215MB ram at peak and 17% processor which both seem pretty reasonable given the processing.

Here is my code, but as I said before, it works with smaller data-sets. The problem here is that PHP is dying and I don't have any way of determining why.

require_once("global_includes.php");

ini_set("memory_limit", "800M");      
ini_set("max_input_time", "7200");      
ini_set("max_execution_time", "7200");      

// $dbh and $USER are set in global include
$USERNAME = $USER->name;

$pdf =& new Cezpdf();
$pdf->selectFont('../lib/pdf/fonts/Helvetica.afm');
$pdf->ezSetMargins(0,0,0,0);

// Set paper coordinates
$com_cols = 3;
$x_pos = array(24,224,422);
$y_interval = 72;
$original_y = 735;
$curCol = 0;

// Pull list of customers
$olist = new CCustomerList($dbh, "customer", $USER);
$olist->processFormConditions($_POST); // Used to filter fields like date_entered>'1/1/2009'
$olist->getObjects();
$num = $olist->getNumObjects();
for ($i = 0; $i < $num; $i++)
{
      $line_buf = "";
      $obj = $olist->getObject($i); // object data is not loaded until this function is called. The list just holds an array of ids
      $name = $obj->getValue("salutation");
      if (!$name)
            $name = CustGetName($dbh, $obj->id);
      
      $street = $obj->getValue('street');
      $street2 = $obj->getValue('street2');
      $city = $obj->getValue('city');
      $state = $obj->getValue('state');
      $zip = $obj->getValue('zip');
      
      if (!$street || !$zip)
      {
            $street = $obj->getValue('business_street');
            $street2 = $obj->getValue('business_street2');
            $city = $obj->getValue('business_city');
            $state = $obj->getValue('business_state');
            $zip = $obj->getValue('business_zip');
      }
      
      if ($curY < 36)
      {
            $pdf->ezNewPage();
            $curY = $original_y;
            $curCol = 0;
      }
      
      $pdf->addText($x_pos[$curCol], $curY, 10, $name);
      $pdf->addText($x_pos[$curCol], $curY - 12, 10, $street);
      if ($street2)
      {
            $pdf->addText($x_pos[$curCol], $curY - 24, 10, $street2);
            $pdf->addText($x_pos[$curCol], $curY - 36, 10, "$city, $state $zip");
      }
      else
            $pdf->addText($x_pos[$curCol], $curY - 24, 10, "$city, $state $zip");
            
      if ($curCol >= $num_cols)
      {
            $curY -= $y_interval;
            $curCol = 0;
      }
      else
            $curCol++;

      $olist->unsetObject($i); // cleans up all data for this selected object
}

// Print results to browser
$pdf->ezStream();
0
 
Amar BardoliwalaCommented:
Hello skymander,

one more thing you can try is following.

Instead of creating pdf, create a HTML table and see if that table displays correctly. with your data of generating PDF report.

if something is wrong you might be able to get hint about it in that page.

Thanks.
0
 
skymanderAuthor Commented:
Well I'm not sure what the problem was, but we resolved it by putting the PDF generation in a background process though gearman, storing the results in a temporary file, and then redirecting the end-user once the job completed.

Thank you all for your help.
0
 
skymanderAuthor Commented:
We were unable to get anything to log the script dying - still a mystery. Instead we created a work-around using background processes.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

  • 7
  • 3
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now