同步机制在本地机器上完美工作,但在Heroku上删除所有video

我有一个启动时运行的机制,将删除任何孤立的数据。 它遍历video和检查的mongodb集合,以确保它可以将db中的logging匹配到磁盘上相应的文件。 如果它发现一个孤儿logging它删除它。 然后翻转目录,确保所有find的文件在db中都有匹配的logging。

当我在本地机器上运行它时,它完美地工作。 但是,每次我部署到heroku和我的dyno重新启动,整个'video'集合被删除。

在Heroku中创buildvideo后,我做了一些基本的testing,它在数据库中,可供下载。 我的GUESS是Heroku不喜欢fs.access的东西。

任何帮助或见解将不胜感激。

walkVideos: function() { // First let's lookup videos and make sure there is a directory for them. If not, delete it from the db. Video.find().exec(function(err, result) { if (err) { console.log(err); return null; } if (result.length === 0) { console.log('No videos found in db'); return null; } _.forEach(result, function(video) { // make sure I can access the style file. fs.access(scormifyConfig.videoBasePath + video._id + '/dist/' + video._id + '_' + video.filename + '.zip', function(err) { if (err && err.code === 'ENOENT') { //Can't find the style file, i need to delete it Video.remove({_id: video._id}, function(err, result) { if (err) { console.log('Error removing video from the db'); } if (result) { console.log('Out of sync video pruned from the db'); } }); } }); // console.log('Matched video from DB to disk. No action.'); }); }); // let's walk the other way and make sure all videos found on disk have a match in the db at the folder level. fsp.traverseTreeSync(scormifyConfig.videoBasePath, // walk through the content directory function(file) { }, function(dir) { let _id = dir.replace('content\\videos\\', ''); // on dir, trim to just the id. this can be matched to the DB _id field. Video.find({_id: _id}, function(err, result) { if (err) { console.log(err); } if (result.length === 0) { // didn't find the video. console.log('video found on disk, but not in the db. Removing from disk.'); fs.removeSync(scormifyConfig.videoBasePath + _id, function(err) { if (err) { console.log(err); } }); } //console.log('Video matched from disk to DB. No action'); }); }, function() { console.log('done checking videos.'); }); } 

编辑

我做了一些额外的debugging。 我仍然不确定为什么会出现这种情况,它看起来应该起作用。

 Jul 14 14:40:37 scormify app[web] { [Error: ENOENT: no such file or directory, access './content/videos/5787f91a9332950300d85ad4/dist/5787f91a9332950300d85ad4_da-dka-dlk-lsk.zip'] Jul 14 14:40:37 scormify app[web] errno: -2, Jul 14 14:40:38 scormify app[web] Out of sync video pruned from the db 

Dynos有Heroku所谓的“临时文件系统”。 换句话说,在部署之间并不是一成不变的。 请参阅https://devcenter.heroku.com/articles/dynos#ephemeral-filesystem

所以你看到的是正常的行为,尽pipe现在对你来说非常不方便。 其背后的原因是,当您开始增加您的应用程序正在运行的dynos数量时,dyno本地的文件系统不会扩展。 它也打破了可以一次性使用的原则。

推荐的替代方法是将文件(如您的video)存储在另一个更适合此目的的服务上,例如Amazon S3。