Skip to content

Commit

Permalink
Make prisma/redis global only
Browse files Browse the repository at this point in the history
  • Loading branch information
gc committed Jul 8, 2024
1 parent 92328da commit f6ea6cf
Show file tree
Hide file tree
Showing 11 changed files with 51 additions and 46 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import './lib/globals';
import './lib/data/itemAliases';
import './lib/data/trophies';
import './lib/crons';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ import { nightmareZoneTask } from './../tasks/minions/minigames/nightmareZoneAct
import { underwaterAgilityThievingTask } from './../tasks/minions/underwaterActivity';
import { modifyBusyCounter } from './busyCounterCache';
import { minionActivityCache } from './constants';
import { convertStoredActivityToFlatActivity, prisma } from './settings/prisma';
import { convertStoredActivityToFlatActivity } from './settings/prisma';
import { activitySync, minionActivityCacheDelete } from './settings/settings';
import { logError } from './util/logError';

Expand Down
41 changes: 41 additions & 0 deletions src/lib/globals.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { isMainThread } from 'node:worker_threads';
import { TSRedis } from '@oldschoolgg/toolkit/dist/lib/TSRedis';
import { PrismaClient } from '@prisma/client';

import { production } from '../config';
import { globalConfig } from './constants';

declare global {
var prisma: PrismaClient;
var redis: TSRedis;
}

function makePrismaClient(): PrismaClient {
if (!production && !process.env.TEST) console.log('Making prisma client...');
if (!isMainThread && !process.env.TEST) {
throw new Error('Prisma client should only be created on the main thread.');
}

return new PrismaClient({
log: [
{
emit: 'event',
level: 'query'
}
]
});
}
// biome-ignore lint/suspicious/noRedeclare: <explanation>
const prisma = global.prisma || makePrismaClient();
global.prisma = prisma;

function makeRedisClient(): TSRedis {
if (!production && !process.env.TEST) console.log('Making Redis client...');
if (!isMainThread && !process.env.TEST) {
throw new Error('Redis client should only be created on the main thread.');
}
return new TSRedis({ mocked: !globalConfig.redisPort, port: globalConfig.redisPort });
}
// biome-ignore lint/suspicious/noRedeclare: <explanation>
const redis = global.redis || makeRedisClient();
global.redis = redis;
2 changes: 1 addition & 1 deletion src/lib/musicCape.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import { championScrolls } from './data/CollectionsExport';
import { RandomEvents } from './randomEvents';
import type { MinigameName } from './settings/minigames';
import { Minigames } from './settings/minigames';
import { getUsersActivityCounts, prisma } from './settings/prisma';
import { getUsersActivityCounts } from './settings/prisma';
import type { RequirementFailure } from './structures/Requirements';
import { Requirements } from './structures/Requirements';
import { itemNameFromID } from './util';
Expand Down
39 changes: 1 addition & 38 deletions src/lib/settings/prisma.ts
Original file line number Diff line number Diff line change
@@ -1,48 +1,11 @@
import { isMainThread } from 'node:worker_threads';
import { TSRedis } from '@oldschoolgg/toolkit/dist/lib/TSRedis';
import type { Activity, Prisma } from '@prisma/client';
import { PrismaClient, activity_type_enum } from '@prisma/client';
import { activity_type_enum } from '@prisma/client';

import { production } from '../../config';
import { globalConfig } from '../constants';
import type { ActivityTaskData } from '../types/minions';
import { sqlLog } from '../util/logger';

declare global {
var prisma: PrismaClient;
var redis: TSRedis;
}

function makePrismaClient(): PrismaClient {
if (!production && !process.env.TEST) console.log('Making prisma client...');
if (!isMainThread && !process.env.TEST) {
throw new Error('Prisma client should only be created on the main thread.');
}

return new PrismaClient({
log: [
{
emit: 'event',
level: 'query'
}
]
});
}
// biome-ignore lint/suspicious/noRedeclare: <explanation>
export const prisma = global.prisma || makePrismaClient();
global.prisma = prisma;

function makeRedisClient(): TSRedis {
if (!production && !process.env.TEST) console.log('Making Redis client...');
if (!isMainThread && !process.env.TEST) {
throw new Error('Redis client should only be created on the main thread.');
}
return new TSRedis({ mocked: !globalConfig.redisPort, port: globalConfig.redisPort });
}
// biome-ignore lint/suspicious/noRedeclare: <explanation>
export const redis = global.redis || makeRedisClient();
global.redis = redis;

export const queryCountStore = { value: 0 };

if (isMainThread) {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { minionActivityCache } from '../constants';
import { channelIsSendable, isGroupActivity } from '../util';
import { handleInteractionError, interactionReply } from '../util/interactionReply';
import { logError } from '../util/logError';
import { convertStoredActivityToFlatActivity, prisma } from './prisma';
import { convertStoredActivityToFlatActivity } from './prisma';

export * from './minigames';

Expand Down
2 changes: 1 addition & 1 deletion src/lib/tickers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { BitField, Channel, PeakTier, informationalButtons } from './constants';
import { GrandExchange } from './grandExchange';
import { cacheGEPrices } from './marketPrices';
import { collectMetrics } from './metrics';
import { prisma, queryCountStore } from './settings/prisma';
import { queryCountStore } from './settings/prisma';
import { runCommand } from './settings/settings';
import { getFarmingInfo } from './skilling/functions/getFarmingInfo';
import Farming from './skilling/skills/farming';
Expand Down
2 changes: 1 addition & 1 deletion src/lib/util/repeatStoredTrip.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { autocompleteMonsters } from '../../mahoji/commands/k';
import type { PvMMethod } from '../constants';
import { SlayerActivityConstants } from '../minions/data/combatConstants';
import { darkAltarRunes } from '../minions/functions/darkAltarCommand';
import { convertStoredActivityToFlatActivity, prisma } from '../settings/prisma';
import { convertStoredActivityToFlatActivity } from '../settings/prisma';
import { runCommand } from '../settings/settings';
import type {
ActivityTaskOptionsWithQuantity,
Expand Down
2 changes: 1 addition & 1 deletion src/mahoji/commands/admin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ import { generateGearImage } from '../../lib/gear/functions/generateGearImage';
import type { GearSetup } from '../../lib/gear/types';
import { GrandExchange } from '../../lib/grandExchange';
import { runRolesTask } from '../../lib/rolesTask';
import { countUsersWithItemInCl, prisma } from '../../lib/settings/prisma';
import { countUsersWithItemInCl } from '../../lib/settings/prisma';
import { cancelTask, minionActivityCacheDelete } from '../../lib/settings/settings';
import { sorts } from '../../lib/sorts';
import { Gear } from '../../lib/structures/Gear';
Expand Down
2 changes: 1 addition & 1 deletion src/mahoji/commands/tools.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import pets from '../../lib/data/pets';
import killableMonsters, { effectiveMonsters, NightmareMonster } from '../../lib/minions/data/killableMonsters';
import type { MinigameName } from '../../lib/settings/minigames';
import { Minigames } from '../../lib/settings/minigames';
import { convertStoredActivityToFlatActivity, prisma } from '../../lib/settings/prisma';
import { convertStoredActivityToFlatActivity } from '../../lib/settings/prisma';
import Skills from '../../lib/skilling/skills';
import {
formatDuration,
Expand Down
2 changes: 1 addition & 1 deletion src/tasks/minions/minigames/infernoActivity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import type { ItemBank } from 'oldschooljs/dist/meta/types';
import { formatDuration } from '@oldschoolgg/toolkit';
import { Events } from '../../../lib/constants';
import { diariesObject, userhasDiaryTier } from '../../../lib/diaries';
import { countUsersWithItemInCl, prisma } from '../../../lib/settings/prisma';
import { countUsersWithItemInCl } from '../../../lib/settings/prisma';
import { getMinigameScore, incrementMinigameScore } from '../../../lib/settings/settings';
import { SkillsEnum } from '../../../lib/skilling/types';
import { calculateSlayerPoints, getUsersCurrentSlayerInfo } from '../../../lib/slayer/slayerUtil';
Expand Down

0 comments on commit f6ea6cf

Please sign in to comment.