文件中的函数的中间消失 [英] Files disappearing in the middle of a function

查看:134
本文介绍了文件中的函数的中间消失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我今天碰见有趣的问题,所以我写这篇文章的意见如何,现在对付这一点,并在未来与我的应用程序的其他部分(也许帮助别人)。

Interesting problem that I ran into today, so I'm writing this post for advice for how to deal with this now and in the future with other parts of my application (and maybe help someone else).

protected function unlinkCachePath($cachePath) {
    if (!file_exists($cachePath)) {
        throw new \Exception("Invalid cache file path '$cachePath'");
    }
    if (!is_writable($cachePath)) {
        throw new \Exception("Check permissions for file path '$cachePath'");
    }
    $result = unlink($cachePath); // delete file
    if (!$result) {
        throw new \Exception("Problem deleting cache file '$cachePath'");
    }
    return true;
}

pretty简单的,对不对?那么,事实证明,在取消链接()函数是的间歇的工作不正常。这很奇怪,因为我有一个 file_exists()检查尝试在我的取消链接()

Pretty straightforward, right? Well, it turns out that the unlink() function was intermittently not working correctly. Which was odd, because I had a file_exists() check before attempting my unlink().

但尽管如此,它产生一个未找到文件的错误。于是我出去调试,找出究竟发生了什么事。

But still, it was generating a 'file not found' error. So I went out to debug, to find out what the heck was going on.

protected function unlinkCachePath($cachePath) {

    // debug the cache path
    echo "testing cache path: $cachePath <br />";

    // check if the file exists
    $this->debugFileExists($cachePath,'1'); 

    if (!file_exists($cachePath)) {
       throw new \Exception("Invalid cache file path '$cachePath'");
    }

    // ...check again
    $this->debugFileExists($cachePath,'2'); 

    if (!is_writable($cachePath)) {
       throw new \Exception("Check permissions for file path '$cachePath'");
    }

    // ...and again
    $this->debugFileExists($cachePath,'3'); 

    $result = unlink($cachePath); // delete file

    // ...and again
    $this->debugFileExists($cachePath,'4');

    if (!$result) {
        throw new \Exception("Problem deleting cache file '$cachePath'");
    }
    return true;
}


private function debugFileExists($filePath, $attemptNumber) {
    if (file_exists($filePath)) {
        $response = "pass";
    } else { 
        $response = "fail";
    }
    echo "file_exists() test $attemptNumber: $response <br />";
}

结果:

testing cache path: /path/to/file.json
file_exists() test 1: pass
file_exists() test 2: pass
file_exists() test 3: fail
# unlink(file): No such file or directory
file_exists() test 4: fail

等等,什么??

在这里,我,抓我的头。该文件如何能够存在,然后在同样的功能,突然不存在?上究竟发生了什么可能会发生任何见解?

Here I am, scratching my head. How can the file exist, and then in the same function, suddenly not exist? Any insight on to what the heck could be happening?

感谢您。

推荐答案

原来,我的应用程序的API后端PHP,而前端是棱角分明。在极少数情况下,多个前端调用的API,它击中 unlinkCachePath($ cachePath),会同时发生 - 和在函数中间,该文件将删除!因为这个特殊的问题,这个问题没有得到广泛的测试抓住了。

Turns out, my application's API backend is PHP, while the frontend is angular. In rare cases, multiple frontend calls to an API, which hits unlinkCachePath($cachePath), would happen simultaneously -- and in the middle of a function, the file would delete! Because of this particular problem, the issue was not caught by extensive testing.

这篇关于文件中的函数的中间消失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