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

Support sandboxed processors in Next.js #2572

Open
lafbelmonte opened this issue May 20, 2024 · 1 comment
Open

Support sandboxed processors in Next.js #2572

lafbelmonte opened this issue May 20, 2024 · 1 comment

Comments

@lafbelmonte
Copy link

lafbelmonte commented May 20, 2024

Is your feature request related to a problem? Please describe.
I want to run sandboxed processors in Next.js but the way it is architectured with webpack is giving me a hard time. The way I am planning to do this with Next.js is create a different entry point (webpack) for my task then pass it as path in the sandboxed processor:

next.config.js:

    webpack: (config, {isServer}) => {
        if (isServer && config.name === 'server') {
            const oldEntry = config.entry;

            return {
                ...config,
                async entry(...args) {
                    const entries = await oldEntry(...args);
                    return {
                        ...entries,
                        task: path.resolve(process.cwd(), 'path/to/task.ts'),
                    };
                },
            };
        }

        return config;
    },

The task file will be generated at .next/server/task.js, so I can just pass the path to the worker as ${__dirname}/.next/server/task.js.

Task file:

const task = async () => {
    console.log('PONG!');
};

export default task;

Now somehow after importing the processor file (${__dirname}/.next/server/task.js) here: https://github.com/taskforcesh/bullmq/blob/master/src/classes/child-processor.ts#L30, processorFn is a Promise, thus giving me:

processorFn value:
Promise {
  <pending>,
  [Symbol(webpack exports)]: Object [Module] { default: [Getter] },
  [Symbol(webpack queues)]: [Function (anonymous)]
}

error:
Error: No function is exported in processor file

I'm really not sure why its returning a Promise, maybe some webpack dynamic import thing?

Describe the solution you'd like
Support processorFn being a Promise, check if it is a Promise then use await.
This worked for my case:

        try {
    
            const { default: processorFn } = await import(processorFile);
            processor = await processorFn; <-- added await here
            if (processor.default) {
                // support es2015 module.
                processor = processor.default;
            }
            if (typeof processor !== 'function') {
                throw new Error('No function is exported in processor file');
            }
        }

Describe alternatives you've considered
N/A

Additional context
N/A

@manast
Copy link
Contributor

manast commented May 23, 2024

I think that without really knowing why webpack is returning a promise when doing a dynamic import, I am going to be reluctant to implemente the proposed solution... it could be the result of something specific in your webpack setup, but even if not, it feels really hacky to do an extra await for seemly no reason.

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

2 participants