<commit_before><?php

namespace djeux\queue\helpers;


use yii\db\Connection;
use yii\db\Exception;

class TimeoutTrait
{
    /**
     * Check whether connection is alive and the server has not "gone away"
     * Supports mysql for now
     *
     * @param Connection $db
     * @throws Exception
     */
    public function dbKeepAlive(Connection $db)
    {
        try {
            $db->createCommand('DO 1')->execute();
        } catch (Exception $e) {
            if ($this->causedByLostConnection($e)) {
                $db->close(); $db->open();
            } else {
                throw $e;
            }
        }
    }

    /**
     * @param Exception $e
     * @return bool
     */
    private function causedByLostConnection(Exception $e)
    {
        $message = $e->getMessage();

        $needles = [
            'server has gone away',
            'no connection to the server',
            'Lost connection',
            'is dead or not enabled',
            'Error while sending',
            'decryption failed or bad record mac',
            'server closed the connection unexpectedly',
            'SSL connection has been closed unexpectedly',
            'Error writing data to the connection',
            'Resource deadlock avoided',
        ];

        foreach ($needles as $needle) {
            if (mb_strpos($message, $needle)) {
                return true;
            }
        }

        return false;
    }
}<commit_msg>BUGFIX: fix TimeoutTrait not being a trait, duh :(<commit_after><?php

namespace djeux\queue\helpers;


use yii\db\Connection;
use yii\db\Exception;

trait TimeoutTrait
{
    /**
     * Check whether connection is alive and the server has not "gone away"
     * Supports mysql for now
     *
     * @param Connection $db
     * @throws Exception
     */
    public function dbKeepAlive(Connection $db)
    {
        try {
            $db->createCommand('DO 1')->execute();
        } catch (Exception $e) {
            if ($this->causedByLostConnection($e)) {
                $db->close(); $db->open();
            } else {
                throw $e;
            }
        }
    }

    /**
     * @param Exception $e
     * @return bool
     */
    private function causedByLostConnection(Exception $e)
    {
        $message = $e->getMessage();

        $needles = [
            'server has gone away',
            'no connection to the server',
            'Lost connection',
            'is dead or not enabled',
            'Error while sending',
            'decryption failed or bad record mac',
            'server closed the connection unexpectedly',
            'SSL connection has been closed unexpectedly',
            'Error writing data to the connection',
            'Resource deadlock avoided',
        ];

        foreach ($needles as $needle) {
            if (mb_strpos($message, $needle)) {
                return true;
            }
        }

        return false;
    }
}
{'fraction_non_alphanumeric': 0.10722768917543264, 'fraction_numerical': 0.0006786562606040041, 'mean_word_length': 1.6111603188662533, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3489599', 'n_tokens_mistral': 805, 'n_tokens_neox': 778, 'n_words': 238}