Skip to content

Commit 0fecd69

Browse files
authored
Merge pull request #3910 from effigies/refactor/lazy-clients
refactor(server): Lazy-initialize Redis, Redlock, and Elasticsearch clients, fix import cycles
2 parents 9a03fe8 + 611f689 commit 0fecd69

30 files changed

Lines changed: 769 additions & 255 deletions

.pnp.cjs

Lines changed: 227 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
"scripts": {
44
"start": "docker-compose up --build -V",
55
"build": "yarn pnpify tsc -b",
6+
"dump-schema": "yarn tsx scripts/dump-schema.ts",
67
"bootstrap": "lerna bootstrap",
78
"test": "vitest",
89
"coverage": "vitest --coverage",
@@ -43,6 +44,7 @@
4344
"react": "18.2.0",
4445
"sass": "1.93.2",
4546
"tsc-watch": "6.0.4",
47+
"tsx": "^3.7.0",
4648
"typescript": "5.6.3",
4749
"typescript-eslint": "^8.11.0",
4850
"vite": "7.3.2",

packages/openneuro-server/scripts/dump-schema.test.ts

Lines changed: 0 additions & 40 deletions
This file was deleted.

packages/openneuro-server/src/app.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ import * as jwt from "./libs/authentication/jwt"
2222
import * as auth from "./libs/authentication/states"
2323
import { sitemapHandler } from "./handlers/sitemap"
2424
import { setupPassportAuth } from "./libs/authentication/passport"
25-
import { redis } from "./libs/redis"
25+
import { getRedis } from "./libs/redis"
2626
import { version } from "./lerna.json"
2727
export { Express } from "express-serve-static-core"
2828

@@ -67,7 +67,7 @@ export async function expressApolloSetup() {
6767
// Always allow introspection - our schema is public
6868
introspection: true,
6969
// @ts-expect-error Type mismatch for keyv and ioredis recent releases
70-
cache: new KeyvAdapter(new Keyv({ store: new KeyvRedis(redis) })),
70+
cache: new KeyvAdapter(new Keyv({ store: new KeyvRedis(getRedis()) })),
7171
plugins: [
7272
ApolloServerPluginLandingPageLocalDefault(),
7373
ApolloServerPluginDrainHttpServer({

packages/openneuro-server/src/datalad/__tests__/contributors.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ vi.mock("../../cache/item")
2727
vi.mock("../files")
2828
vi.mock("../../utils/datasetOrSnapshot")
2929
vi.mock("../libs/redis", () => ({
30-
redis: vi.fn(),
30+
getRedis: vi.fn(),
3131
}))
3232

3333
const mockYamlLoad = vi.mocked(yaml.load)

packages/openneuro-server/src/datalad/__tests__/files.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import type { TreeEntry } from "../../cache/tree"
1313

1414
vi.mock("ioredis")
1515
vi.mock("../../config.ts")
16-
vi.mock("../../libs/redis", () => ({ redis: {} }))
16+
vi.mock("../../libs/redis", () => ({ getRedis: () => ({}) }))
1717
vi.mock("../../libs/presign", () => ({
1818
getPresignedUrl: vi.fn().mockResolvedValue(
1919
"https://s3.amazonaws.com/bucket/key?presigned=true",

packages/openneuro-server/src/datalad/__tests__/snapshots.spec.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@ import { connect } from "mongoose"
1010
// Mock requests to Datalad service
1111
vi.mock("superagent")
1212
vi.mock("../../libs/redis.js", () => ({
13-
redis: {
13+
getRedis: () => ({
1414
del: vi.fn(),
15-
},
16-
redlock: {
15+
}),
16+
getRedlock: () => ({
1717
lock: vi.fn().mockImplementation(() => ({ unlock: vi.fn() })),
18-
},
18+
}),
1919
}))
2020
// Mock draft files calls
2121
vi.mock("../draft.ts", () => ({

packages/openneuro-server/src/datalad/contributors.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import * as Sentry from "@sentry/node"
22
import CacheItem, { CacheType } from "../cache/item"
3-
import { redis } from "../libs/redis"
3+
import { getRedis } from "../libs/redis"
44
import {
55
type DatasetOrSnapshot,
66
datasetOrSnapshot,
@@ -26,7 +26,7 @@ export const contributors = async (
2626
if (!datasetId) return []
2727

2828
const revisionShort = revision ? revision.substring(0, 7) : "HEAD"
29-
const dataciteCache = new CacheItem(redis, CacheType.dataciteYml, [
29+
const dataciteCache = new CacheItem(getRedis(), CacheType.dataciteYml, [
3030
datasetId,
3131
revisionShort,
3232
])

packages/openneuro-server/src/datalad/dataset.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import type * as Mongoose from "mongoose"
1212
import config from "../config"
1313
import * as subscriptions from "../handlers/subscriptions"
1414
import { generateDataladCookie } from "../libs/authentication/jwt"
15-
import { redis } from "../libs/redis"
15+
import { getRedis } from "../libs/redis"
1616
import CacheItem, { CacheType } from "../cache/item"
1717
import { getDraftRevision, updateDatasetRevision } from "./draft"
1818
import { encodeFilePath, filesUrl, fileUrl, getFileName } from "./files"
@@ -114,7 +114,7 @@ export const deleteDataset = async (datasetId, user) => {
114114
export const cacheDatasetConnection = (options) => (connectionArguments) => {
115115
const connection = datasetsConnection(options)
116116
const cache = new CacheItem(
117-
redis,
117+
getRedis(),
118118
CacheType.datasetsConnection,
119119
[objectHash(options)],
120120
60,

packages/openneuro-server/src/datalad/description.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*/
44
import config from "../config"
55
import request from "superagent"
6-
import { redis } from "../libs/redis"
6+
import { getRedis } from "../libs/redis"
77
import { commitFiles } from "./dataset"
88
import { fileUrl } from "./files"
99
import { generateDataladCookie } from "../libs/authentication/jwt"
@@ -135,7 +135,7 @@ export const description = async (obj) => {
135135
Name: datasetId,
136136
BIDSVersion: "1.8.0",
137137
}
138-
const cache = new CacheItem(redis, CacheType.datasetDescription, [
138+
const cache = new CacheItem(getRedis(), CacheType.datasetDescription, [
139139
datasetId,
140140
revision.substring(0, 7),
141141
])

0 commit comments

Comments
 (0)