Skip to content

Commit 008cb3e

Browse files
Merge pull request #198 from okerekechinweotito/fix/pdl-library-failure
Fixes:[T348412] Handle PDL library failures
2 parents c5e1e41 + e695cfd commit 008cb3e

File tree

2 files changed

+73
-31
lines changed

2 files changed

+73
-31
lines changed

bull/pdl-queue/consumer.js

Lines changed: 55 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -28,33 +28,42 @@ async function getZipAndBytelength(no_of_pages, id, title, job) {
2828
title = title.replace(/ /g, "_");
2929
var img = zip.folder(`${title}_images`);
3030
let temp_pages = no_of_pages;
31+
let downloadImageStatus;
32+
let errorFlag = { status: false, page: "" };
3133
var download_image = async function (uri, filename) {
32-
await rp({
33-
method: "GET",
34-
uri,
35-
encoding: null,
36-
transform: function (body, response) {
37-
return { headers: response.headers, data: body };
38-
},
39-
})
40-
.then(async function (body) {
41-
if (/image/.test(body.headers["content-type"])) {
42-
var data = new Buffer(body.data);
43-
img.file(filename, data.toString("base64"), { base64: true });
44-
}
45-
})
46-
.catch(function (err) {
47-
--no_of_pages;
34+
try {
35+
const body = await rp({
36+
method: "GET",
37+
uri,
38+
encoding: null,
39+
transform: function (body, response) {
40+
return { headers: response.headers, data: body };
41+
},
4842
});
43+
if (/image/.test(body.headers["content-type"])) {
44+
var data = new Buffer(body.data);
45+
img.file(filename, data.toString("base64"), { base64: true });
46+
}
47+
return 200;
48+
} catch (err) {
49+
--no_of_pages;
50+
return err.statusCode;
51+
}
4952
};
5053
for (let i = 1; i <= temp_pages; ++i) {
5154
const str = `http://www.panjabdigilib.org/images?ID=${id}&page=${i}&pagetype=null&Searched=W3GX`;
52-
await download_image(str, `${title}_${i}.jpeg`);
55+
downloadImageStatus = await download_image(str, `${title}_${i}.jpeg`);
5356
job.progress(Math.round((i / temp_pages) * 82));
57+
if (downloadImageStatus >= 200 && downloadImageStatus < 300) {
58+
continue;
59+
} else {
60+
errorFlag = { status: true, page: str };
61+
break;
62+
}
5463
}
5564
let { byteLength } = await zip.generateAsync({ type: "nodebuffer" });
5665
byteLength = Number(byteLength + no_of_pages * 16); //No. of pages * 16
57-
return [zip, byteLength];
66+
return [zip, byteLength, errorFlag];
5867
}
5968

6069
function setHeaders(metadata, byteLength, title) {
@@ -117,22 +126,37 @@ PDLQueue.process(async (job, done) => {
117126
const trueURI = `http://archive.org/details/${job.data.details.IAIdentifier}`;
118127
jobLogs["trueURI"] = trueURI;
119128
jobLogs["userName"] = job.data.details.userName;
120-
job.log(JSON.stringify(jobLogs));
121-
logUserData(jobLogs["userName"], "Panjab Digital Library");
122-
const [zip, byteLength] = await getZipAndBytelength(
129+
const [zip, byteLength, errorFlag] = await getZipAndBytelength(
123130
job.data.details.Pages,
124131
job.data.details.bookID,
125132
job.data.details.title,
126133
job
127134
);
128-
job.progress(90);
129-
await uploadToIA(
130-
zip,
131-
job.data.details,
132-
byteLength,
133-
job.data.details.email,
134-
job
135-
);
136-
job.progress(100);
137-
done(null, true);
135+
if (errorFlag.status) {
136+
job.log(JSON.stringify(jobLogs));
137+
logUserData(jobLogs["userName"], "Panjab Digital Library");
138+
logger.log({
139+
level: "error",
140+
message: `Upload to Internet Archive failed because ${errorFlag.page} is not reachable. Please try again or contact Panjab Digital Library for more details.`,
141+
});
142+
job.progress(100);
143+
done(
144+
new Error(
145+
`Upload to Internet Archive failed because <a href=${errorFlag.page} target='_blank'>${errorFlag.page}</a> is not reachable. Please try again or contact Panjab Digital Library for more details.`
146+
)
147+
);
148+
} else {
149+
job.log(JSON.stringify(jobLogs));
150+
logUserData(jobLogs["userName"], "Panjab Digital Library");
151+
job.progress(90);
152+
await uploadToIA(
153+
zip,
154+
job.data.details,
155+
byteLength,
156+
job.data.details.email,
157+
job
158+
);
159+
job.progress(100);
160+
done(null, true);
161+
}
138162
});

components/QueueTable.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,22 @@ const ShowUploadQueue = (props) => {
117117
label: "Status",
118118
minWidth: 30,
119119
align: "left",
120+
format: (value) => {
121+
const isPDLMissingPage = /<a [^>]*>([^<]+)<\/a>/;
122+
const missingPageLink = isPDLMissingPage.exec(value);
123+
return missingPageLink ? (
124+
<span>
125+
Failed! (Reason: Upload to Internet Archive failed because {""}
126+
<a href={missingPageLink[1]} target="_blank">
127+
{missingPageLink[1]}
128+
</a>{" "}
129+
is not reachable. Please try again or contact Panjab Digital Library
130+
for more details. )
131+
</span>
132+
) : (
133+
value
134+
);
135+
},
120136
},
121137
{
122138
id: "upload_progress",
@@ -154,6 +170,8 @@ const ShowUploadQueue = (props) => {
154170
return column.format((value === "-" ? "" : "User:") + value);
155171
} else if (column.id === "date") {
156172
return column.format(value);
173+
} else if (column.id === "status") {
174+
return column.format(value);
157175
} else {
158176
return value;
159177
}

0 commit comments

Comments
 (0)