Skip to content

Commit

Permalink
fix: Better logging for HasContentChanged (#169)
Browse files Browse the repository at this point in the history
  • Loading branch information
dalkia authored Oct 28, 2024
1 parent 7113be8 commit cc0da5b
Showing 1 changed file with 26 additions and 13 deletions.
39 changes: 26 additions & 13 deletions consumer-server/src/logic/has-content-changed-task.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@ async function getActiveEntity(ids: string, contentServer: string): Promise<Enti
return JSON.parse(response)[0]
}

async function getManifestFiles(entityID: string, buildTarget: string, env: string): Promise<any | null> {
async function getManifestFiles(
entityID: string,
buildTarget: string,
env: string,
logger: ILoggerComponent.ILogger
): Promise<any | null> {
const url = `https://ab-cdn.decentraland.${env}/manifest/${entityID}_${buildTarget}.json`

const res = await fetch(url)
Expand All @@ -34,7 +39,7 @@ async function getManifestFiles(entityID: string, buildTarget: string, env: stri
if (response.exitCode === 0) {
return response
} else {
console.error(`Error: exitCode is ${response.exitCode}`)
logger.error(`Error: exitCode is ${response.exitCode}`)
return null
}
}
Expand Down Expand Up @@ -102,7 +107,8 @@ async function downloadFilesFromManifestSuccesfully(
buildTarget: string,
previousHash: string,
outputFolder: string,
env: string
env: string,
logger: ILoggerComponent.ILogger
): Promise<boolean> {
const baseUrl = `https://ab-cdn.decentraland.${env}/${version}/${previousHash}/`

Expand All @@ -122,9 +128,9 @@ async function downloadFilesFromManifestSuccesfully(
// Write the file to the output folder
fs.writeFileSync(outputPath, buffer)

console.log(`Downloaded and saved: ${outputPath}`)
logger.log(`Downloaded and saved: ${outputPath}`)
} catch (error) {
console.error(`Error downloading file ${file}:`, error)
logger.log(`Error downloading file ${file} from ${fileUrl}: ${error}`)
return false
}
}
Expand All @@ -140,8 +146,6 @@ async function DeleteFilesInOutputFolder(outputFolder: string): Promise<void> {
const filePath = path.join(outputFolder, file)
fs.unlinkSync(filePath) // Delete each file
}

console.log(`Deleted all files in ${outputFolder}`)
}
}

Expand All @@ -157,14 +161,13 @@ export async function hasContentChange(
logger: ILoggerComponent.ILogger
): Promise<boolean> {
const entity = await getActiveEntity(entityId, contentServerUrl)

if (entity.type === 'scene') {
logger.info(`HasContentChanged: Entity ${entityId} is a scene`)
const environemnt = contentServerUrl.includes('org') ? 'org' : 'zone'
const previousHash = await getLastEntityIdByBase(entityId, entity.metadata.scene.base, contentServerUrl)
if (previousHash !== null) {
logger.info(`HasContentChanged: Previous hash is ${previousHash}`)
const manifest = await getManifestFiles(previousHash, buildTarget, environemnt)
const manifest = await getManifestFiles(previousHash, buildTarget, environemnt, logger)
if (manifest !== null) {
logger.info(`HasContentChanged: Manifest exists for hash ${previousHash}`)
if (manifest.version === abVersion) {
Expand All @@ -179,20 +182,30 @@ export async function hasContentChange(
buildTarget,
previousHash,
outputFolder,
environemnt
environemnt,
logger
)
//If all files download successfully, content has not changed
if (allFilesDownloadSuccesfully) {
logger.info(`HasContentChanged: All files downloaded successfully`)
return false
} else {
logger.info(`HasContentChanged: Some downloads failed`)
//In case we downloaded some file, remove the corrupt state
logger.info(`HasContentChanged Error: Some files failed to download`)
await DeleteFilesInOutputFolder(outputFolder)
}
} else {
logger.info(`HasContentChanged Error: Not all entities contained in old manifest`)
}
} else {
logger.info(`HasContentChanged Error: Manifest versions are different`)
}
} else {
logger.info(`HasContentChanged Error: Manifest does not exist for hash ${previousHash}`)
}
} else {
logger.info(`HasContentChanged Error: Previous hash is null`)
}
} else {
logger.info(`HasContentChanged Error: Entity ${entityId} is not a scene`)
}
return true
}

0 comments on commit cc0da5b

Please sign in to comment.