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

log errors to db for later inspection or for sending warnings #7

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
7 changes: 5 additions & 2 deletions app.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const CRON_FREQUENCY = process.env.CACHING_CRON_PATTERN || '0 */5 * * * *';
const MAX_ATTEMPTS = parseInt(process.env.MAX_ATTEMPTS || 10);
const SEARCH_GRAPH = process.env.SEARCH_GRAPH || 'http://mu.semte.ch/graphs/public';
import bodyParser from 'body-parser';
import { DEFAULT_LOGS_GRAPH as LOGS_GRAPH, saveLog } from './logs';

console.info(`besluit-publicatie-publish-service starting at ${new Date()}`);
console.debug({
Expand All @@ -15,6 +16,7 @@ console.debug({
MAX_ATTEMPTS,
SEARCH_GRAPH
});

async function startPublishing(origin = "http call"){
console.log(`Service triggered by ${origin} at ${new Date().toISOString()}`);
let unprocessedResources = await getUnprocessedPublishedResources(SEARCH_GRAPH, PENDING_TIMEOUT, MAX_ATTEMPTS);
Expand All @@ -36,9 +38,10 @@ async function startPublishing(origin = "http call"){
}

catch(e){
console.log(`Error processing: ${item.resource}`);
console.log(e);
console.error(`Error processing: ${item.resource}`);
console.error(e);
await updateStatus(item, FAILED_STATUS, item.numberOfRetries);
await saveLog(LOGS_GRAPH, 'app', e, { resourceURI: item.resource} );
}
}
}
Expand Down
32 changes: 32 additions & 0 deletions support/logs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import { updateSudo as update } from '@lblod/mu-auth-sudo';
import { sparqlEscapeString, sparqlEscapeUri, sparqlEscapeDateTime, uuid } from 'mu';

/**
* Save the log into the database.
*/
export async function saveLog(logsGraph, classNameUri, message, specificInformation) {
const logEntryUuid = uuid();
const logEntryUri = "http://data.lblod.info/id/log-entries/".concat(logEntryUuid);

const stringifiedSpecificInformation = JSON.stringify(specificInformation);

const result = await update(`
PREFIX rlog: <http://persistence.uni-leipzig.org/nlp2rdf/ontologies/rlog#>
PREFIX dct: <http://purl.org/dc/terms/>
PREFIX ext: <http://mu.semte.ch/vocabularies/ext/>
INSERT DATA {
GRAPH ${sparqlEscapeUri(logsGraph)} {
${sparqlEscapeUri(logEntryUri)} a rlog:Entry ;
<http://mu.semte.ch/vocabularies/core/uuid> ${sparqlEscapeString(logEntryUuid)} ;
dct:source <https://github.com/lblod/besluit-publicatie-publish-service> ;
rlog:className ${sparqlEscapeUri(classNameUri)} ;
rlog:message ${sparqlEscapeString(message)} ;
rlog:date ${sparqlEscapeDateTime(new Date())} ;
rlog:level <http://data.lblod.info/id/log-levels/3af9ebe1-e6a8-495c-a392-16ced1f38ef1> ;
ext:specificInformation ${sparqlEscapeString(stringifiedSpecificInformation)} .
}
}
`);
};

export const DEFAULT_LOGS_GRAPH = 'http://mu.semte.ch/graphs/logs';
12 changes: 4 additions & 8 deletions support/pipeline.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { getRelationDataForZitting, persistExtractedData, belongsToType, cleanUp
import { uuid } from 'mu';
import crypto from 'crypto';
import { JSDOM } from 'jsdom';
import { DEFAULT_LOGS_GRAPH as LOGS_GRAPH, saveLog } from './logs';

/**
* Main entry point for extraction of data.
Expand All @@ -21,7 +22,6 @@ async function startPipeline(resourceToPublish){
let contexts = analyse( doc.getTopDomNode() );
let triples = flatTriples(contexts.map((c) => c.context));
triples = preProcess(triples);

await insertZitting(triples, resourceToPublish);
await insertAgenda(triples, resourceToPublish);
await insertUittreksel(triples, resourceToPublish, doc, contexts);
Expand Down Expand Up @@ -233,12 +233,9 @@ function orderGebeurtNa(triples, type = 'http://data.vlaanderen.be/ns/besluit#Ag
let ap1 = rootAps[0];

if(!ap1) return triples;

let currIndex = 0;
let currAp = ap1;
try {

let currIndex = 0;
let currAp = ap1;

orderInformation.push({ subject: ap1, predicate: 'http://schema.org/position', object: currIndex });

while (currIndex < childAps.length) {
Expand All @@ -257,12 +254,11 @@ function orderGebeurtNa(triples, type = 'http://data.vlaanderen.be/ns/besluit#Ag
}
catch(e){
console.warn(e);

saveLog(LOGS_GRAPH, 'pipeline', 'could not determine correct order of resources, position not set', {type, lastIndex: currIndex, lastResource: currAp});
if(rootAps.length > 1){
console.warn(`Found ${rootAps.length} potential root ${type}`);
console.warn(`See also ${rootAps.join('\n')} for broken data.`);
}

console.warn('Returning random order');
return triples;
}
Expand Down