bree icon indicating copy to clipboard operation
bree copied to clipboard

[fix] Windows 11 Home Edition, "SyntaxError: Invalid or unexpected token"

Open qzw881130 opened this issue 1 year ago • 1 comments
trafficstars

electron + bree.js, package windows app, but the job doesn't work.

Node.js version: 20.14.0

OS version: Windows 11 Home Edition

Description:

but it works fine on Mac OS. just windows OS.... WechatIMG742

Actual behavior

Expected behavior

Code to reproduce

Here is the error.

[worker eval]:1
D:\Program Files (x86)\Box-New-App\resources\app.asar\esrc\jobs\debug.js
  ^

SyntaxError: Invalid or unexpected token
    at makeContextifyScript (node:internal/vm:185:14)
    at node:internal/process/execution:107:22
    at [worker eval]-wrapper:6:24
    at runScript (node:internal/process/execution:101:62)
    at evalScript (node:internal/process/execution:133:3)
    at MessagePort.<anonymous> (node:internal/main/worker_thread:168:9)
    at [nodejs.internal.kHybridDispatch] (node:internal/event_target:825:20)
    at MessagePort.<anonymous> (node:internal/per_context/messageport:23:28)

esrc/crontab.js


const Bree = require("bree");
const { getJob, getIncomingWaitingJobs } = require("./models/jobs");
const path = require('path');
const moment = require('moment');
const Graceful = require('@ladjs/graceful');
const { notify, objReplacer } = require("./helper");
const { initBugSnag, bugsnag } = require("./BugSnagInit");
const { app } = require("electron");
const fs = require('fs')

let mainWin = null;
const errorHandler = (err, workerMetadata) => {
    console.error(`Error occurred in worker ${workerMetadata?.name}:`, err, workerMetadata);
    try{
        bugsnag.notify(new Error(JSON.stringify(err, objReplacer, 2)));
        bugsnag.notify(new Error(JSON.stringify(workerMetadata, objReplacer, 2)));
        mainWin.webContents.send('TaskChange', `child exception1: ${JSON.stringify(err, objReplacer, 2)}`)
        mainWin.webContents.send('TaskChange', `child exception2: ${JSON.stringify(workerMetadata, objReplacer, 2)}`)
    }catch(err){
        console.log('errorHandler mainWin error', err);
    }
  };
  
  // 定义 workerMessageHandler
  const workerMessageHandler = (recvData) => {
    console.log(`Message received from worker:`, recvData);
    try{
        const {message: data} = recvData;
        // mainWin.webContents.send('TaskChange', message);
        try{
            mainWin.webContents.send('TaskChange', data);
            if(data?.action == 'job-change' && (data?.job?.status == 'finish' || data?.job?.status == 'fail')) {
                let body = data?.job?.status == 'finish' ? ` is done successfully` : ` failed`;
                notify('Message', `Job[#${data?.job?.id}] ${body}`)
                if(data?.job?.status == 'fail') bugsnag.notify(new Error(JSON.stringify(data, null, 2)));
            } else if(data == 'over'){
                console.log('kill child')
                // child.kill();
                return;
            }
        }catch(err){
            bugsnag.notify(new Error(JSON.stringify(err, null, 2)));
            mainWin.webContents.send('TaskChange', `child message exception: ${err.message}`)
            throw err;
        }

    }catch(e){
        console.log('mainWin error', e);
    }
  };

const BreeAddJob = async (id)=>{
    const job = await getJob(id);

    const taskPath = path.join(app.getAppPath(), 'esrc', 'jobs', `debug.js`);
    console.log('task path===', taskPath, ' exists=', fs.existsSync(taskPath) ? 'Yes' : 'No');

    console.log('job scheduled at===', moment(job.scheduled_at).format('YYYY-MM-DD HH:mm:ss'));
    console.log('now time===', moment().format('YYYY-MM-DD HH:mm:ss'));

    const jobName= `jod-${id}`;
    await bree.add({
        name: jobName,
        date: new Date(job.scheduled_at),
        path: taskPath,
        worker: {
            name: '-worker-',
            workerData: {
                action: 'create',
                id: job.id,
                version: app.getVersion()
            }
        }
    })
    bree.start(jobName);
}

const BreeInitJob = async ()=>{
    const jobs = await getIncomingWaitingJobs();
    console.log('BreeInitJob jobs', jobs.map(job=> job.id));
    jobs.map(async (job)=>{
        await BreeAddJob(job.id);
    })
}

const BreeRemoveJob = async (id)=>{
    const jobName= `jod-${id}`;
    console.log('BreeRemoveJob jobName', jobName);
    await bree.stop(jobName);
    await bree.remove(jobName);
}

const bree = new Bree({
    root: false,
    jobs: [],
    outputWorkerMetadata: true,
    errorHandler,
    workerMessageHandler
});
BreeInitJob();
const graceful = new Graceful({ brees: [bree] });
graceful.listen();


module.exports = {BreeAddJob, BindMainWin, BreeRemoveJob};

esrc/jobs/debug.js

const { parentPort, workerData } = require('worker_threads');
module.exports = (() => {
    if(parentPort){
        console.log('workerData from job', workerData);
        parentPort.postMessage('hi, example from job');
        parentPort.postMessage('done');
        parentPort.postMessage('after done');
    }else{
        process.exit(0);
    }
})();

### I've tried other job file format.
const { parentPort, workerData } = require('worker_threads');
if(parentPort){
    console.log('workerData from job', workerData);
    parentPort.postMessage('hi, example from job');
    parentPort.postMessage('done');
    parentPort.postMessage('after done');
}else{
    process.exit(0);
}

Checklist

  • [x] I have searched through GitHub issues for similar issues.
  • [x] I have completely read through the README and documentation.
  • [x] I have tested my code with the latest version of Node.js and this package and confirmed it is still not working.

qzw881130 avatar Jul 11 '24 01:07 qzw881130