private function safelyWriteData($path, $mode, $data)
    {
        $fp = fopen($path, $mode);
        $retries = 0;

        if (empty($fp)) {
            return false;
        }

        do {
            if ($retries > 0) {
                usleep(static::MS_BETWEEN_RETRIES * 1000);
            }

            $retries++;
        } while (!flock($fp, LOCK_EX) && $retries <= static::MAX_RETRIES);

        if ($retries == static::MAX_RETRIES) {
            return false;
        }


        fwrite($fp, "$data\n");

        flock($fp, LOCK_UN);
        fclose($fp);

        return true;
    }