电子:在保留数据库时崩溃

时间:2019-02-28 03:40:34

标签: javascript electron

我正在创建一个电子应用程序,该应用程序可以下载图像并将其标签存储在数据库中。我目前在内存数据库中使用lokijs和loki-structured-file-adapter进行持久化。但是,在使用我的应用程序时,我遇到了死机的蓝屏。我不知道为什么会发生这些崩溃。

当我的计算机在崩溃后重新启动时,该应用程序尝试下载的最后两个图像已损坏,我的loki.db.0文件也已损坏。我无法诊断是下载图像还是持久存储数据库,导致我的计算机崩溃,但这很可能是这两件事之一。

Windows资源管理器报告图像似乎是正确的文件大小。我尝试了免费的文件恢复软件并转换了文件扩展名,但恢复失败。根据恢复软件,图像为空,尝试在记事本中打开图像支持此操作,因为我返回的是空字符行,而不是通常的编码字符行。崩溃似乎是随机的,因为稍后再次下载相同的映像不会产生相同的崩溃。

在Visual Studio Code中打开loki.db.0 json文件将返回一个损坏的数据库,其外观类似于:

...
{"url": "...", "files": [{"name": "1.jpeg", "width": 1280, "height": 1800}, {"name": "2.jpeg", "width": 1280, "height": 1800}], bytes: 1025050}
{"url": "...", "files": [{"name": "1.png", "width": 2048, "height": 2048}, {"name": "2.png", "width": 1300, "height": 1800 }], byte��������
�������������������������������������������������������������������������������������
�������������������������������������������������������������������������������������
...

这是创建数据库的代码:

module.exports = class {

    constructor() {
        var database = this;
        this.db;
        this.downloads;
    }

    init() {
        const loki = require('lokijs');
        const lfsa = require('../node_modules/lokijs/src/loki-fs-structured-adapter.js');

        return new Promise((resolve) => {
            var adapter = new lfsa();
            database.db = new loki(pathLocal + '/loki.db', { 
                adapter : adapter,
                autoload: true,
                autoloadCallback : database.createCollections,
                autosave: true, 
                autosaveInterval: 10000
            });
        });
    }

    createCollections() {
        database.downloads = database.db.getCollection('downloads');
        if (database.downloads === null) {
            database.downloads = database.db.addCollection('downloads');
        }
    }
}

这是保存图像的代码:

const request = require('request');
var progress = require('request-progress');
const mime = require('mime');
...
var entry = database.downloads.findOne({ '$loki': queuedId });
downloadImages(entry);

downloadImages(entry) {
    if (entry.files == entry.length) return 'done';
    var downloadPath = path.resolve(settings.download.downloadPath, entry.$loki.toString(), page.toString());
    var req = request(getPage(entry));

    downloadImage(entry, req, downloadPath).then((fileInfo) => {
        entry.files.push(fileInfo);
        entry.bytes += fileInfo.bytes;
        database.downloads.update(entry);

        // Download the next page
        downloadImages(entry);
    });
}


downloadImage(entry, req, filename) {
    return new Promise((resolve, reject) => {
        var dirname = path.dirname(filename);
        if (!fs.existsSync(dirname)) fs.ensureDirSync(dirname);
        var image;
        var bytes;

        progress(req, { delay: 0 }).on('progress', function (state) {
            updateProgress(entry, state.speed);
        })
        .on('response', response => {
            if (response.statusCode == 200) {
                filename = filename + '.' + mime.getExtension(response.headers['content-type']);
                image = fs.createWriteStream(filename);
                bytes = response.headers['content-length'];
                req.pipe(image);
             } else {
                reject(response.statusCode);
             }
         })
        .on('error', function (error) {
            reject(error);
        })
        .on('end', function () {
            image.close();
            try {
                var input = require('fs').createReadStream(filename);
                probe(input).then(result => {
                    input.destroy();
                    if (result != null) {
                        resolve({
                            'name': filename,
                            'bytes': bytes,
                            'width': result.width,
                            'height': result.height,
                        });
                    } else {
                        reject('not image or corrupt');
                    }
                });
            }
            catch (error) {
                reject(error);
            }
        });
    });
}

此代码中是否有可能导致这些崩溃的内容?如果没有,我可以在计算机崩溃之前捕获错误或从崩溃中查看错误日志吗?如果不是,lokijs是否有其他替代方法可能会更好?

编辑: 我再次运行程序,并且出于未知原因而不是蓝屏检查,只有程序崩溃了。而不是空的图像文件,而是在下载过程中切断了图像,并完全清空了loki.db.0文件。这是产生的对数电子:

npm ERR! code ELIFECYCLE
npm ERR! errno 3221225477
npm ERR! electron-quick-start@1.0.0 start: `electron .`
npm ERR! Exit status 3221225477
npm ERR!
npm ERR! Failed at the electron-quick-start@1.0.0 start script.
npm ERR! This is probably not a problem with npm. There is likely additional logging output above.

npm ERR! A complete log of this run can be found in:
npm ERR!     C:\Users\*****\AppData\Roaming\npm-cache\_logs\2019-02-28T03_57_10_031Z-debug.log

和日志文件:

0 info it worked if it ends with ok
1 verbose cli [ 'C:\\Program Files\\nodejs\\node.exe',
1 verbose cli   'C:\\Users\\*****\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js',
1 verbose cli   'start' ]
2 info using npm@6.4.1
3 info using node@v8.11.4
4 verbose run-script [ 'prestart', 'start', 'poststart' ]
5 info lifecycle electron-quick-start@1.0.0~prestart: electron-quick-start@1.0.0
6 info lifecycle electron-quick-start@1.0.0~start: electron-quick-start@1.0.0
7 verbose lifecycle electron-quick-start@1.0.0~start: unsafe-perm in lifecycle true
8 verbose lifecycle electron-quick-start@1.0.0~start: PATH: C:\Users\*****\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\node-gyp-bin;C:\Users\*****\Documents\My Projects\My Program\node_modules\.bin;C:\Windows\system32;C:\Windows;C:\Windows\System32\Wbem;C:\Windows\System32\WindowsPowerShell\v1.0\;C:\Windows\System32\OpenSSH\;C:\Program Files\nodejs\;C:\Program Files\Git\cmd;C:\Program Files\dotnet\;C:\Program Files\PuTTY\;C:\Ruby25-x64\bin;C:\Users\*****\AppData\Local\Microsoft\WindowsApps;;C:\Users\*****\AppData\Local\Programs\Microsoft VS Code\bin;C:\Users\*****\AppData\Roaming\npm;C:\Users\*****\.dotnet\tools
9 verbose lifecycle electron-quick-start@1.0.0~start: CWD: C:\Users\*****\Documents\My Projects\My Program
10 silly lifecycle electron-quick-start@1.0.0~start: Args: [ '/d /s /c', 'electron .' ]
11 silly lifecycle electron-quick-start@1.0.0~start: Returned: code: 3221225477  signal: null
12 info lifecycle electron-quick-start@1.0.0~start: Failed to exec start script
13 verbose stack Error: electron-quick-start@1.0.0 start: `electron .`
13 verbose stack Exit status 3221225477
13 verbose stack     at EventEmitter.<anonymous> (C:\Users\*****\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\index.js:301:16)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at EventEmitter.emit (events.js:214:7)
13 verbose stack     at ChildProcess.<anonymous> (C:\Users\*****\AppData\Roaming\npm\node_modules\npm\node_modules\npm-lifecycle\lib\spawn.js:55:14)
13 verbose stack     at emitTwo (events.js:126:13)
13 verbose stack     at ChildProcess.emit (events.js:214:7)
13 verbose stack     at maybeClose (internal/child_process.js:925:16)
13 verbose stack     at Process.ChildProcess._handle.onexit (internal/child_process.js:209:5)
14 verbose pkgid electron-quick-start@1.0.0
15 verbose cwd C:\Users\*****\Documents\My Projects\My Program
16 verbose Windows_NT 10.0.17134
17 verbose argv "C:\\Program Files\\nodejs\\node.exe" "C:\\Users\\*****\\AppData\\Roaming\\npm\\node_modules\\npm\\bin\\npm-cli.js" "start"
18 verbose node v8.11.4
19 verbose npm  v6.4.1
20 error code ELIFECYCLE
21 error errno 3221225477
22 error electron-quick-start@1.0.0 start: `electron .`
22 error Exit status 3221225477
23 error Failed at the electron-quick-start@1.0.0 start script.
23 error This is probably not a problem with npm. There is likely additional logging output above.
24 verbose exit [ 3221225477, true ]

任何人都可以解释此日志的含义吗?

修改2: 我再次运行该程序,并遇到了另一个蓝屏崩溃。这次,当我重新启动并检查文件时,只有两个最近下载的映像被破坏,数据库保持不变。这很可能意味着崩溃是由于下载图像而不是持久保存数据库引起的(尽管如果程序崩溃或我突然终止它,看起来数据库损坏仍然是一个问题)。有人对我的程序为什么崩溃或我可以采取进一步调试的步骤有任何解释吗?我有什么办法可以防止程序崩溃时数据库损坏?

0 个答案:

没有答案