Files
habitica/website/server/libs/worker.js
T
Kalista Payne fb2eaa3950 Move user deletion to worker (#15586)
* WIP(delete): remove business logic from controller

* fix(deletion): handle group leave logic on app server still

* fix(lint): unused import

* fix(import): bracket syntax

* fix(test): adapt test for worker flow

* fix(deletion): update delete/feedback form copy

* fix(text): don't break to new paragraph about Gems

* fix(deletion): remove orphaned chat messages

* Revert "fix(deletion): handle group leave logic on app server still"

This reverts commit 9db541f4c3.

* fix(tests): remove tests
These can potentially be tested in the worker's suite? They target functionality that the group leave route handles within the deletion flow

* fix(lint): no-undef

* refactor redis setup into own file and use ioredis

* use bullmq directly to schedule jobs

* add space

* add key prefix

* add semicolon

* fix(jobs): update redis package

---------

Co-authored-by: Phillip Thelen <phillip@habitica.com>
2026-05-26 17:43:49 -05:00

50 lines
1.2 KiB
JavaScript

import nconf from 'nconf';
import { Queue } from 'bullmq';
import setupRedis from './redis';
import SERVER_STATUS from './serverStatus';
let redisClient;
const queues = {};
if (nconf.get('WORKER_REDIS_URL')) {
redisClient = setupRedis({
url: nconf.get('WORKER_REDIS_URL'),
username: nconf.get('WORKER_REDIS_USERNAME'),
password: nconf.get('WORKER_REDIS_PASSWORD'),
});
redisClient.on('ready', () => {
SERVER_STATUS.WORKER = true;
});
redisClient.on('reconnecting', () => {
SERVER_STATUS.WORKER = false;
});
const queueConfig = {
connection: redisClient,
};
if (nconf.get('WORKER_REDIS_KEY_PREFIX')) {
queueConfig.prefix = nconf.get('WORKER_REDIS_KEY_PREFIX');
}
queues.email = new Queue('emails', queueConfig);
queues.deleteUser = new Queue('DeleteUsers', queueConfig);
} else {
SERVER_STATUS.WORKER = true;
}
function sendJob (type, config) {
if (!queues[type]) {
return Promise.reject(new Error(`Queue ${type} does not exist`));
}
const { identifier, data } = config;
return queues[type].add(identifier, data);
}
export function getRedisClient () {
return redisClient;
}
export default { sendJob };