-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
898379e
commit 411134f
Showing
3 changed files
with
49 additions
and
43 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,49 +1,52 @@ | ||
import {Queue, Worker, QueueScheduler} from 'bullmq'; | ||
import { redis } from '~/utils/redis.server'; | ||
import { Queue, Worker, QueueScheduler } from "bullmq"; | ||
|
||
import { redis } from "~/utils/redis.server"; | ||
|
||
declare global { | ||
var __notifierQueue: Queue | undefined; | ||
var __notifierWorker: Worker | undefined; | ||
var __notifierScheduler: QueueScheduler | undefined; | ||
var __notifierQueue: Queue | undefined; | ||
var __notifierWorker: Worker | undefined; | ||
var __notifierScheduler: QueueScheduler | undefined; | ||
} | ||
|
||
let queue: Queue; | ||
let worker: Worker; | ||
let scheduler: QueueScheduler; | ||
|
||
const QUEUE_NAME = 'notifier'; | ||
const QUEUE_NAME = "notifier"; | ||
|
||
type QueueData = { | ||
emailAddress: string; | ||
} | ||
emailAddress: string; | ||
}; | ||
|
||
// Bullmq queues are the storage container managing jobs. | ||
queue = global.__notifierQueue || (global.__notifierQueue = new Queue<QueueData>(QUEUE_NAME, { | ||
connection: redis, | ||
})); | ||
export const queue: Queue = | ||
global.__notifierQueue || | ||
(global.__notifierQueue = new Queue<QueueData>(QUEUE_NAME, { | ||
connection: redis | ||
})); | ||
|
||
// Workers are where the meat of our procssing lives within a queue. | ||
// Workers are where the meat of our processing lives within a queue. | ||
// They reach out to our redis connection and pull jobs off the queue | ||
// in an order determined by factors such as job priority, delay, etc. | ||
// The scheduler plays an important role in helping workers stay busy. | ||
worker = global.__notifierWorker || (global.__notifierWorker = new Worker<QueueData>(QUEUE_NAME, async (job) => { | ||
console.log(`Sending email to ${job.data.emailAddress}`); | ||
|
||
// Delay 1 second to simulate sending an email, be it for user registration, a newsletter, etc. | ||
await new Promise((resolve) => setTimeout(resolve, 1000)); | ||
|
||
console.log(`Email sent to ${job.data.emailAddress}`); | ||
}, { | ||
connection: redis, | ||
})); | ||
const worker: Worker = | ||
global.__notifierWorker || | ||
(global.__notifierWorker = new Worker<QueueData>( | ||
QUEUE_NAME, | ||
async job => { | ||
console.log(`Sending email to ${job.data.emailAddress}`); | ||
|
||
// Delay 1 second to simulate sending an email, be it for user registration, a newsletter, etc. | ||
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
|
||
console.log(`Email sent to ${job.data.emailAddress}`); | ||
}, | ||
{ | ||
connection: redis | ||
} | ||
)); | ||
|
||
// Schedulers are used to move tasks between states within the queue. | ||
// Jobs may be queued in a delayed or waiting state, but the scheduler's | ||
// job is to eventually move them to an active state. | ||
scheduler = global.__notifierScheduler || (global.__notifierScheduler = new QueueScheduler(QUEUE_NAME, { | ||
connection: redis, | ||
})); | ||
|
||
export { | ||
queue | ||
} | ||
const scheduler: QueueScheduler = | ||
global.__notifierScheduler || | ||
(global.__notifierScheduler = new QueueScheduler(QUEUE_NAME, { | ||
connection: redis | ||
})); |
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