csgo-league-web icon indicating copy to clipboard operation
csgo-league-web copied to clipboard

web matches cache json folder when have too much files returns a php timeout error

Open 39ma opened this issue 5 years ago • 8 comments

Describe the bug When in app/cache/matches folder stucks too many match json files (like mine had ~2000), then when discord bot creating a server, web return a php error of max execution time.

Possible fix* I fixed it by running bash cronjob to clean old .json files from that folder, and it solved a problem to me. PHP error points to this function. I tested it, DB is answering very fastly, but checking MATCHES_CACHE takes a long time if there are a lot of files.

protected function doesMatchIdExist(int $matchId): bool
    {
        // Check whether the matchId already exists in the database.
        $query = $this->db->query('SELECT matches.matchid FROM matches WHERE matchid = :matchId', [
            ':matchId' => $matchId
        ]);

        $matches = array_filter(scandir(self::MATCHES_CACHE), function ($item) {
            return !is_dir(self::MATCHES_CACHE . "/$item");
        });

        return $query->rowCount() !== 0 || in_array("$matchId.json", $matches);
    }

39ma avatar Oct 07 '20 06:10 39ma

This is a good solution, I'll happily accept a PR for this.

B3none avatar Oct 07 '20 08:10 B3none

5 7 * * * find /var/www/csgo-league-web/app/cache/matches -name '*.json' -mtime +10 -delete

to crontab of Linux OS.

This is a good solution, I'll happily accept a PR for this.

39ma avatar Oct 07 '20 08:10 39ma

idk how to PR this because its not, a part of code.

39ma avatar Oct 07 '20 08:10 39ma

Is there a way to figure out the next match ID without having to read all the JSON files? What's even in the JSON files? I thought all the data gets stored in the matches table.

cameronshinn avatar Oct 07 '20 17:10 cameronshinn

It's so that the server can get all the match information - it's pretty much just formatted data from the table with a bit of extra data bolted on.

B3none avatar Oct 07 '20 18:10 B3none

Why does the server need all of the information from previous matches when it's creating a new one? Shouldn't only need to look at their IDs to create a new ID. Don't you use auto-incrementing keys in the matches table?

cameronshinn avatar Oct 07 '20 18:10 cameronshinn

They're used for the match requests

B3none avatar Oct 07 '20 19:10 B3none

@b3none I think using array_flip() and isset() is much faster than in_array()

    protected function doesMatchIdExist(int $matchId): bool
    {
        // Check whether the matchId already exists in the database.
        $query = $this->db->query('SELECT matches.matchid FROM matches WHERE matchid = :matchId', [
            ':matchId' => $matchId
        ]);

        $matches = array_filter(scandir(self::MATCHES_CACHE), function ($item) {
            return !is_dir(self::MATCHES_CACHE . "/$item");
        });

        $flipped_matches = array_flip($matches);

        return $query->rowCount() !== 0 || isset($flipped_matches["$matchId.json"]);
    }

thboss avatar Oct 12 '20 11:10 thboss