-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #19 from Phoenix-Commerce/feat/worker-modes
feat: add bullmq workers and queues
- Loading branch information
Showing
11 changed files
with
184 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import { Worker, Job } from 'bullmq'; | ||
import { Container } from 'typedi'; | ||
import env from '../../../config/config'; | ||
import logger from '../../../config/logger'; | ||
import { SampleService } from './sample-service'; | ||
|
||
const loggerCtx = { context: 'sample-worker' }; | ||
|
||
const sampleWorker = new Worker( | ||
'sampleQueue', | ||
async (job: Job | undefined) => { | ||
if (!job) { | ||
logger.error('Received an undefined job', loggerCtx); | ||
return; | ||
} | ||
|
||
const sampleService = Container.get(SampleService); | ||
const sampleId = job.data.sampleId; | ||
|
||
// Process the sample | ||
const sample = await sampleService.getSampleById(sampleId); | ||
logger.info(`Processing sample: ${sample?.name}`); | ||
// Add your processing logic here | ||
}, | ||
{ | ||
connection: { | ||
host: env.REDIS_HOST, | ||
port: env.REDIS_PORT, | ||
}, | ||
} | ||
); | ||
|
||
sampleWorker.on('completed', (job: Job | undefined) => { | ||
if (job) { | ||
logger.info(`Job ${job.id} in sampleQueue completed!`, loggerCtx); | ||
} else { | ||
logger.error('Completed job is undefined', loggerCtx); | ||
} | ||
}); | ||
|
||
sampleWorker.on('failed', (job: Job | undefined, err: unknown) => { | ||
const errorMessage = typeof err === 'string' ? err : (err instanceof Error ? err.message : JSON.stringify(err)); | ||
if (job) { | ||
logger.error(`Job ${job.id} in sampleQueue failed with error: ${errorMessage}`, loggerCtx); | ||
} else { | ||
logger.error(`A job failed with error: ${errorMessage}`, loggerCtx); | ||
} | ||
}); | ||
|
||
export default sampleWorker; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { connectToDatabase } from './config/database'; | ||
import logger from './config/logger'; | ||
import { initEnforcer } from './rbac'; | ||
import { bootstrap } from './plugins/auth-plugin/bootstrap'; | ||
import PluginLoader from './plugins/plugin-loader'; | ||
|
||
const loggerCtx = { context: 'worker' }; | ||
|
||
export async function startWorker() { | ||
try { | ||
await connectToDatabase(); | ||
await initEnforcer(); // Initialize Casbin | ||
await bootstrap(); // Bootstrap the application with a superuser | ||
|
||
const pluginLoader = new PluginLoader(); | ||
pluginLoader.loadPlugins(); | ||
|
||
// Register models before initializing plugins | ||
pluginLoader.registerModels(); | ||
|
||
// Initialize plugins (extend models and resolvers) | ||
pluginLoader.initializePlugins(); | ||
|
||
// Initialize queues | ||
pluginLoader.initializeQueues(); | ||
|
||
logger.info('Worker started and ready to process jobs', loggerCtx); | ||
} catch (error) { | ||
logger.error('Failed to start worker:', error, loggerCtx); | ||
} | ||
} |