Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Electron: Shell Scripts #142

Open
xvno opened this issue Jul 5, 2021 · 2 comments
Open

Electron: Shell Scripts #142

xvno opened this issue Jul 5, 2021 · 2 comments

Comments

@xvno
Copy link
Owner

xvno commented Jul 5, 2021

Refs

@xvno
Copy link
Owner Author

xvno commented Jul 5, 2021

child_process.spawn

const cp = require('child_process');
const process = cp.spawn(cmd, paramList);

proc.stdout.on("data", chunk => {
    log('data: ', chunk.toString());
});

proc.stderr.on('data', data => {
    stop(data);
});

proc.on('close', code => {
    log(`child process exited with code ${code}`);
    //TODO: update db.take
});

proc.on('exit', function (code, signal) {
    log(`proc: on-exit: code: ${code}, signal: ${signal}`);
    if (signal === 'SIGKILL') {
        let unlink = fs.unlinkSync(destFile);
        cb(null, {
            cxed: 0,
            op: 1,
            taskstate: CODE.CONVERT_PAUSED
        });
    } else {
        const stat = getStat(destFile);
        let size = 0;
        if (stat) {
            size = stat.size;
        }
        cb(null, {
            taskstate: CODE.CONVERT_SUCCESS,
            op: 1,
            total: size,
            taskendTS: Date.now()
        });
    }
});

function stop (data) {
    if (opt.suspend) {
        proc.kill('SIGKILL');
        cb(null, {
            taskstate: CODE.CONVERT_PAUSED,
            op: CODE.CONVERT_OP_PAUSE,
            total: 0,
            taskendTS: Date.now()
        });
        return;
    } else {
        console.log('Not suspended!');
    }
}

@xvno
Copy link
Owner Author

xvno commented Jul 20, 2021

child_process.exec

function handleService (apiName, cmd, cmdArgs = [], {/*  args,  */sender, action, taskerid }) {
    log(apiName);
    const backStatus = {
        isSentBack: false
    };
    cmdArgs.unshift(cmd);
    log(cmdArgs.join(' '));
    exec(cmdArgs.join(' '), (err, stdout, stderr) => {
        if (err) {
            error(`${apiName}.error: `, err);
            return sendBack(
                backStatus,
                {
                    action,
                    sender,
                    code: CODE.STATE_ERROR,
                    message: MESSAGE.STATE_ERROR,
                    data: { detail: err.errno },
                    taskerid
                });

        }
        if (stderr) {
            log(`${apiName}.stderr: `, stderr);
            return sendBack(backStatus,
                {
                    action,
                    sender,
                    code: CODE.STATE_ERROR,
                    message: MESSAGE.STATE_ERROR,
                    data: { detail: stderr },
                    taskerid
                });
        }
        console.log(`stdout: ${stdout}`);
        log(`${apiName}.stdout: `, stdout);
        sendBack(backStatus, { action, sender, code: CODE.STATE_OK, message: MESSAGE.STATE_OK, data: { detail: stdout }, taskerid });
    });

}

@xvno xvno changed the title Electron: Shell commands Electron: Shell Scripts Aug 25, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant