Skip to content

Commit 791d12f

Browse files
committed
update start-worker script
1 parent 0ae044a commit 791d12f

1 file changed

Lines changed: 100 additions & 32 deletions

File tree

scripts/start-worker.js

Lines changed: 100 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -40,42 +40,110 @@ if (
4040

4141
// Start the worker with more detailed logging
4242
async function startWorker() {
43-
console.log("Initializing worker...");
43+
console.log(" Initializing workers...");
4444

4545
try {
46-
// Import the queue and get stats before starting worker
47-
const { emailQueue, getQueueStats } = require("../src/lib/queue/email");
48-
49-
// Check if queue has jobs before starting worker
50-
const stats = await getQueueStats();
51-
console.log("Queue stats before starting worker:", stats);
52-
53-
// Start the worker manually instead of just requiring the module
54-
const { startWorkers } = require("../src/lib/queue/workers/index.ts");
55-
const worker = startWorkers();
56-
57-
console.log("Worker started successfully");
58-
59-
// Add event listeners to the queue to see job events
60-
emailQueue.on("active", (job) => {
61-
console.log(`Job ${job.id} has started processing`);
46+
// Import the centralized queue system
47+
const queueSystem = require("../src/lib/queue");
48+
49+
// Display initial queue stats if available
50+
try {
51+
// Email queue stats
52+
const emailStats = await queueSystem.getQueueStats();
53+
console.log(" Email queue stats:", JSON.stringify(emailStats, null, 2));
54+
55+
// Reminder queue stats
56+
const reminderStats = await queueSystem.getReminderQueueStats();
57+
console.log(
58+
" Reminder queue stats:",
59+
JSON.stringify(reminderStats, null, 2)
60+
);
61+
62+
// Slack queue stats
63+
const slackStats = await queueSystem.getSlackQueueStats();
64+
console.log(" Slack queue stats:", JSON.stringify(slackStats, null, 2));
65+
66+
// Priority email queue stats
67+
const priorityStats = await queueSystem.getPriorityEmailQueueStats();
68+
console.log(
69+
" Priority email queue stats:",
70+
JSON.stringify(priorityStats, null, 2)
71+
);
72+
} catch (statsError) {
73+
console.warn(" Could not get all queue stats:", statsError.message);
74+
}
75+
76+
// Start all workers using the centralized starter
77+
console.log(" Starting all queue workers...");
78+
queueSystem.startWorkers();
79+
console.log(" All workers started successfully");
80+
81+
// Log queue status periodically (every 30 seconds)
82+
const statsInterval = setInterval(async () => {
83+
try {
84+
console.log("\n--- Queue Stats Update ---");
85+
const emailStats = await queueSystem.getQueueStats();
86+
console.log(
87+
" Email queue:",
88+
emailStats.waiting,
89+
"waiting,",
90+
emailStats.active,
91+
"active,",
92+
emailStats.completed,
93+
"completed"
94+
);
95+
96+
const reminderStats = await queueSystem.getReminderQueueStats();
97+
console.log(
98+
" Reminder queue:",
99+
reminderStats.waiting,
100+
"waiting,",
101+
reminderStats.active,
102+
"active,",
103+
reminderStats.completed,
104+
"completed"
105+
);
106+
107+
const slackStats = await queueSystem.getSlackQueueStats();
108+
console.log(
109+
" Slack queue:",
110+
slackStats.waiting,
111+
"waiting,",
112+
slackStats.active,
113+
"active,",
114+
slackStats.completed,
115+
"completed"
116+
);
117+
118+
const priorityStats = await queueSystem.getPriorityEmailQueueStats();
119+
console.log(
120+
" Priority email:",
121+
priorityStats.waiting,
122+
"waiting,",
123+
priorityStats.active,
124+
"active,",
125+
priorityStats.completed,
126+
"completed"
127+
);
128+
} catch (error) {
129+
console.warn(" Error getting queue stats:", error.message);
130+
}
131+
}, 30000); // Every 30 seconds
132+
133+
// Handle graceful shutdown
134+
process.on("SIGINT", () => {
135+
console.log("\n Graceful shutdown initiated...");
136+
clearInterval(statsInterval);
137+
console.log("Queue workers will shut down automatically.");
138+
console.log("Goodbye! ");
139+
process.exit(0);
62140
});
63141

64-
emailQueue.on("completed", (job) => {
65-
console.log(`Job ${job.id} has been completed`);
66-
});
67-
68-
emailQueue.on("failed", (job, err) => {
69-
console.log(`Job ${job.id} has failed with error: ${err.message}`);
70-
});
71-
72-
// Log queue status every 5 seconds
73-
setInterval(async () => {
74-
const currentStats = await getQueueStats();
75-
console.log("Current queue stats:", currentStats);
76-
}, 5000);
77-
78-
return worker;
142+
// Prevent the process from exiting
143+
console.log("All queue workers started. Waiting for jobs...\n");
144+
console.log(
145+
"Press Ctrl+C to exit workers. Redis connections will be cleaned up automatically."
146+
);
79147
} catch (error) {
80148
console.error("Error starting worker:", error);
81149
throw error;

0 commit comments

Comments
 (0)