Next.js ^12 + Worker Queue
I've adopted industries best practices into this template from my professional experience working in big tech, startups and YC companies.
Made by Nam Dao
-
Please set up the Google OAuth 2.0 API & Credentials by following this tutorial
-
Make sure you have the following
.env
file present.DATABASE_URL= Your PostgreSQL url GOOGLE_ID= Your Google Credentials API ID for NextAuth GOOGLE_SECRET= Your Google Credentials API secret for NextAuth SECRET= A secret string used to sign the JWT token for NextAuth
-
Spin up docker and start the postgreSQL via docker compose.
-
Run
npx prisma db push
. Make sure it is pointed at the local db. -
Run
npm run dev
. -
Run
npm run start-workers
to spin up the workers. -
Deploy to Vercel for free 😎
-
Add a new job type to
JobType
inschema.prisma
, then runnpx prisma migrate dev
. -
Add a new worker to a file called
src/workers/[JobType]Worker.ts
using the following template.
import { logger } from '../lib/utils/logger';
import { handleWorkerError, onCompleted, queueOptions } from './utils';
import { JobType } from '@prisma/client';
const Queue = require('bull');
export const newWorker = new Queue(JobType.NEW, queueOptions);
if (process.env.WORKER) {
logger.info('new workers listening...');
newWorker.process(100, async (job: any, done: () => void) => {
try {
// Your logic goes here
// ...
} catch (err) {
await handleWorkerError(job.data.id, err);
}
done();
});
newWorker.on('completed', onCompleted);
}
-
Add
import './[JobType]Worker';
tosrc/workers/index.ts
-
Add a new method called
create[JobType]Worker
toJobService
insrc/services/jobService.ts
. This should be used as the entry point to adding jobs to a worker queue.
Always looking for feedbacks and contributors! Please open an issue or a PR if you have any suggestions 😁