douxie2029 2018-06-01 08:37
浏览 281
已采纳

Php文件有时会导致500内部服务器错误

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
ew\branchTaskSummary.php on line 61

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

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

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

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

Notice: Undefined index: total in C:\WS\JMIS\Library
ew\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.

  • 写回答

6条回答 默认 最新

  • dongzhuo5185 2018-06-29 16:10
    关注

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(5条)

报告相同问题?

悬赏问题

  • ¥15 有赏,i卡绘世画不出
  • ¥15 如何用stata画出文献中常见的安慰剂检验图
  • ¥15 c语言链表结构体数据插入
  • ¥40 使用MATLAB解答线性代数问题
  • ¥15 COCOS的问题COCOS的问题
  • ¥15 FPGA-SRIO初始化失败
  • ¥15 MapReduce实现倒排索引失败
  • ¥15 ZABBIX6.0L连接数据库报错,如何解决?(操作系统-centos)
  • ¥15 找一位技术过硬的游戏pj程序员
  • ¥15 matlab生成电测深三层曲线模型代码