Skip to content

Commit

Permalink
Merge pull request #3052 from OpenNeuroOrg/3047-draft-cache-fix
Browse files Browse the repository at this point in the history
Bust cache for draft files when file exists on S3
  • Loading branch information
nellh authored May 8, 2024
2 parents 1d04778 + 7cee72f commit edd8c62
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
1 change: 1 addition & 0 deletions packages/openneuro-server/src/cache/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export enum CacheType {
snapshotIndex = "snapshotIndex",
participantCount = "participantCount",
snapshotDownload = "download",
draftRevision = "revision",
}
21 changes: 13 additions & 8 deletions packages/openneuro-server/src/datalad/draft.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,21 @@
import request from "superagent"
import Dataset from "../models/dataset"
import { getDatasetWorker } from "../libs/datalad-service"
import CacheItem, { CacheType } from "../cache/item"
import { redis } from "../libs/redis"

export const getDraftRevision = async (datasetId) => {
const draftUrl = `http://${
getDatasetWorker(
datasetId,
)
}/datasets/${datasetId}/draft`
const response = await fetch(draftUrl)
const { hexsha } = await response.json()
return hexsha
const cache = new CacheItem(redis, CacheType.draftRevision, [datasetId], 10)
return cache.get(async (_doNotCache): Promise<string> => {
const draftUrl = `http://${
getDatasetWorker(
datasetId,
)
}/datasets/${datasetId}/draft`
const response = await fetch(draftUrl)
const { hexsha } = await response.json()
return hexsha
})
}

export const updateDatasetRevision = (datasetId, gitRef) => {
Expand Down
5 changes: 4 additions & 1 deletion packages/openneuro-server/src/handlers/datalad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Readable } from "node:stream"
import mime from "mime-types"
import { getFiles } from "../datalad/files"
import { getDatasetWorker } from "../libs/datalad-service"
import { getDraftRevision } from "../datalad/draft"

/**
* Handlers for datalad dataset manipulation
Expand All @@ -21,7 +22,9 @@ export const getFile = async (req, res) => {
const worker = getDatasetWorker(datasetId)
// Find the right tree
const pathComponents = filename.split(":")
let tree = snapshotId || "HEAD"
// Get the draft commit for cache busting
const draftCommit = await getDraftRevision(datasetId)
let tree = snapshotId || draftCommit
let file
for (const level of pathComponents) {
try {
Expand Down

0 comments on commit edd8c62

Please sign in to comment.