1+ import { RedisQueue } from "@muniter/queue" ;
2+ import { createClient } from "redis" ;
3+
4+ interface EmailJobs {
5+ "welcome-email" : { to : string ; name : string } ;
6+ "notification" : { to : string ; subject : string ; body : string } ;
7+ }
8+
9+ // Create Redis client directly
10+ const redisClient = createClient ( { url : 'redis://localhost:6379' } ) ;
11+
12+ redisClient . on ( 'error' , ( err ) => console . error ( 'Redis Client Error:' , err ) ) ;
13+ redisClient . on ( 'connect' , ( ) => console . log ( 'Redis Client Connected' ) ) ;
14+ redisClient . on ( 'ready' , ( ) => console . log ( 'Redis Client Ready' ) ) ;
15+
16+ await redisClient . connect ( ) ;
17+
18+ // Create Redis queue - no adapter needed!
19+ export const emailQueue = new RedisQueue < EmailJobs > ( redisClient , 'email' ) ;
20+
21+ // Register job handlers
22+ emailQueue . onJob ( "welcome-email" , async ( payload ) => {
23+ const { to, name } = payload ;
24+ console . log ( `[Redis] Sending welcome email to ${ to } (${ name } )` ) ;
25+ await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) ) ;
26+ console . log ( `[Redis] Welcome email sent successfully to ${ to } ` ) ;
27+ } ) ;
28+
29+ emailQueue . onJob ( "notification" , async ( payload ) => {
30+ const { to, subject, body } = payload ;
31+ console . log ( `[Redis] Sending notification email to ${ to } : ${ subject } ` ) ;
32+ await new Promise ( ( resolve ) => setTimeout ( resolve , 500 ) ) ;
33+ console . log ( `[Redis] Notification sent successfully` ) ;
34+ } ) ;
35+
36+ // Event listeners
37+ emailQueue . on ( "beforeExec" , ( event ) => {
38+ console . log (
39+ `\n[Redis Queue][${ new Date ( ) . toISOString ( ) } ] Starting ${ event . name } job ${ event . id } ...`
40+ ) ;
41+ } ) ;
42+
43+ emailQueue . on ( "afterExec" , ( event ) => {
44+ console . log (
45+ `[Redis Queue][${ new Date ( ) . toISOString ( ) } ] Job ${ event . id } (${ event . name } ) completed successfully`
46+ ) ;
47+ } ) ;
48+
49+ emailQueue . on ( "afterError" , ( event ) => {
50+ console . error (
51+ `[Redis Queue][${ new Date ( ) . toISOString ( ) } ] Job ${ event . id } (${ event . name } ) failed:` ,
52+ event . error
53+ ) ;
54+ } ) ;
0 commit comments