Skip to content

Commit

Permalink
Merge pull request #55 from lblod/feature/prepublish-with-job
Browse files Browse the repository at this point in the history
Use a job for a pre-publishing
  • Loading branch information
abeforgit authored Aug 18, 2021
2 parents a50d6c4 + 22f334d commit f043975
Showing 1 changed file with 62 additions and 6 deletions.
68 changes: 62 additions & 6 deletions routes/preview.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,61 @@ const router = express.Router();
*
*/

/**
* Contains the job results.
*/
const prepublishJobResults = new Map();

/**
* Adds a job result.
*
* @param {string} uuid The uuid of the job.
* @param {number} status HTTP status code.
* @param {object} result The response to send to the user.
*/
function pushJobResult( uuid, status, result ) {
prepublishJobResults.set( uuid, { status, result } );
}

/**
* Creates a new job.
*
* @param res Ngenix response object.
* @return job Uuid
*/
function yieldJobId(res) {
const jobUuid = uuid();
res
.status(200)
.send({
data: {
attributes: {
jobId: jobUuid
},
type: "prebulish-jobs"
}
});

return jobUuid;
}

router.get('/prepublish/job-result/:jobUuid', (req, res) => {
const jobUuid = req.params.jobUuid;

if( prepublishJobResults.has(jobUuid) ) {
const { status, result } = prepublishJobResults.get(jobUuid);
prepublishJobResults.delete(jobUuid);
res
.status( status )
.send( result );
} else {
res
.status( 404 )
.send("UUID not found, production may be ongoing yet.");
}
});


/**
* Prepublish an agenda as HTML+RDFa snippet for a given document
* The snippet is not persisted in the store
Expand Down Expand Up @@ -62,17 +117,18 @@ router.get('/prepublish/besluitenlijst/:meetingUuid', async function(req, res, n
* Prepublish besluiten as HTML+RDFa snippet for a given document
* The snippets are not persisted in the store
*/
router.get('/prepublish/behandelingen/:zittingIdentifier', async function(req, res, next) {
router.get('/prepublish/behandelingen/:zittingIdentifier', async function(req, res) {
const jobUuid = yieldJobId( res );

try {
const extracts = await buildAllExtractsForMeeting(req.params.zittingIdentifier);
return res.send(extracts).end();
pushJobResult(jobUuid, 200, extracts);
// return res.send(extracts).end();
}
catch (err) {
console.log(err);
const error = new Error(`An error occured while fetching contents for prepublished besluiten ${req.params.documentIdentifier}: ${err}`);
// @ts-ignore
error.status = 500;
return next(error);
const errorString = `An error occured while fetching contents for prepublished besluiten ${req.params.zittingIdentifier}: ${err}`;
pushJobResult(jobUuid, 500, errorString);
}
});

Expand Down

0 comments on commit f043975

Please sign in to comment.