Running BullMQ worker (background process) from a SolidStart project

I have a solidstart project with Redis as its backend. I'm trying to use BullMQ worker: sandboxed processor as the consumer. I'm trying to initialise it during server startup from entry-middleware.js as below:
import { createMiddleware } from "@solidjs/start/middleware";
import { authMiddleware } from "~/lib/auth/auth.middleware";
import logger from "~/lib/logger";
import { initializeAllWorkers, shutdownAllWorkers } from "~/lib/queue/bull/workers/worker.client";

// Flag to ensure we only initialize once
let workersInitialized = false;

// Store worker references for shutdown
let shutdownRegistered = false;

export default createMiddleware({
// some logging

await authMiddleware(event);

// Only initialize workers once
if (!workersInitialized && typeof window === 'undefined') {
workersInitialized = true;

logger.info('Initializing BullMQ workers from middleware...');

try {
await initializeAllWorkers();
logger.info('BullMQ workers initialized successfully');

// Register shutdown handlers (only once)
if (!shutdownRegistered) {
shutdownRegistered = true;
// rest of the code
import { createMiddleware } from "@solidjs/start/middleware";
import { authMiddleware } from "~/lib/auth/auth.middleware";
import logger from "~/lib/logger";
import { initializeAllWorkers, shutdownAllWorkers } from "~/lib/queue/bull/workers/worker.client";

// Flag to ensure we only initialize once
let workersInitialized = false;

// Store worker references for shutdown
let shutdownRegistered = false;

export default createMiddleware({
// some logging

await authMiddleware(event);

// Only initialize workers once
if (!workersInitialized && typeof window === 'undefined') {
workersInitialized = true;

logger.info('Initializing BullMQ workers from middleware...');

try {
await initializeAllWorkers();
logger.info('BullMQ workers initialized successfully');

// Register shutdown handlers (only once)
if (!shutdownRegistered) {
shutdownRegistered = true;
// rest of the code
The file worker.client.js is calling various other modules which has use server directive set. And, I'm getting the below error. I tried a similar approach from entry-server.js with the same outcome!!
"message": "Cannot call server function outside of a request",
"message": "Cannot call server function outside of a request",
I did some digging and it seems running background process is a bit of a pain. Has anyone tried doing anything similar? May you please help.
Sandboxed processors | BullMQ
Running jobs in isolated processes
GitHub
How to hook into the startup for one-off initialization? · solidjs ...
How does SolidStart execute certain tasks on the server side upon startup, such as background scheduled tasks, similar to using an instrumentation file in NestJS for configuration?
1 Reply
peerreynders
peerreynders5d ago
The file worker.client.js is calling various other modules which has use server directive set.
So the issue is that you are marking entire server modules as accessible by the client which as a consequence restricts their use on the server side. I'd keep the server code in a plain modules and create separate modules for the "use server" access points for client RPC.

Did you find this page helpful?