Skip to content

Commit

Permalink
Moved e18 job into flow for retry
Browse files Browse the repository at this point in the history
  • Loading branch information
jorgtho committed Jun 20, 2022
1 parent 750bc66 commit c1bf8f7
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 14 deletions.
11 changes: 5 additions & 6 deletions GetDocuments/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const { DEMO, DISABLE_LOGGING } = require('../config')
const { documentsRequest } = require('../lib/generate-request')
const getDocuments = require('../lib/isi-lokal')
const { getVariables, updateVariables } = require('../lib/handle-variables')
const { createJob } = require('../lib/e18')

const isEmptyDocument = documents => documents.length === 1 && documents[0].Fodselsnummer === '' && documents[0].Dokumentelement.Dokumenttype === ''

Expand All @@ -17,6 +16,11 @@ module.exports = async function (context, req) {
}
})

// HandleDocument sets suffix, and it is "inherited" here, so we reset it.
logConfig({
suffix: ''
})

try {
logger('info', ['GetDocuments', 'start'])

Expand Down Expand Up @@ -84,11 +88,6 @@ module.exports = async function (context, req) {
nextRun: '',
retryCount: 0
}
// create e18 job for this document
const jobId = await createJob(blobName)
if (jobId) {
document.e18JobId = jobId
}

await save(`queue/${blobName}.json`, JSON.stringify(document, null, 2))
logger('info', ['GetDocuments', `${blobName} successfully uploaded to blob storage`])
Expand Down
14 changes: 10 additions & 4 deletions lib/archive.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { logger } = require('@vtfk/logger')
const axios = require('axios').default
const { ARCHIVE_URL, ARCHIVE_KEY } = require('../config')
const { createTask } = require('./e18')
const { createTask, createJob } = require('./e18')

const callArchive = async (payload, e18JobId, e18TaskId, endpoint) => {
try {
Expand Down Expand Up @@ -90,11 +90,16 @@ module.exports.signOff = async blobContent => {
}

module.exports.syncElevmappa = async blobContent => {
const { e18JobId, Fodselsnummer, Fornavn, Etternavn, FolkeRegisterAdresse } = blobContent
blobContent.flow.syncElevmappaTaskId = blobContent.flow.syncElevmappaTaskId || await createTask(blobContent.e18JobId, { system: 'p360', method: 'SyncElevmappe' })
const { e18JobId, Fodselsnummer, Fornavn, Etternavn, FolkeRegisterAdresse, Dokumentelement: { Dokumenttype, DokumentId } } = blobContent

// first try, ssn
try {
// E18
logger('info', ['archive', 'syncElevmappa', 'calling E18'])
const blobName = `${Dokumenttype}_${DokumentId}`
blobContent.e18JobId = blobContent.e18JobId || await createJob(blobName)
blobContent.flow.syncElevmappaTaskId = blobContent.flow.syncElevmappaTaskId || await createTask(blobContent.e18JobId, { system: 'p360', method: 'SyncElevmappe' })

// first try, ssn
logger('info', ['archive', 'syncElevmappa', 'calling ssn'])
const res = await callArchive({ ssn: Fodselsnummer }, e18JobId, blobContent.flow.syncElevmappaTaskId, 'SyncElevmappe')
if (typeof res === 'object') {
Expand All @@ -105,6 +110,7 @@ module.exports.syncElevmappa = async blobContent => {
} catch (error) {
blobContent.flow.syncElevmappa = { error }
blobContent.flow.syncElevmappa.status = 'failed'
return blobContent
}

// last try, skip DSF. use street address from VIGO
Expand Down
12 changes: 8 additions & 4 deletions lib/e18.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ module.exports.createJob = async blobName => {
} catch (error) {
const { status, message } = error.response
logger('error', ['e18', 'createJob', message, status])
return null
throw error
}
}

Expand All @@ -40,7 +40,7 @@ module.exports.createTask = async (jobId, payload) => {
} catch (error) {
const { status, message } = error.response
logger('error', ['e18', 'createTask', message, status])
return false
throw error
}
}

Expand Down Expand Up @@ -81,7 +81,11 @@ module.exports.createTaskTemplate = async blobContent => {
payload.tags.push(blobContent.flow.archive.DocumentNumber)
}

const taskCreated = await this.createTask(blobContent.e18JobId, payload)
blobContent.flow.e18StatsTask = { status: taskCreated ? 'finished' : 'failed' }
try {
await this.createTask(blobContent.e18JobId, payload)
blobContent.flow.e18StatsTask = { status: 'finished' }
} catch (error) {
blobContent.flow.e18StatsTask = { status: 'failed' }
}
return blobContent
}

0 comments on commit c1bf8f7

Please sign in to comment.