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

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

问题描述

我今天遇到了一个有趣的问题,所以我写这篇文章是为了就如何在现在和将来用我的应用程序的其他部分处理这个问题提供建议(也许可以帮助其他人).

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;
}

很简单,对吧?好吧,事实证明 unlink() 函数间歇性不能正常工作.这很奇怪,因为我在尝试 unlink() 之前进行了 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,而前端是 angular.在极少数情况下,对 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天全站免登陆