miasmos / op.gg-api

Serves op.gg web pages as json.
MIT License
81 stars 21 forks source link

Mysterious server crashes #5

Closed freakpants closed 7 years ago

freakpants commented 9 years ago

There is a mysterious error that will crash the server from time to time. I have not been able to reproduce it reliably. it doesnt seem to be linked to specific summoners. (But i will keep investigating)

parsing http://na.op.gg/summoner/ajax/update.json/?summonerId=35488173 undefined:1 undefined ^ SyntaxError: Unexpected token u at Object.parse (native) at Request.parseSummonerRefresh as _callback at self.callback (C:\op.gg-api-master\node_modules\request\request.js:237:22 ) at Request.emit (events.js:107:17) at ClientRequest.self.clientErrorHandler (C:\op.gg-api-master\node_modules\r equest\request.js:342:10) at ClientRequest.emit (events.js:107:17) at Socket.socketOnData (_http_client.js:322:9) at Socket.emit (events.js:107:17) at readableAddChunk (_stream_readable.js:163:16) at Socket.Readable.push (_stream_readable.js:126:10) at TCP.onread (net.js:538:20)

C:\op.gg-api-master>

freakpants commented 9 years ago

happened again - could it be that im calling the endpoint too often/too fast?

parsing http://euw.op.gg/summoner/userName=ImpExNaminitor done parsing http://na.op.gg/summoner/ajax/update.json/?summonerId=19005629 done parsing http://euw.op.gg/summoner/userName=BEVBUSDRIVER done parsing http://na.op.gg/summoner/ajax/update.json/?summonerId=25266744 undefined:1 undefined ^ SyntaxError: Unexpected token u at Object.parse (native) at Request.parseSummonerRefresh as _callback at self.callback (C:\op.gg-api-master\node_modules\request\request.js:237:22 ) at Request.emit (events.js:107:17) at ClientRequest.self.clientErrorHandler (C:\op.gg-api-master\node_modules\r equest\request.js:342:10) at ClientRequest.emit (events.js:107:17) at Socket.socketOnData (_http_client.js:322:9) at Socket.emit (events.js:107:17) at readableAddChunk (_stream_readable.js:163:16) at Socket.Readable.push (_stream_readable.js:126:10) at TCP.onread (net.js:538:20)

C:\op.gg-api-master>

freakpants commented 9 years ago

it seems to work fine for an undetermined amount of time until this happens. Once every minute i call 10 summoners one after the other via php.

freakpants commented 9 years ago

the code that is called every minute (scheduled task) <?php $time_start = microtime(true); require("connect.php");

$limit = 10;

$sql = 'SELECT player, teamidentity, lkid FROM players WHERE teamidentity != 0 ORDER BY last_history_update ASC LIMIT '.$limit; $stmt = $db->prepare($sql); $stmt->execute();

