From c2fbfa06eb48240033c22ffedac0afd425199348 Mon Sep 17 00:00:00 2001 From: schneefux Date: Thu, 13 Apr 2017 15:17:39 +0200 Subject: send progress report to web --- worker.js | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/worker.js b/worker.js index 470e4bd..7e30db7 100644 --- a/worker.js +++ b/worker.js @@ -31,9 +31,10 @@ if (MADGLORY_TOKEN == undefined) throw "Need an API token"; await ch.prefetch(1); ch.consume("grab", async (msg) => { - let payload = JSON.parse(msg.content.toString()); + let payload = JSON.parse(msg.content.toString()), + notify = msg.properties.headers.notify; // where to send progress report if (msg.properties.type == "matches") - await getAPI(payload, "matches"); + await getAPI(payload, "matches", notify); if (msg.properties.type == "samples") await getAPI(payload, "samples"); @@ -50,8 +51,8 @@ if (MADGLORY_TOKEN == undefined) throw "Need an API token"; ch.ack(msg); }, { noAck: false }); - // loop over API data pages - async function getAPI(payload, where) { + // loop over API data pages, notify of progress + async function getAPI(payload, where, notify="global") { let exhausted = false; payload.params["page[limit]"] = payload.params["page[limit]"] || 50; payload.params["page[offset]"] = payload.params["page[offset]"] || 0; @@ -87,7 +88,9 @@ if (MADGLORY_TOKEN == undefined) throw "Need an API token"; { persistent: true, type: "sample" }) )); } - if (datas.length < 50) exhausted = true; + // tell web about progress + await ch.publish("amq.topic", notify, + new Buffer("grab_success")); } catch (err) { if (err.statusCode == 429) { await sleep(1000); @@ -99,6 +102,8 @@ if (MADGLORY_TOKEN == undefined) throw "Need an API token"; } console.log(err.statusCode); failed = true; + await ch.publish("amq.topic", notify, + new Buffer("grab_failed")); } // next page -- cgit v1.3.1