11import { PostgresQueueAdapter } from "@vorsteh-queue/adapter-drizzle"
22import { Queue } from "@vorsteh-queue/core"
33
4- import { db , client } from "./database"
4+ import { client , db } from "./database"
55
66// Job payload types
77interface ReportJob {
88 userId : string
9- type : ' daily' | ' weekly' | ' monthly'
9+ type : " daily" | " weekly" | " monthly"
1010 includeCharts ?: boolean
1111}
1212
@@ -18,7 +18,7 @@ interface CleanupJob {
1818// Job result types
1919interface ReportResult {
2020 reportId : string
21- status : ' completed' | ' failed'
21+ status : " completed" | " failed"
2222 fileSize ?: number
2323}
2424
@@ -28,40 +28,42 @@ interface CleanupResult {
2828}
2929
3030// Queue setup
31- const queue = new Queue ( new PostgresQueueAdapter ( db ) , {
31+ const queue = new Queue ( new PostgresQueueAdapter ( db , { modelName : "customQueueJobs" } ) , {
3232 name : "advanced-queue" ,
3333 removeOnComplete : 20 ,
34- removeOnFail : 10
34+ removeOnFail : 10 ,
3535} )
3636
3737// Job handlers with proper types
3838queue . register < ReportJob , ReportResult > ( "generate-report" , async ( job ) => {
3939 const { userId, type, includeCharts = false } = job . payload
40- console . log ( `📈 Generating ${ type } report for user ${ userId } ${ includeCharts ? ' with charts' : '' } ` )
41-
40+ console . log (
41+ `📈 Generating ${ type } report for user ${ userId } ${ includeCharts ? " with charts" : "" } ` ,
42+ )
43+
4244 // Simulate report generation with progress
43- const steps = [ ' Collecting data' , ' Processing metrics' , ' Generating charts' , ' Finalizing report' ]
45+ const steps = [ " Collecting data" , " Processing metrics" , " Generating charts" , " Finalizing report" ]
4446 for ( let i = 0 ; i < steps . length ; i ++ ) {
4547 console . log ( ` ${ steps [ i ] } ...` )
4648 await new Promise ( ( resolve ) => setTimeout ( resolve , 1000 ) )
4749 await job . updateProgress ( Math . round ( ( ( i + 1 ) / steps . length ) * 100 ) )
4850 }
49-
50- return {
51- reportId : `report_${ Date . now ( ) } ` ,
51+
52+ return {
53+ reportId : `report_${ Date . now ( ) } ` ,
5254 status : "completed" ,
53- fileSize : Math . floor ( Math . random ( ) * 1000000 ) + 100000
55+ fileSize : Math . floor ( Math . random ( ) * 1000000 ) + 100000 ,
5456 }
5557} )
5658
5759queue . register < CleanupJob , CleanupResult > ( "cleanup-files" , async ( job ) => {
58- const { olderThan, fileTypes = [ ' tmp' , ' log' ] } = job . payload
59- console . log ( `🧽 Cleaning up ${ fileTypes . join ( ', ' ) } files older than ${ olderThan } ` )
60-
60+ const { olderThan, fileTypes = [ " tmp" , " log" ] } = job . payload
61+ console . log ( `🧽 Cleaning up ${ fileTypes . join ( ", " ) } files older than ${ olderThan } ` )
62+
6163 await new Promise ( ( resolve ) => setTimeout ( resolve , 1500 ) )
6264 const deletedCount = Math . floor ( Math . random ( ) * 50 ) + 10
6365 const freedSpace = deletedCount * Math . floor ( Math . random ( ) * 1000000 )
64-
66+
6567 return { deletedCount, freedSpace }
6668} )
6769
@@ -94,21 +96,33 @@ async function main() {
9496 console . log ( "🚀 Starting Advanced Drizzle PostgreSQL Queue Example" )
9597
9698 // Add jobs with different priorities and features
97- await queue . add < ReportJob > ( "generate-report" , {
98- userId : "user123" ,
99- type : "monthly" ,
100- includeCharts : true
101- } , { priority : 1 } )
102-
103- await queue . add < CleanupJob > ( "cleanup-files" , {
104- olderThan : "30d" ,
105- fileTypes : [ 'tmp' , 'log' , 'cache' ]
106- } , { priority : 3 } )
107-
108- await queue . add < ReportJob > ( "generate-report" , {
109- userId : "user456" ,
110- type : "weekly"
111- } , { priority : 2 , delay : 5000 } )
99+ await queue . add < ReportJob > (
100+ "generate-report" ,
101+ {
102+ userId : "user123" ,
103+ type : "monthly" ,
104+ includeCharts : true ,
105+ } ,
106+ { priority : 1 } ,
107+ )
108+
109+ await queue . add < CleanupJob > (
110+ "cleanup-files" ,
111+ {
112+ olderThan : "30d" ,
113+ fileTypes : [ "tmp" , "log" , "cache" ] ,
114+ } ,
115+ { priority : 3 } ,
116+ )
117+
118+ await queue . add < ReportJob > (
119+ "generate-report" ,
120+ {
121+ userId : "user456" ,
122+ type : "weekly" ,
123+ } ,
124+ { priority : 2 , delay : 5000 } ,
125+ )
112126
113127 // Add recurring cleanup job
114128 await queue . add < CleanupJob > (
@@ -125,7 +139,7 @@ async function main() {
125139 { userId : "system" , type : "daily" } ,
126140 {
127141 cron : "0 9 * * *" , // Every day at 9 AM
128- }
142+ } ,
129143 )
130144
131145 // Start processing
@@ -137,7 +151,7 @@ async function main() {
137151 const stats = await queue . getStats ( )
138152 console . log ( "📊 Detailed Queue Stats:" , {
139153 ...stats ,
140- total : Object . values ( stats ) . reduce ( ( sum , count ) => sum + count , 0 )
154+ total : Object . values ( stats ) . reduce ( ( sum , count ) => sum + count , 0 ) ,
141155 } )
142156 } , 15000 )
143157
0 commit comments