Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent crashing when output directory does not exists #133

Merged
merged 2 commits into from
Jun 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 14 additions & 2 deletions consumer-server/src/logic/lod-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,7 @@ export function createLodGeneratorComponent({ logs }: Pick<AppComponents, 'logs'

result = await new Promise((resolve, _) => {
childProcess.on('error', (error) => {
const generatedFiles = fs.readdirSync(processOutput)
const logFile = generatedFiles.find((file) => file.endsWith('output.txt')) || ''
const logFile = fs.existsSync(processOutput) ? fs.readdirSync(processOutput).find((file) => file.endsWith('output.txt')) || '' : ''

resolve({
error: {
Expand All @@ -53,6 +52,19 @@ export function createLodGeneratorComponent({ logs }: Pick<AppComponents, 'logs'
})

childProcess.on('exit', (code) => {
if (!fs.existsSync(processOutput)) {
resolve({
error: {
message: 'Output directory does not exist',
detailedError: `Directory ${processOutput} was not created`
},
lodsFiles: [],
logFile: '',
outputPath: processOutput
})
return
}

const generatedFiles = fs.readdirSync(processOutput)
const logFile = generatedFiles.find((file) => file.endsWith('output.txt')) || ''
if (code !== 0) {
Expand Down
8 changes: 6 additions & 2 deletions consumer-server/src/logic/message-processor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,12 @@ export async function createMessageProcesorComponent({
base,
attempt: retry + 1
})
await storage.storeFiles([lodGenerationResult.logFile], `failures/${base}`)
metrics.increment('lod_generation_count', { status: 'failed' }, 1)

if (!!lodGenerationResult.logFile) {
logger.debug(`Output file exists, uploading it to the bucket`)
await storage.storeFiles([lodGenerationResult.logFile], `failures/${base}`)
metrics.increment('lod_generation_count', { status: 'failed' }, 1)
}
}

await queue.deleteMessage(receiptMessageHandle)
Expand Down
4 changes: 2 additions & 2 deletions consumer-server/test/unit/message-processor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ describe('message-processor', () => {
const components = getMessageProcessorMockComponents()
components.lodGenerator.generate.mockResolvedValue({
lodsFiles: [],
logFile: '',
logFile: 'somefile',
error: {
message: 'Error message',
detailedError: 'Detailed error message'
Expand All @@ -85,7 +85,7 @@ describe('message-processor', () => {
await messageProcessor.process(message, 'receiptHandle-3')

expect(components.queue.deleteMessage).toHaveBeenCalledWith('receiptHandle-3')
expect(components.storage.storeFiles).toHaveBeenCalledWith([''], 'failures/0,0')
expect(components.storage.storeFiles).toHaveBeenCalledWith(['somefile'], 'failures/0,0')
expect(components.queue.send).not.toHaveBeenCalled()
})

Expand Down
Loading