$players = $stmt->fetchAll(PDO::FETCH_ASSOC); foreach ($players as $player) {

$summ = $string = preg_replace('/\s+/', '', $player['player']);;

$url = 'http://localhost:1337/euw/summoner/'.urlencode($summ);

$refreshurl = 'http://localhost:1337/euw/refresh/'.$player['lkid'];

// $url = 'http://localhost:1337/euw/summoner/B%C3%AAluga';

echo $url.' - '.$refreshurl.'</br>';

file_get_contents($refreshurl);
$response = file_get_contents($url);
$data = json_decode($response); 

echo $player['player'].'</br>';

// determine which team our player is on
$teamidentity = $player['teamidentity'];

// touch player to update the timestamp - even if we dont actually update anything
$sql = "UPDATE players SET last_history_update = CURRENT_TIMESTAMP WHERE player = :player";
$stmt = $db->prepare($sql);
$stmt->execute(array(':player' => $player['player']));

$games = $data->data[0]->games;

foreach($games as $game){

    // only inspect custom (= tournament matches)
    if($game->type == 'Custom'){

        echo 'custom found </br>';
        // check if game is already in db
        $match_identity = $game->gameId;
        $date_time = $game->datetime;
        echo $match_identity.'</br>';
        $inner_sql = "SELECT match_identity from tournament_matches WHERE match_identity = '$match_identity'";
        $inner_stmt = $db->prepare($inner_sql);
        $inner_stmt->execute();
        if($inner_stmt->rowCount() > 0){
            echo 'match already in db </br>';
        } else {
            echo 'adding match '.$match_identity.'</br>';
            // loop over the opposing team to try and find out if any of the opponents was swiss
            echo 'opposing: </br>';

            if($game->teamNum == 1){
                $players = (array)$game->team2;
            } else {
                $players = (array)$game->team1;
            }

            $opposing  = 0;

            $teamsize = 0;

            foreach($players as $player){
                $teamsql = "SELECT teamidentity FROM players WHERE player = :player";
                // echo 'query: '.$teamsql.'</br>';
                $teamstmt = $db->prepare($teamsql);

                $teamstmt->execute(array(':player' => $player->name));
                $teams = $teamstmt->fetchAll(PDO::FETCH_ASSOC);
                foreach($teams as $team){
                    $opposing = $team['teamidentity'];
                }
                $teamsize++;
                echo $player->name.' - Team: '.$opposing.'</br>';
            }
            echo $teamsize.' Players</br>';

            // determine winning team
            $result = $game->result;

            $winner_teamside = 1;

            if($result == 'Defeat'){
                $loser = $teamidentity;
                $winner = $opposing;
                if($game->teamNum == 1){
                    $winner_teamside = 2;
                }

            } else {
                $loser = $opposing;
                $winner = $teamidentity;
                if($game->teamNum == 2){
                    $winner_teamside = 2;
                }
            }
            echo 'winner: '.$winner.'</br>';
            echo 'loser: '.$loser.'</br>';

            $json = json_encode($game);
            // echo $json;
            try {
                $sql = "INSERT INTO tournament_matches(match_identity, json, winner, loser, datetime, winner_teamside) VALUES (:match_identity, :json, :winner, :loser, :datetime, :winner_teamside)";
                $insert = $db->prepare($sql);
                if($winner == $loser) echo 'winner equals loser. ignoring match. </br>';
                if($teamsize == 5 && $winner != $loser && $loser != 0 && $winner != 0){
                    $insert->execute(
                    array(
                    ':match_identity' => $match_identity, 
                    ':json' => $json,
                    ':winner' => $winner,
                    ':loser' => $loser,
                    ':datetime' => $date_time,
                    ':winner_teamside' => $winner_teamside
                    ));
                }

            } catch(PDOException $ex) {
                echo 'Error: '.$ex->getMessage();
            }
        }

    }   
}

} $time_end = microtime(true);

//dividing with 60 will give the execution time in minutes other wise seconds $execution_time = ($time_end - $time_start);

//execution time of the script echo ''.$limit.' Queries. Total Execution Time: '.$execution_time.' Secs. Timer per query ='.($execution_time/$limit).' Secs';

?>

miasmos commented 9 years ago

I forgot to error check the refresh endpoint. I've committed updated code. I think it may be that you're making too many queries too fast. Then again, they don't secure their json endpoints, so I wouldn't be surprised if they didn't throttle page queries.

freakpants commented 9 years ago

problem seems fixed for refreshes - but also happens for summoner requests -

parsing http://na.op.gg/summoner/ajax/update.json/?summonerId=19036982 done parsing http://euw.op.gg/summoner/userName=ERQAractosa C:\op.gg-api-master\node_modules\cheerio\lib\parse.js:62 var oldParent = node.parent || node.root, ^ TypeError: Cannot read property 'parent' of undefined at exports.update (C:\op.gg-api-master\node_modules\cheerio\lib\parse.js:62: 25) at module.exports (C:\op.gg-api-master\node_modules\cheerio\lib\parse.js:24: 3) at Function.exports.load (C:\op.gg-api-master\node_modules\cheerio\lib\stati c.js:19:14) at Request.parseSummoner as _callback at self.callback (C:\op.gg-api-master\node_modules\request\request.js:237:22 ) at Request.emit (events.js:107:17) at ClientRequest.self.clientErrorHandler (C:\op.gg-api-master\node_modules\r equest\request.js:342:10) at ClientRequest.emit (events.js:107:17) at Socket.socketOnData (_http_client.js:322:9) at Socket.emit (events.js:107:17)

freakpants commented 9 years ago

another one:

parsing http://na.op.gg/summoner/ajax/update.json/?summonerId=23920549 undefined:1 <!DOCTYPE html> ^ SyntaxError: Unexpected token < at Object.parse (native) at Request.parseSummonerRefresh as _callback at Request.self.callback (C:\op.gg-api-master\node_modules\request\request.j s:237:22) at Request.emit (events.js:110:17) at Request. (C:\op.gg-api-master\node_modules\request\request.js: 1146:14) at Request.emit (events.js:129:20) at IncomingMessage. (C:\op.gg-api-master\node_modules\request\req uest.js:1097:12) at IncomingMessage.emit (events.js:129:20) at _stream_readable.js:908:16 at process._tickCallback (node.js:355:11)

C:\op.gg-api-master>node server.js