Php file sometimes causing 500 Internal Server Error

ThePerplexedOne

So, in our company we have branches. Each branch has been given a set of tasks they must complete on a daily/weekly/monthly basis.

I've written a script that summarizes which tasks have been completed for every single branch for the current month. Now, this can take a long time depending on how much data there is. (Usually increases as the month goes on)

At the start of the month, everything works fine. Towards the middle/end of the month is when the problems start happening (usually because there's more data to check, due to there being more days).

This is the code that calls the function to summarize everything (which is also the page that throws the 500 error):

<?php
ini_set('max_execution_time', 3600);
require_once('../../Connections/newConn.php');
require_once('../../Library/new/branchTaskSummary.php');
header('Content-Type: application/json');
$summary = array();
if(isset($_GET['month']) && isset($_GET['year']) && isset($_GET['update'])) {
    global $conn;
    $update = $_GET['update'] == 1;
    $month = $_GET['month'];
    $year = $_GET['year'];
    if(!$update) {
        $query = $conn->prepare("SELECT data_json, DATE_FORMAT(last_updated, '%d/%m/%Y %H:%i') AS last_updated FROM branchtasksummarydata WHERE month = ? AND year = ?");
        $query->bind_param('ii', $month, $year);
        $query->execute();
        $result = $query->get_result();
        if($result->num_rows > 0) {
            $summary = $result->fetch_assoc();
        } else {
            $summary = summariseEverything($month, $year);
        }
    } else {
        $summary = summariseEverything($month, $year);
    }
}
echo json_encode($summary);
?>

It calls a function called summarizeEverything which is where all the magic happens.

Here is the code for that function:

function summariseEverything($month, $year) {
    global $conn;
    $now = new DateTime();
    $data = array();
    $daysInMonth = cal_days_in_month(CAL_GREGORIAN, $month, $year);
    $start = sprintf("%s-%s-%s", $year, $month, "01");
    $end = sprintf("%s-%s-%s", $year, $month, $daysInMonth);
    //Get all branches
    $branches = getBranches();
    //Get all tasks
    $dailyTasks = getAllTasks(1, $start, $end);
    $weeklyTasks = getAllTasks(2, $start, $end);
    $monthlyTasks = getAllTasks(3, $start, $end);
    //Get dates
    $dailyDates = Calendar::getDaysBetween($start, $end);
    $weeklyDates = Calendar::getWeeksBetween($start, $end);
    $monthlyDates = Calendar::getMonthsBetween($start, $end);
    foreach($branches as $branch) {
        $instance = array("id" => $branch["id"], "name" => $branch["name"], "area" => $branch["area"], "finance_area" => $branch["finance_area"], "company" => $branch["company"], "icb" => $branch["icb"], "tasks" => array());
        $instance["tasks"]["completed"] = 0;
        $instance["tasks"]["daily"]["completed"] = 0;
        $instance["tasks"]["weekly"]["completed"] = 0;
        $instance["tasks"]["monthly"]["completed"] = 0;
        //Do daily tasks.
        foreach($dailyTasks as $task) {
            foreach($dailyDates as $date) {
                $dateObj = new DateTime($date);
                if($dateObj > $now) {
                    break;
                }
                $isWorkingDay = true;
                Calendar::isBankHoliday($dateObj->format('Y-m-d'), &$Colour, &$Msg, &$isWorkingDay);
                Calendar::isWeekend($dateObj->format('Y-m-d'), &$Colour, &$Msg, &$isWorkingDay);
                $dayName = $dateObj->format("l");
                $isOpen = branchIsOpen($branch, $dayName);
                if($isWorkingDay && $isOpen) {
                    $instance["tasks"]["total"] += 1;
                    $instance["tasks"]["daily"]["total"] += 1;
                    $instance["tasks"]["daily"][$task["department"]]["total"] += 1;
                    $instance["tasks"]["daily"][$task["department"]][$task["id"]]["total"] += 1;
                    $completed = taskCompleted($branch["id"], $task["id"], $date." 00:00:00", $date." 23:59:59");
                    if($completed) {
                        $instance["tasks"]["completed"] += 1;
                        $instance["tasks"]["daily"]["completed"] += 1;
                        $instance["tasks"]["daily"][$task["department"]]["completed"] += 1;
                        $instance["tasks"]["daily"][$task["department"]][$task["id"]]["completed"] += 1;
                    }
                }
            }
            if(!$instance["tasks"]["daily"][$task["department"]][$task["id"]]["completed"]) {
                $instance["tasks"]["daily"][$task["department"]][$task["id"]]["completed"] = 0;
            }
            if(!$instance["tasks"]["daily"][$task["department"]]["completed"]) {
                $instance["tasks"]["daily"][$task["department"]]["completed"] = 0;
            }
        }

        //Do weekly tasks.
        foreach($weeklyTasks as $task) {
            foreach($weeklyDates as $date) {
                $instance["tasks"]["total"] += 1;
                $instance["tasks"]["weekly"]["total"] += 1;
                $instance["tasks"]["weekly"][$task["department"]]["total"] += 1;
                $instance["tasks"]["weekly"][$task["department"]][$task["id"]]["total"] += 1;
                $completed = taskCompleted($branch["id"], $task["id"], $date["start"], $date["end"]);
                if($completed) {
                    $instance["tasks"]["completed"] += 1;
                    $instance["tasks"]["weekly"]["completed"] += 1;
                    $instance["tasks"]["weekly"][$task["department"]]["completed"] += 1;
                    $instance["tasks"]["weekly"][$task["department"]][$task["id"]]["completed"] += 1;
                }
            }
            if(!$instance["tasks"]["weekly"][$task["department"]][$task["id"]]["completed"]) {
                $instance["tasks"]["weekly"][$task["department"]][$task["id"]]["completed"] = 0;
            }
            if(!$instance["tasks"]["weekly"][$task["department"]]["completed"]) {
                $instance["tasks"]["weekly"][$task["department"]]["completed"] = 0;
            }
        }

        //Do monthly tasks.
        foreach($monthlyTasks as $task) {
            foreach($monthlyDates as $date) {
                $instance["tasks"]["total"] += 1;
                $instance["tasks"]["monthly"]["total"] += 1;
                $instance["tasks"]["monthly"][$task["department"]]["total"] += 1;
                $instance["tasks"]["monthly"][$task["department"]][$task["id"]]["total"] += 1;
                $completed = taskCompleted($branch["id"], $task["id"], $date["start"], $date["end"]);
                if($completed) {
                    $instance["tasks"]["completed"] += 1;
                    $instance["tasks"]["monthly"]["completed"] += 1;
                    $instance["tasks"]["monthly"][$task["department"]]["completed"] += 1;
                    $instance["tasks"]["monthly"][$task["department"]][$task["id"]]["completed"] += 1;
                }
            }
            if(!$instance["tasks"]["monthly"][$task["department"]][$task["id"]]["completed"]) {
                $instance["tasks"]["monthly"][$task["department"]][$task["id"]]["completed"] = 0;
            }
            if(!$instance["tasks"]["monthly"][$task["department"]]["completed"]) {
                $instance["tasks"]["monthly"][$task["department"]]["completed"] = 0;
            }
        }
        $data["branches"][] = $instance;
    }
    $json = json_encode($data);
    //Check if data exists for this month & year before inserting into DB.
    $query = $conn->prepare("SELECT summaryPK FROM branchtasksummarydata WHERE month = ? AND year = ?");
    $query->bind_param('ii', $month, $year);
    $query->execute();
    $result = $query->get_result();
    $exists = $result->num_rows > 0;
    if($exists) {
        //Data already exists, so let's update it.
        $query = $conn->prepare("UPDATE branchtasksummarydata SET data_json = ?, last_updated = ? WHERE month = ? AND year = ?");
        $query->bind_param('ssii', $json, $now->format("Y-m-d H:i:s"), $month, $year);
        $query->execute();
    } else {
        //No data exists, so we will insert it.
        $query = $conn->prepare("INSERT INTO branchtasksummarydata (year, month, last_updated, data_json) VALUES(?, ?, ?, ?)");
        $query->bind_param('iiss', $year, $month, $now->format("Y-m-d H:i:s"), $json);
        $query->execute();
    }
    return array("data_json" => $json, "last_updated" => $now->format("d/m/Y H:i"));
}

Now when I call this function, it will hang for around 6/7 minutes before giving me an internal server error.

The funny thing is, I test it on my local machine and it works fine. However, on the live server, it throws the error.

EDIT

After coming back to try and fix this issue, I turned on any error logging I could and it just gives me these with each iteration of looping through branches:

Notice: Undefined index: total in C:\WS\JMIS\Library\new\branchTaskSummary.php on line 61

Notice: Undefined index: total in C:\WS\JMIS\Library\new\branchTaskSummary.php on line 62

Notice: Undefined offset: 9 in C:\WS\JMIS\Library\new\branchTaskSummary.php on line 63

Notice: Undefined index: total in C:\WS\JMIS\Library\new\branchTaskSummary.php on line 63

Notice: Undefined offset: 12 in C:\WS\JMIS\Library\new\branchTaskSummary.php on line 64

Notice: Undefined index: total in C:\WS\JMIS\Library\new\branchTaskSummary.php on line 64

Without the logging, it throws a 500 internal server error. I cannot for the life of me figure out what's going on! The code works, I've tested it on multiple local systems. Yet doesn't seem to work on the live server which I do not have access to.

ThePerplexedOne

I solved the issue. It was an IIS FastCGI activity timeout configuration.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Load many images from php file sometimes throw 500 internal server error

Jsp file throw internal server 500 error sometimes

update-php class causing 500 internal server error

imagecreatefromstring is causing 500 internal server error

PDOStatement::fetch causing 500 Internal server error

Cronjob causing 500 internal server error

IIS Giving a 500 Internal Server Error When Serving a PHP file

Why is my Apache2 .htaccess file CORS header causing a 500 internal server error?

Web Config Error Causing 500 Internal Server Error

Mod rewrite causing internal server error, error 500

PHP use keyword is causing 500 server error

Unexplained PHP error (Internal Server Error 500)

500 Internal Server Error (trying to upload file)

PHP code giving 500 internal server error

POST 500 Internal Server Error -- PHP

Internal Server Error 500 on .PHP pages only

500 internal server error php, how to solve?

Slim PHP 500 Internal Server Error

Internal Server Error 500 with Rewrite Engine On on Shared Server with Apache 2.2, php 5.4 using .htaccess file

Internal Server Error (500)

PHP Telegram Bot: calling functions from another php file results in 500 Internal Server Error

Wp_query doesn't work inside a php file for ajax call (return 500 Internal Server Error)

suphp internal server error 500. file "../index.php" is writeable by others suPHP 0.7.2

Phpmailer causing internal error 500 with ajax

PHP SQL Server : 500 Internal server error with specific query

php MySQL insert into error, 500 internal server error

Valid foreach loop causing 500 Internal Server Error in valid Ajax request

How do I find out what is causing Azure B2C 500 Internal Server Error?

Unable update file store in appdata scope - 500 Internal Server Error