breejs / bree

Bree is a Node.js and JavaScript job task scheduler with worker threads, cron, Date, and human syntax. Built for @ladjs, @forwardemail, @spamscanner, @cabinjs.
https://jobscheduler.net
MIT License
3.04k stars 79 forks source link

[fix] Attempting to access worker data within a function job throws `worker_threads_1 is not defined` #246

Open Gmanicus opened 6 months ago

Gmanicus commented 6 months ago

Describe the bug

Node.js version: 18

OS version: Ubuntu (Docker)

Description: After discovering that class functions cannot be used for the scheduler jobs, I created a top-level function and am attempting to access the workerData to run the correct Collector (see #245).

function runCollectorJob() {
    console.log(workerData)
    const { datasource }: { datasource: string } = workerData;
    getCollector(datasource).runJob()
}

// ...elsewhere

... await this.scheduler.add({
                name: options.name,
                path: runCollectorJob,
                worker: {
                     name: options.name,
                     workerData: {
                          datasource: options.name,
                     }
             }
    });
[worker eval]:3
    console.log(worker_threads_1.workerData);
                ^

ReferenceError: worker_threads_1 is not defined
    at runCollectorJob ([worker eval]:3:17)
    at [worker eval]:9:3
    at runScriptInThisContext (node:internal/vm:143:10)
    at node:internal/process/execution:100:14
    at [worker eval]-wrapper:6:24
    at runScript (node:internal/process/execution:83:62)
    at evalScript (node:internal/process/execution:114:10)
    at MessagePort.<anonymous> (node:internal/main/worker_thread:166:9)
    at MessagePort.[nodejs.internal.kHybridDispatch] (node:internal/event_target:786:20)
    at MessagePort.exports.emitMessage (node:internal/per_context/messageport:23:28)

Code to reproduce

I can create a sample reproduction of the problem if needed.

Checklist