60 lines
1.7 KiB
JavaScript
60 lines
1.7 KiB
JavaScript
const path = require("path");
|
|
require("dotenv").config({
|
|
path: path.resolve(process.cwd(), `.env.${process.env.NODE_ENV || "development"}`)
|
|
});
|
|
const Queue = require("better-queue");
|
|
const moment = require("moment/moment");
|
|
const { client } = require("../graphql-client/graphql-client");
|
|
const { UPDATE_TASKS_REMIND_AT_SENT } = require("../graphql-client/queries");
|
|
const logger = require("../utils/logger");
|
|
|
|
const taskEmailQueue = () =>
|
|
new Queue(
|
|
(taskIds, cb) => {
|
|
console.log("Processing reminds for taskIds: ", taskIds.join(", "));
|
|
|
|
// Set the remind_at_sent to the current time.
|
|
const now = moment().toISOString();
|
|
|
|
client
|
|
.request(UPDATE_TASKS_REMIND_AT_SENT, { taskIds, now })
|
|
.then((taskResponse) => {
|
|
logger.log("task-remind-email-queue", "info", null, null, taskResponse);
|
|
cb(null, taskResponse);
|
|
})
|
|
.catch((err) => {
|
|
logger.log("task-remind-email-queue", "error", null, null, err);
|
|
cb(err);
|
|
});
|
|
},
|
|
{
|
|
batchSize: 50,
|
|
batchDelay: 5000,
|
|
// The lower this is, the more likely we are to hit the rate limit.
|
|
batchDelayTimeout: 1000
|
|
}
|
|
);
|
|
|
|
// Handle process signals
|
|
process.on("SIGINT", () => {
|
|
taskEmailQueue.destroy();
|
|
process.exit(0);
|
|
});
|
|
process.on("SIGTERM", () => {
|
|
taskEmailQueue.destroy();
|
|
process.exit(0);
|
|
});
|
|
process.on("uncaughtException", (err) => {
|
|
console.error("Unhandled Exception:", err);
|
|
taskEmailQueue.destroy();
|
|
// Perform cleanup or log before exit
|
|
process.exit(1); // Exit with a 'failure' code
|
|
});
|
|
process.on("unhandledRejection", (reason, promise) => {
|
|
console.error("Unhandled Rejection:", reason);
|
|
taskEmailQueue.destroy();
|
|
process.exit(1);
|
|
});
|
|
|
|
module.exports = { taskEmailQueue };
|